Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 7 | * the information ethtool needs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 10 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 13 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/ethtool.h> |
| 16 | #include <linux/netdevice.h> |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 17 | #include <linux/net_tstamp.h> |
| 18 | #include <linux/phy.h> |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 19 | #include <linux/bitops.h> |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
David S. Miller | 73da16c | 2010-09-21 16:12:11 -0700 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 22 | #include <linux/sfp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 24 | #include <linux/rtnetlink.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 25 | #include <linux/sched/signal.h> |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 26 | #include <linux/net.h> |
Heiner Kallweit | f32a213 | 2021-08-01 12:36:48 +0200 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Jakub Kicinski | ddb6e99 | 2019-01-31 10:50:47 -0800 | [diff] [blame] | 28 | #include <net/devlink.h> |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 29 | #include <net/xdp_sock_drv.h> |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 30 | #include <net/flow_offload.h> |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 31 | #include <linux/ethtool_netlink.h> |
Leon Romanovsky | 1c79031 | 2020-04-19 17:18:47 +0300 | [diff] [blame] | 32 | #include <generated/utsrelease.h> |
Michal Kubecek | d44e131 | 2019-12-11 10:58:29 +0100 | [diff] [blame] | 33 | #include "common.h" |
| 34 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 35 | /* State held across locks and calls for commands which have devlink fallback */ |
| 36 | struct ethtool_devlink_compat { |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 37 | struct devlink *devlink; |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 38 | union { |
| 39 | struct ethtool_flash efl; |
| 40 | struct ethtool_drvinfo info; |
| 41 | }; |
| 42 | }; |
| 43 | |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 44 | static struct devlink *netdev_to_devlink_get(struct net_device *dev) |
| 45 | { |
| 46 | struct devlink_port *devlink_port; |
| 47 | |
| 48 | if (!dev->netdev_ops->ndo_get_devlink_port) |
| 49 | return NULL; |
| 50 | |
| 51 | devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev); |
| 52 | if (!devlink_port) |
| 53 | return NULL; |
| 54 | |
| 55 | return devlink_try_get(devlink_port->devlink); |
| 56 | } |
| 57 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 58 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * Some useful ethtool_ops methods that're device independent. |
| 60 | * If we find that all drivers want to do the same thing here, |
| 61 | * we can turn these into dev_() function calls. |
| 62 | */ |
| 63 | |
| 64 | u32 ethtool_op_get_link(struct net_device *dev) |
| 65 | { |
| 66 | return netif_carrier_ok(dev) ? 1 : 0; |
| 67 | } |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 68 | EXPORT_SYMBOL(ethtool_op_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Richard Cochran | 02eacbd | 2012-04-03 22:59:22 +0000 | [diff] [blame] | 70 | int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) |
| 71 | { |
| 72 | info->so_timestamping = |
| 73 | SOF_TIMESTAMPING_TX_SOFTWARE | |
| 74 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 75 | SOF_TIMESTAMPING_SOFTWARE; |
| 76 | info->phc_index = -1; |
| 77 | return 0; |
| 78 | } |
| 79 | EXPORT_SYMBOL(ethtool_op_get_ts_info); |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* Handlers for each ethtool command */ |
| 82 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 83 | static int ethtool_get_features(struct net_device *dev, void __user *useraddr) |
| 84 | { |
| 85 | struct ethtool_gfeatures cmd = { |
| 86 | .cmd = ETHTOOL_GFEATURES, |
| 87 | .size = ETHTOOL_DEV_FEATURE_WORDS, |
| 88 | }; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 89 | struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 90 | u32 __user *sizeaddr; |
| 91 | u32 copy_size; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 92 | int i; |
| 93 | |
| 94 | /* in case feature bits run out again */ |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 95 | BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 96 | |
| 97 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 98 | features[i].available = (u32)(dev->hw_features >> (32 * i)); |
| 99 | features[i].requested = (u32)(dev->wanted_features >> (32 * i)); |
| 100 | features[i].active = (u32)(dev->features >> (32 * i)); |
| 101 | features[i].never_changed = |
| 102 | (u32)(NETIF_F_NEVER_CHANGE >> (32 * i)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 103 | } |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 104 | |
| 105 | sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size); |
| 106 | if (get_user(copy_size, sizeaddr)) |
| 107 | return -EFAULT; |
| 108 | |
| 109 | if (copy_size > ETHTOOL_DEV_FEATURE_WORDS) |
| 110 | copy_size = ETHTOOL_DEV_FEATURE_WORDS; |
| 111 | |
| 112 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 113 | return -EFAULT; |
| 114 | useraddr += sizeof(cmd); |
Gustavo A. R. Silva | ed71761 | 2021-09-28 14:57:35 -0500 | [diff] [blame] | 115 | if (copy_to_user(useraddr, features, |
| 116 | array_size(copy_size, sizeof(*features)))) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 117 | return -EFAULT; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int ethtool_set_features(struct net_device *dev, void __user *useraddr) |
| 123 | { |
| 124 | struct ethtool_sfeatures cmd; |
| 125 | struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 126 | netdev_features_t wanted = 0, valid = 0; |
| 127 | int i, ret = 0; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 128 | |
| 129 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 130 | return -EFAULT; |
| 131 | useraddr += sizeof(cmd); |
| 132 | |
| 133 | if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if (copy_from_user(features, useraddr, sizeof(features))) |
| 137 | return -EFAULT; |
| 138 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 139 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 140 | valid |= (netdev_features_t)features[i].valid << (32 * i); |
| 141 | wanted |= (netdev_features_t)features[i].requested << (32 * i); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (valid & ~NETIF_F_ETHTOOL_BITS) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 145 | return -EINVAL; |
| 146 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 147 | if (valid & ~dev->hw_features) { |
| 148 | valid &= dev->hw_features; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 149 | ret |= ETHTOOL_F_UNSUPPORTED; |
| 150 | } |
| 151 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 152 | dev->wanted_features &= ~valid; |
| 153 | dev->wanted_features |= wanted & valid; |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 154 | __netdev_update_features(dev); |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 155 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 156 | if ((dev->wanted_features ^ dev->features) & valid) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 157 | ret |= ETHTOOL_F_WISH; |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 162 | static int __ethtool_get_sset_count(struct net_device *dev, int sset) |
| 163 | { |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 164 | const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 165 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 166 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 167 | if (sset == ETH_SS_FEATURES) |
| 168 | return ARRAY_SIZE(netdev_features_strings); |
| 169 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 170 | if (sset == ETH_SS_RSS_HASH_FUNCS) |
| 171 | return ARRAY_SIZE(rss_hash_func_strings); |
| 172 | |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 173 | if (sset == ETH_SS_TUNABLES) |
| 174 | return ARRAY_SIZE(tunable_strings); |
| 175 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 176 | if (sset == ETH_SS_PHY_TUNABLES) |
| 177 | return ARRAY_SIZE(phy_tunable_strings); |
| 178 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 179 | if (sset == ETH_SS_PHY_STATS && dev->phydev && |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 180 | !ops->get_ethtool_phy_stats && |
| 181 | phy_ops && phy_ops->get_sset_count) |
| 182 | return phy_ops->get_sset_count(dev->phydev); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 183 | |
Michal Kubecek | 428c122 | 2019-12-11 10:58:34 +0100 | [diff] [blame] | 184 | if (sset == ETH_SS_LINK_MODES) |
| 185 | return __ETHTOOL_LINK_MODE_MASK_NBITS; |
| 186 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 187 | if (ops->get_sset_count && ops->get_strings) |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 188 | return ops->get_sset_count(dev, sset); |
| 189 | else |
| 190 | return -EOPNOTSUPP; |
| 191 | } |
| 192 | |
| 193 | static void __ethtool_get_strings(struct net_device *dev, |
| 194 | u32 stringset, u8 *data) |
| 195 | { |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 196 | const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 197 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 198 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 199 | if (stringset == ETH_SS_FEATURES) |
| 200 | memcpy(data, netdev_features_strings, |
| 201 | sizeof(netdev_features_strings)); |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 202 | else if (stringset == ETH_SS_RSS_HASH_FUNCS) |
| 203 | memcpy(data, rss_hash_func_strings, |
| 204 | sizeof(rss_hash_func_strings)); |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 205 | else if (stringset == ETH_SS_TUNABLES) |
| 206 | memcpy(data, tunable_strings, sizeof(tunable_strings)); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 207 | else if (stringset == ETH_SS_PHY_TUNABLES) |
| 208 | memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings)); |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 209 | else if (stringset == ETH_SS_PHY_STATS && dev->phydev && |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 210 | !ops->get_ethtool_phy_stats && phy_ops && |
| 211 | phy_ops->get_strings) |
| 212 | phy_ops->get_strings(dev->phydev, data); |
Michal Kubecek | 428c122 | 2019-12-11 10:58:34 +0100 | [diff] [blame] | 213 | else if (stringset == ETH_SS_LINK_MODES) |
| 214 | memcpy(data, link_mode_names, |
| 215 | __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN); |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 216 | else |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 217 | /* ops->get_strings is valid because checked earlier */ |
| 218 | ops->get_strings(dev, stringset, data); |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 221 | static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 222 | { |
| 223 | /* feature masks of legacy discrete ethtool ops */ |
| 224 | |
| 225 | switch (eth_cmd) { |
| 226 | case ETHTOOL_GTXCSUM: |
| 227 | case ETHTOOL_STXCSUM: |
Vladyslav Tarasiuk | 9d648fb | 2020-03-24 13:57:08 +0200 | [diff] [blame] | 228 | return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC | |
Michal Kubecek | f70bb06 | 2020-03-12 21:07:43 +0100 | [diff] [blame] | 229 | NETIF_F_SCTP_CRC; |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 230 | case ETHTOOL_GRXCSUM: |
| 231 | case ETHTOOL_SRXCSUM: |
| 232 | return NETIF_F_RXCSUM; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 233 | case ETHTOOL_GSG: |
| 234 | case ETHTOOL_SSG: |
Michal Kubecek | f70bb06 | 2020-03-12 21:07:43 +0100 | [diff] [blame] | 235 | return NETIF_F_SG | NETIF_F_FRAGLIST; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 236 | case ETHTOOL_GTSO: |
| 237 | case ETHTOOL_STSO: |
| 238 | return NETIF_F_ALL_TSO; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 239 | case ETHTOOL_GGSO: |
| 240 | case ETHTOOL_SGSO: |
| 241 | return NETIF_F_GSO; |
| 242 | case ETHTOOL_GGRO: |
| 243 | case ETHTOOL_SGRO: |
| 244 | return NETIF_F_GRO; |
| 245 | default: |
| 246 | BUG(); |
| 247 | } |
| 248 | } |
| 249 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 250 | static int ethtool_get_one_feature(struct net_device *dev, |
| 251 | char __user *useraddr, u32 ethcmd) |
| 252 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 253 | netdev_features_t mask = ethtool_get_feature_mask(ethcmd); |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 254 | struct ethtool_value edata = { |
| 255 | .cmd = ethcmd, |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 256 | .data = !!(dev->features & mask), |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 257 | }; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 258 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 259 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 260 | return -EFAULT; |
| 261 | return 0; |
| 262 | } |
| 263 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 264 | static int ethtool_set_one_feature(struct net_device *dev, |
| 265 | void __user *useraddr, u32 ethcmd) |
| 266 | { |
| 267 | struct ethtool_value edata; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 268 | netdev_features_t mask; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 269 | |
| 270 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 271 | return -EFAULT; |
| 272 | |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 273 | mask = ethtool_get_feature_mask(ethcmd); |
| 274 | mask &= dev->hw_features; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 275 | if (!mask) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 276 | return -EOPNOTSUPP; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 277 | |
| 278 | if (edata.data) |
| 279 | dev->wanted_features |= mask; |
| 280 | else |
| 281 | dev->wanted_features &= ~mask; |
| 282 | |
| 283 | __netdev_update_features(dev); |
| 284 | |
| 285 | return 0; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 288 | #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ |
| 289 | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 290 | #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ |
| 291 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ |
| 292 | NETIF_F_RXHASH) |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 293 | |
| 294 | static u32 __ethtool_get_flags(struct net_device *dev) |
| 295 | { |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 296 | u32 flags = 0; |
| 297 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 298 | if (dev->features & NETIF_F_LRO) |
| 299 | flags |= ETH_FLAG_LRO; |
| 300 | if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) |
| 301 | flags |= ETH_FLAG_RXVLAN; |
| 302 | if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) |
| 303 | flags |= ETH_FLAG_TXVLAN; |
| 304 | if (dev->features & NETIF_F_NTUPLE) |
| 305 | flags |= ETH_FLAG_NTUPLE; |
| 306 | if (dev->features & NETIF_F_RXHASH) |
| 307 | flags |= ETH_FLAG_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 308 | |
| 309 | return flags; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int __ethtool_set_flags(struct net_device *dev, u32 data) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 313 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 314 | netdev_features_t features = 0, changed; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 315 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 316 | if (data & ~ETH_ALL_FLAGS) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 319 | if (data & ETH_FLAG_LRO) |
| 320 | features |= NETIF_F_LRO; |
| 321 | if (data & ETH_FLAG_RXVLAN) |
| 322 | features |= NETIF_F_HW_VLAN_CTAG_RX; |
| 323 | if (data & ETH_FLAG_TXVLAN) |
| 324 | features |= NETIF_F_HW_VLAN_CTAG_TX; |
| 325 | if (data & ETH_FLAG_NTUPLE) |
| 326 | features |= NETIF_F_NTUPLE; |
| 327 | if (data & ETH_FLAG_RXHASH) |
| 328 | features |= NETIF_F_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 329 | |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 330 | /* allow changing only bits set in hw_features */ |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 331 | changed = (features ^ dev->features) & ETH_ALL_FEATURES; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 332 | if (changed & ~dev->hw_features) |
| 333 | return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; |
| 334 | |
| 335 | dev->wanted_features = |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 336 | (dev->wanted_features & ~changed) | (features & changed); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 337 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 338 | __netdev_update_features(dev); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Alan Brady | 5a6cd6d | 2017-10-05 14:53:40 -0700 | [diff] [blame] | 343 | /* Given two link masks, AND them together and save the result in dst. */ |
| 344 | void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst, |
| 345 | struct ethtool_link_ksettings *src) |
| 346 | { |
| 347 | unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS); |
| 348 | unsigned int idx = 0; |
| 349 | |
| 350 | for (; idx < size; idx++) { |
| 351 | dst->link_modes.supported[idx] &= |
| 352 | src->link_modes.supported[idx]; |
| 353 | dst->link_modes.advertising[idx] &= |
| 354 | src->link_modes.advertising[idx]; |
| 355 | } |
| 356 | } |
| 357 | EXPORT_SYMBOL(ethtool_intersect_link_masks); |
| 358 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 359 | void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst, |
| 360 | u32 legacy_u32) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 361 | { |
Sean Anderson | 4973056 | 2021-10-22 18:41:04 -0400 | [diff] [blame] | 362 | linkmode_zero(dst); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 363 | dst[0] = legacy_u32; |
| 364 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 365 | EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 366 | |
| 367 | /* return false if src had higher bits set. lower bits always updated. */ |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 368 | bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32, |
| 369 | const unsigned long *src) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 370 | { |
| 371 | bool retval = true; |
| 372 | |
| 373 | /* TODO: following test will soon always be true */ |
| 374 | if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) { |
| 375 | __ETHTOOL_DECLARE_LINK_MODE_MASK(ext); |
| 376 | |
Sean Anderson | 4973056 | 2021-10-22 18:41:04 -0400 | [diff] [blame] | 377 | linkmode_zero(ext); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 378 | bitmap_fill(ext, 32); |
| 379 | bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS); |
Sean Anderson | 4973056 | 2021-10-22 18:41:04 -0400 | [diff] [blame] | 380 | if (linkmode_intersects(ext, src)) { |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 381 | /* src mask goes beyond bit 31 */ |
| 382 | retval = false; |
| 383 | } |
| 384 | } |
| 385 | *legacy_u32 = src[0]; |
| 386 | return retval; |
| 387 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 388 | EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 389 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 390 | /* return false if ksettings link modes had higher bits |
| 391 | * set. legacy_settings always updated (best effort) |
| 392 | */ |
| 393 | static bool |
| 394 | convert_link_ksettings_to_legacy_settings( |
| 395 | struct ethtool_cmd *legacy_settings, |
| 396 | const struct ethtool_link_ksettings *link_ksettings) |
| 397 | { |
| 398 | bool retval = true; |
| 399 | |
| 400 | memset(legacy_settings, 0, sizeof(*legacy_settings)); |
| 401 | /* this also clears the deprecated fields in legacy structure: |
| 402 | * __u8 transceiver; |
| 403 | * __u32 maxtxpkt; |
| 404 | * __u32 maxrxpkt; |
| 405 | */ |
| 406 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 407 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 408 | &legacy_settings->supported, |
| 409 | link_ksettings->link_modes.supported); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 410 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 411 | &legacy_settings->advertising, |
| 412 | link_ksettings->link_modes.advertising); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 413 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 414 | &legacy_settings->lp_advertising, |
| 415 | link_ksettings->link_modes.lp_advertising); |
| 416 | ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed); |
| 417 | legacy_settings->duplex |
| 418 | = link_ksettings->base.duplex; |
| 419 | legacy_settings->port |
| 420 | = link_ksettings->base.port; |
| 421 | legacy_settings->phy_address |
| 422 | = link_ksettings->base.phy_address; |
| 423 | legacy_settings->autoneg |
| 424 | = link_ksettings->base.autoneg; |
| 425 | legacy_settings->mdio_support |
| 426 | = link_ksettings->base.mdio_support; |
| 427 | legacy_settings->eth_tp_mdix |
| 428 | = link_ksettings->base.eth_tp_mdix; |
| 429 | legacy_settings->eth_tp_mdix_ctrl |
| 430 | = link_ksettings->base.eth_tp_mdix_ctrl; |
Florian Fainelli | 19cab88 | 2017-09-20 15:52:13 -0700 | [diff] [blame] | 431 | legacy_settings->transceiver |
| 432 | = link_ksettings->base.transceiver; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 433 | return retval; |
| 434 | } |
| 435 | |
| 436 | /* number of 32-bit words to store the user's link mode bitmaps */ |
| 437 | #define __ETHTOOL_LINK_MODE_MASK_NU32 \ |
| 438 | DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32) |
| 439 | |
| 440 | /* layout of the struct passed from/to userland */ |
| 441 | struct ethtool_link_usettings { |
| 442 | struct ethtool_link_settings base; |
| 443 | struct { |
| 444 | __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 445 | __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 446 | __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 447 | } link_modes; |
| 448 | }; |
| 449 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 450 | /* Internal kernel helper to query a device ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 451 | int __ethtool_get_link_ksettings(struct net_device *dev, |
| 452 | struct ethtool_link_ksettings *link_ksettings) |
| 453 | { |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 454 | ASSERT_RTNL(); |
| 455 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 456 | if (!dev->ethtool_ops->get_link_ksettings) |
David Decotigny | 3237fc6 | 2016-02-24 10:58:11 -0800 | [diff] [blame] | 457 | return -EOPNOTSUPP; |
| 458 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 459 | memset(link_ksettings, 0, sizeof(*link_ksettings)); |
Danielle Ratson | a975d7d | 2021-04-07 13:06:51 +0300 | [diff] [blame] | 460 | return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 461 | } |
| 462 | EXPORT_SYMBOL(__ethtool_get_link_ksettings); |
| 463 | |
| 464 | /* convert ethtool_link_usettings in user space to a kernel internal |
| 465 | * ethtool_link_ksettings. return 0 on success, errno on error. |
| 466 | */ |
| 467 | static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to, |
| 468 | const void __user *from) |
| 469 | { |
| 470 | struct ethtool_link_usettings link_usettings; |
| 471 | |
| 472 | if (copy_from_user(&link_usettings, from, sizeof(link_usettings))) |
| 473 | return -EFAULT; |
| 474 | |
| 475 | memcpy(&to->base, &link_usettings.base, sizeof(to->base)); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 476 | bitmap_from_arr32(to->link_modes.supported, |
| 477 | link_usettings.link_modes.supported, |
| 478 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 479 | bitmap_from_arr32(to->link_modes.advertising, |
| 480 | link_usettings.link_modes.advertising, |
| 481 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 482 | bitmap_from_arr32(to->link_modes.lp_advertising, |
| 483 | link_usettings.link_modes.lp_advertising, |
| 484 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Cris Forno | 70ae1e1 | 2020-02-28 14:12:04 -0600 | [diff] [blame] | 489 | /* Check if the user is trying to change anything besides speed/duplex */ |
| 490 | bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd) |
| 491 | { |
| 492 | struct ethtool_link_settings base2 = {}; |
| 493 | |
| 494 | base2.speed = cmd->base.speed; |
| 495 | base2.port = PORT_OTHER; |
| 496 | base2.duplex = cmd->base.duplex; |
| 497 | base2.cmd = cmd->base.cmd; |
| 498 | base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords; |
| 499 | |
| 500 | return !memcmp(&base2, &cmd->base, sizeof(base2)) && |
| 501 | bitmap_empty(cmd->link_modes.supported, |
| 502 | __ETHTOOL_LINK_MODE_MASK_NBITS) && |
| 503 | bitmap_empty(cmd->link_modes.lp_advertising, |
| 504 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 505 | } |
| 506 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 507 | /* convert a kernel internal ethtool_link_ksettings to |
| 508 | * ethtool_link_usettings in user space. return 0 on success, errno on |
| 509 | * error. |
| 510 | */ |
| 511 | static int |
| 512 | store_link_ksettings_for_user(void __user *to, |
| 513 | const struct ethtool_link_ksettings *from) |
| 514 | { |
| 515 | struct ethtool_link_usettings link_usettings; |
| 516 | |
Gustavo A. R. Silva | c1d9e34 | 2021-04-16 15:15:40 -0500 | [diff] [blame] | 517 | memcpy(&link_usettings, from, sizeof(link_usettings)); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 518 | bitmap_to_arr32(link_usettings.link_modes.supported, |
| 519 | from->link_modes.supported, |
| 520 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 521 | bitmap_to_arr32(link_usettings.link_modes.advertising, |
| 522 | from->link_modes.advertising, |
| 523 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 524 | bitmap_to_arr32(link_usettings.link_modes.lp_advertising, |
| 525 | from->link_modes.lp_advertising, |
| 526 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 527 | |
| 528 | if (copy_to_user(to, &link_usettings, sizeof(link_usettings))) |
| 529 | return -EFAULT; |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 534 | /* Query device for its ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 535 | static int ethtool_get_link_ksettings(struct net_device *dev, |
| 536 | void __user *useraddr) |
| 537 | { |
| 538 | int err = 0; |
| 539 | struct ethtool_link_ksettings link_ksettings; |
| 540 | |
| 541 | ASSERT_RTNL(); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 542 | if (!dev->ethtool_ops->get_link_ksettings) |
| 543 | return -EOPNOTSUPP; |
| 544 | |
| 545 | /* handle bitmap nbits handshake */ |
| 546 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 547 | sizeof(link_ksettings.base))) |
| 548 | return -EFAULT; |
| 549 | |
| 550 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 551 | != link_ksettings.base.link_mode_masks_nwords) { |
| 552 | /* wrong link mode nbits requested */ |
| 553 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
Ben Hutchings | 793cf87 | 2016-03-14 01:05:38 +0000 | [diff] [blame] | 554 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 555 | /* send back number of words required as negative val */ |
| 556 | compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX, |
| 557 | "need too many bits for link modes!"); |
| 558 | link_ksettings.base.link_mode_masks_nwords |
| 559 | = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32); |
| 560 | |
| 561 | /* copy the base fields back to user, not the link |
| 562 | * mode bitmaps |
| 563 | */ |
| 564 | if (copy_to_user(useraddr, &link_ksettings.base, |
| 565 | sizeof(link_ksettings.base))) |
| 566 | return -EFAULT; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* handshake successful: user/kernel agree on |
| 572 | * link_mode_masks_nwords |
| 573 | */ |
| 574 | |
| 575 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 576 | err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); |
| 577 | if (err < 0) |
| 578 | return err; |
| 579 | |
| 580 | /* make sure we tell the right values to user */ |
| 581 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
| 582 | link_ksettings.base.link_mode_masks_nwords |
| 583 | = __ETHTOOL_LINK_MODE_MASK_NU32; |
Oleksij Rempel | bdbdac7 | 2020-05-05 08:35:05 +0200 | [diff] [blame] | 584 | link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED; |
| 585 | link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 586 | |
| 587 | return store_link_ksettings_for_user(useraddr, &link_ksettings); |
| 588 | } |
| 589 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 590 | /* Update device ethtool_link_settings. */ |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 591 | static int ethtool_set_link_ksettings(struct net_device *dev, |
| 592 | void __user *useraddr) |
| 593 | { |
| 594 | int err; |
| 595 | struct ethtool_link_ksettings link_ksettings; |
| 596 | |
| 597 | ASSERT_RTNL(); |
| 598 | |
| 599 | if (!dev->ethtool_ops->set_link_ksettings) |
| 600 | return -EOPNOTSUPP; |
| 601 | |
| 602 | /* make sure nbits field has expected value */ |
| 603 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 604 | sizeof(link_ksettings.base))) |
| 605 | return -EFAULT; |
| 606 | |
| 607 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 608 | != link_ksettings.base.link_mode_masks_nwords) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | /* copy the whole structure, now that we know it has expected |
| 612 | * format |
| 613 | */ |
| 614 | err = load_link_ksettings_from_user(&link_ksettings, useraddr); |
| 615 | if (err) |
| 616 | return err; |
| 617 | |
| 618 | /* re-check nwords field, just in case */ |
| 619 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 620 | != link_ksettings.base.link_mode_masks_nwords) |
| 621 | return -EINVAL; |
| 622 | |
Oleksij Rempel | bdbdac7 | 2020-05-05 08:35:05 +0200 | [diff] [blame] | 623 | if (link_ksettings.base.master_slave_cfg || |
| 624 | link_ksettings.base.master_slave_state) |
| 625 | return -EINVAL; |
| 626 | |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 627 | err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); |
Michal Kubecek | 1b1b184 | 2019-12-27 15:56:18 +0100 | [diff] [blame] | 628 | if (err >= 0) { |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 629 | ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL); |
Michal Kubecek | 1b1b184 | 2019-12-27 15:56:18 +0100 | [diff] [blame] | 630 | ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL); |
| 631 | } |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 632 | return err; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Cris Forno | 70ae1e1 | 2020-02-28 14:12:04 -0600 | [diff] [blame] | 635 | int ethtool_virtdev_set_link_ksettings(struct net_device *dev, |
| 636 | const struct ethtool_link_ksettings *cmd, |
| 637 | u32 *dev_speed, u8 *dev_duplex) |
| 638 | { |
| 639 | u32 speed; |
| 640 | u8 duplex; |
| 641 | |
| 642 | speed = cmd->base.speed; |
| 643 | duplex = cmd->base.duplex; |
| 644 | /* don't allow custom speed and duplex */ |
| 645 | if (!ethtool_validate_speed(speed) || |
| 646 | !ethtool_validate_duplex(duplex) || |
| 647 | !ethtool_virtdev_validate_cmd(cmd)) |
| 648 | return -EINVAL; |
| 649 | *dev_speed = speed; |
| 650 | *dev_duplex = duplex; |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings); |
| 655 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 656 | /* Query device for its ethtool_cmd settings. |
| 657 | * |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 658 | * Backward compatibility note: for compatibility with legacy ethtool, this is |
| 659 | * now implemented via get_link_ksettings. When driver reports higher link mode |
| 660 | * bits, a kernel warning is logged once (with name of 1st driver/device) to |
| 661 | * recommend user to upgrade ethtool, but the command is successful (only the |
| 662 | * lower link mode bits reported back to user). Deprecated fields from |
| 663 | * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero. |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 664 | */ |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 665 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) |
| 666 | { |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 667 | struct ethtool_link_ksettings link_ksettings; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 668 | struct ethtool_cmd cmd; |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 669 | int err; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 670 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 671 | ASSERT_RTNL(); |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 672 | if (!dev->ethtool_ops->get_link_ksettings) |
| 673 | return -EOPNOTSUPP; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 674 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 675 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 676 | err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); |
| 677 | if (err < 0) |
| 678 | return err; |
| 679 | convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 680 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 681 | /* send a sensible cmd tag back to user */ |
| 682 | cmd.cmd = ETHTOOL_GSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 685 | return -EFAULT; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 690 | /* Update device link settings with given ethtool_cmd. |
| 691 | * |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 692 | * Backward compatibility note: for compatibility with legacy ethtool, this is |
| 693 | * now always implemented via set_link_settings. When user's request updates |
| 694 | * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel |
| 695 | * warning is logged once (with name of 1st driver/device) to recommend user to |
| 696 | * upgrade ethtool, and the request is rejected. |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 697 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) |
| 699 | { |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 700 | struct ethtool_link_ksettings link_ksettings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | struct ethtool_cmd cmd; |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 702 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 704 | ASSERT_RTNL(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 707 | return -EFAULT; |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 708 | if (!dev->ethtool_ops->set_link_ksettings) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 709 | return -EOPNOTSUPP; |
| 710 | |
Michal Kubecek | 9b30049 | 2018-08-28 19:56:58 +0200 | [diff] [blame] | 711 | if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd)) |
| 712 | return -EINVAL; |
| 713 | link_ksettings.base.link_mode_masks_nwords = |
| 714 | __ETHTOOL_LINK_MODE_MASK_NU32; |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 715 | ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); |
Michal Kubecek | 1b1b184 | 2019-12-27 15:56:18 +0100 | [diff] [blame] | 716 | if (ret >= 0) { |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 717 | ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL); |
Michal Kubecek | 1b1b184 | 2019-12-27 15:56:18 +0100 | [diff] [blame] | 718 | ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL); |
| 719 | } |
Michal Kubecek | 7328673 | 2019-12-27 15:56:03 +0100 | [diff] [blame] | 720 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 723 | static int |
| 724 | ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 726 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 728 | rsp->info.cmd = ETHTOOL_GDRVINFO; |
| 729 | strlcpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version)); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 730 | if (ops->get_drvinfo) { |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 731 | ops->get_drvinfo(dev, &rsp->info); |
Ben Hutchings | 0141480 | 2010-08-17 02:31:15 -0700 | [diff] [blame] | 732 | } else if (dev->dev.parent && dev->dev.parent->driver) { |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 733 | strlcpy(rsp->info.bus_info, dev_name(dev->dev.parent), |
| 734 | sizeof(rsp->info.bus_info)); |
| 735 | strlcpy(rsp->info.driver, dev->dev.parent->driver->name, |
| 736 | sizeof(rsp->info.driver)); |
Ben Hutchings | 0141480 | 2010-08-17 02:31:15 -0700 | [diff] [blame] | 737 | } else { |
| 738 | return -EOPNOTSUPP; |
| 739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 741 | /* |
| 742 | * this method of obtaining string set info is deprecated; |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 743 | * Use ETHTOOL_GSSET_INFO instead. |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 744 | */ |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 745 | if (ops->get_sset_count) { |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 746 | int rc; |
| 747 | |
| 748 | rc = ops->get_sset_count(dev, ETH_SS_TEST); |
| 749 | if (rc >= 0) |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 750 | rsp->info.testinfo_len = rc; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 751 | rc = ops->get_sset_count(dev, ETH_SS_STATS); |
| 752 | if (rc >= 0) |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 753 | rsp->info.n_stats = rc; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 754 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
| 755 | if (rc >= 0) |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 756 | rsp->info.n_priv_flags = rc; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 757 | } |
Yunsheng Lin | f9fc54d | 2018-12-26 19:51:46 +0800 | [diff] [blame] | 758 | if (ops->get_regs_len) { |
| 759 | int ret = ops->get_regs_len(dev); |
| 760 | |
| 761 | if (ret > 0) |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 762 | rsp->info.regdump_len = ret; |
Yunsheng Lin | f9fc54d | 2018-12-26 19:51:46 +0800 | [diff] [blame] | 763 | } |
| 764 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 765 | if (ops->get_eeprom_len) |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 766 | rsp->info.eedump_len = ops->get_eeprom_len(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 768 | if (!rsp->info.fw_version[0]) |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 769 | rsp->devlink = netdev_to_devlink_get(dev); |
| 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Eric Dumazet | f5c445e | 2010-03-08 12:17:04 -0800 | [diff] [blame] | 774 | static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev, |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 775 | void __user *useraddr) |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 776 | { |
| 777 | struct ethtool_sset_info info; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 778 | u64 sset_mask; |
| 779 | int i, idx = 0, n_bits = 0, ret, rc; |
| 780 | u32 *info_buf = NULL; |
| 781 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 782 | if (copy_from_user(&info, useraddr, sizeof(info))) |
| 783 | return -EFAULT; |
| 784 | |
| 785 | /* store copy of mask, because we zero struct later on */ |
| 786 | sset_mask = info.sset_mask; |
| 787 | if (!sset_mask) |
| 788 | return 0; |
| 789 | |
| 790 | /* calculate size of return buffer */ |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 791 | n_bits = hweight64(sset_mask); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 792 | |
| 793 | memset(&info, 0, sizeof(info)); |
| 794 | info.cmd = ETHTOOL_GSSET_INFO; |
| 795 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 796 | info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 797 | if (!info_buf) |
| 798 | return -ENOMEM; |
| 799 | |
| 800 | /* |
| 801 | * fill return buffer based on input bitmask and successful |
| 802 | * get_sset_count return |
| 803 | */ |
| 804 | for (i = 0; i < 64; i++) { |
| 805 | if (!(sset_mask & (1ULL << i))) |
| 806 | continue; |
| 807 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 808 | rc = __ethtool_get_sset_count(dev, i); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 809 | if (rc >= 0) { |
| 810 | info.sset_mask |= (1ULL << i); |
| 811 | info_buf[idx++] = rc; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | ret = -EFAULT; |
| 816 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 817 | goto out; |
| 818 | |
| 819 | useraddr += offsetof(struct ethtool_sset_info, data); |
Gustavo A. R. Silva | ed71761 | 2021-09-28 14:57:35 -0500 | [diff] [blame] | 820 | if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32)))) |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 821 | goto out; |
| 822 | |
| 823 | ret = 0; |
| 824 | |
| 825 | out: |
| 826 | kfree(info_buf); |
| 827 | return ret; |
| 828 | } |
| 829 | |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 830 | static noinline_for_stack int |
| 831 | ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc, |
| 832 | const struct compat_ethtool_rxnfc __user *useraddr, |
| 833 | size_t size) |
| 834 | { |
| 835 | struct compat_ethtool_rxnfc crxnfc = {}; |
| 836 | |
| 837 | /* We expect there to be holes between fs.m_ext and |
| 838 | * fs.ring_cookie and at the end of fs, but nowhere else. |
| 839 | * On non-x86, no conversion should be needed. |
| 840 | */ |
| 841 | BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) && |
| 842 | sizeof(struct compat_ethtool_rxnfc) != |
| 843 | sizeof(struct ethtool_rxnfc)); |
| 844 | BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) + |
| 845 | sizeof(useraddr->fs.m_ext) != |
| 846 | offsetof(struct ethtool_rxnfc, fs.m_ext) + |
| 847 | sizeof(rxnfc->fs.m_ext)); |
| 848 | BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) - |
| 849 | offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) != |
| 850 | offsetof(struct ethtool_rxnfc, fs.location) - |
| 851 | offsetof(struct ethtool_rxnfc, fs.ring_cookie)); |
| 852 | |
| 853 | if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc)))) |
| 854 | return -EFAULT; |
| 855 | |
| 856 | *rxnfc = (struct ethtool_rxnfc) { |
| 857 | .cmd = crxnfc.cmd, |
| 858 | .flow_type = crxnfc.flow_type, |
| 859 | .data = crxnfc.data, |
| 860 | .fs = { |
| 861 | .flow_type = crxnfc.fs.flow_type, |
| 862 | .h_u = crxnfc.fs.h_u, |
| 863 | .h_ext = crxnfc.fs.h_ext, |
| 864 | .m_u = crxnfc.fs.m_u, |
| 865 | .m_ext = crxnfc.fs.m_ext, |
| 866 | .ring_cookie = crxnfc.fs.ring_cookie, |
| 867 | .location = crxnfc.fs.location, |
| 868 | }, |
| 869 | .rule_cnt = crxnfc.rule_cnt, |
| 870 | }; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc, |
| 876 | const void __user *useraddr, |
| 877 | size_t size) |
| 878 | { |
| 879 | if (compat_need_64bit_alignment_fixup()) |
| 880 | return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size); |
| 881 | |
| 882 | if (copy_from_user(rxnfc, useraddr, size)) |
| 883 | return -EFAULT; |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | static int ethtool_rxnfc_copy_to_compat(void __user *useraddr, |
| 889 | const struct ethtool_rxnfc *rxnfc, |
| 890 | size_t size, const u32 *rule_buf) |
| 891 | { |
| 892 | struct compat_ethtool_rxnfc crxnfc; |
| 893 | |
| 894 | memset(&crxnfc, 0, sizeof(crxnfc)); |
| 895 | crxnfc = (struct compat_ethtool_rxnfc) { |
| 896 | .cmd = rxnfc->cmd, |
| 897 | .flow_type = rxnfc->flow_type, |
| 898 | .data = rxnfc->data, |
| 899 | .fs = { |
| 900 | .flow_type = rxnfc->fs.flow_type, |
| 901 | .h_u = rxnfc->fs.h_u, |
| 902 | .h_ext = rxnfc->fs.h_ext, |
| 903 | .m_u = rxnfc->fs.m_u, |
| 904 | .m_ext = rxnfc->fs.m_ext, |
| 905 | .ring_cookie = rxnfc->fs.ring_cookie, |
| 906 | .location = rxnfc->fs.location, |
| 907 | }, |
| 908 | .rule_cnt = rxnfc->rule_cnt, |
| 909 | }; |
| 910 | |
| 911 | if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc)))) |
| 912 | return -EFAULT; |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | static int ethtool_rxnfc_copy_to_user(void __user *useraddr, |
| 918 | const struct ethtool_rxnfc *rxnfc, |
| 919 | size_t size, const u32 *rule_buf) |
| 920 | { |
| 921 | int ret; |
| 922 | |
| 923 | if (compat_need_64bit_alignment_fixup()) { |
| 924 | ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size, |
| 925 | rule_buf); |
| 926 | useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs); |
| 927 | } else { |
Saeed Mahameed | 9b29a16 | 2021-07-26 15:15:39 -0700 | [diff] [blame] | 928 | ret = copy_to_user(useraddr, rxnfc, size); |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 929 | useraddr += offsetof(struct ethtool_rxnfc, rule_locs); |
| 930 | } |
| 931 | |
| 932 | if (ret) |
| 933 | return -EFAULT; |
| 934 | |
| 935 | if (rule_buf) { |
| 936 | if (copy_to_user(useraddr, rule_buf, |
| 937 | rxnfc->rule_cnt * sizeof(u32))) |
| 938 | return -EFAULT; |
| 939 | } |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 944 | static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 945 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 946 | { |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 947 | struct ethtool_rxnfc info; |
| 948 | size_t info_size = sizeof(info); |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 949 | int rc; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 950 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 951 | if (!dev->ethtool_ops->set_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 952 | return -EOPNOTSUPP; |
| 953 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 954 | /* struct ethtool_rxnfc was originally defined for |
| 955 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 956 | * members. User-space might still be using that |
| 957 | * definition. */ |
| 958 | if (cmd == ETHTOOL_SRXFH) |
| 959 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 960 | sizeof(info.data)); |
| 961 | |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 962 | if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 963 | return -EFAULT; |
| 964 | |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 965 | rc = dev->ethtool_ops->set_rxnfc(dev, &info); |
| 966 | if (rc) |
| 967 | return rc; |
| 968 | |
| 969 | if (cmd == ETHTOOL_SRXCLSRLINS && |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 970 | ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL)) |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 971 | return -EFAULT; |
| 972 | |
| 973 | return 0; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 974 | } |
| 975 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 976 | static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 977 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 978 | { |
| 979 | struct ethtool_rxnfc info; |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 980 | size_t info_size = sizeof(info); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 981 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 982 | int ret; |
| 983 | void *rule_buf = NULL; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 984 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 985 | if (!ops->get_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 986 | return -EOPNOTSUPP; |
| 987 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 988 | /* struct ethtool_rxnfc was originally defined for |
| 989 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 990 | * members. User-space might still be using that |
| 991 | * definition. */ |
| 992 | if (cmd == ETHTOOL_GRXFH) |
| 993 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 994 | sizeof(info.data)); |
| 995 | |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 996 | if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 997 | return -EFAULT; |
| 998 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 999 | /* If FLOW_RSS was requested then user-space must be using the |
| 1000 | * new definition, as FLOW_RSS is newer. |
| 1001 | */ |
| 1002 | if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) { |
| 1003 | info_size = sizeof(info); |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 1004 | if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size)) |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1005 | return -EFAULT; |
Wenwen Wang | d656fe4 | 2018-04-30 12:31:13 -0500 | [diff] [blame] | 1006 | /* Since malicious users may modify the original data, |
| 1007 | * we need to check whether FLOW_RSS is still requested. |
| 1008 | */ |
| 1009 | if (!(info.flow_type & FLOW_RSS)) |
| 1010 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Wenwen Wang | 2bb3207 | 2018-10-09 08:15:38 -0500 | [diff] [blame] | 1013 | if (info.cmd != cmd) |
| 1014 | return -EINVAL; |
| 1015 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1016 | if (info.cmd == ETHTOOL_GRXCLSRLALL) { |
| 1017 | if (info.rule_cnt > 0) { |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 1018 | if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1019 | rule_buf = kcalloc(info.rule_cnt, sizeof(u32), |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 1020 | GFP_USER); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1021 | if (!rule_buf) |
| 1022 | return -ENOMEM; |
| 1023 | } |
| 1024 | } |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1025 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1026 | ret = ops->get_rxnfc(dev, &info, rule_buf); |
| 1027 | if (ret < 0) |
| 1028 | goto err_out; |
| 1029 | |
Arnd Bergmann | dd98d28 | 2021-07-22 16:28:59 +0200 | [diff] [blame] | 1030 | ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1031 | err_out: |
Wei Yongjun | c9cacec | 2009-03-31 15:06:26 -0700 | [diff] [blame] | 1032 | kfree(rule_buf); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1033 | |
| 1034 | return ret; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1037 | static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, |
| 1038 | struct ethtool_rxnfc *rx_rings, |
| 1039 | u32 size) |
| 1040 | { |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1041 | int i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1042 | |
Gustavo A. R. Silva | ed71761 | 2021-09-28 14:57:35 -0500 | [diff] [blame] | 1043 | if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0])))) |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1044 | return -EFAULT; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1045 | |
| 1046 | /* Validate ring indices */ |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1047 | for (i = 0; i < size; i++) |
| 1048 | if (indir[i] >= rx_rings->data) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | return 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1052 | } |
| 1053 | |
Kim Jones | ba905f5 | 2016-02-02 03:51:16 +0000 | [diff] [blame] | 1054 | u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly; |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 1055 | |
| 1056 | void netdev_rss_key_fill(void *buffer, size_t len) |
| 1057 | { |
| 1058 | BUG_ON(len > sizeof(netdev_rss_key)); |
| 1059 | net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key)); |
| 1060 | memcpy(buffer, netdev_rss_key, len); |
| 1061 | } |
| 1062 | EXPORT_SYMBOL(netdev_rss_key_fill); |
| 1063 | |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1064 | static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, |
| 1065 | void __user *useraddr) |
| 1066 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1067 | u32 user_size, dev_size; |
| 1068 | u32 *indir; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1069 | int ret; |
| 1070 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1071 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1072 | !dev->ethtool_ops->get_rxfh) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1073 | return -EOPNOTSUPP; |
| 1074 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); |
| 1075 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1076 | return -EOPNOTSUPP; |
| 1077 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1078 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1079 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1080 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1081 | return -EFAULT; |
| 1082 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1083 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), |
| 1084 | &dev_size, sizeof(dev_size))) |
| 1085 | return -EFAULT; |
| 1086 | |
| 1087 | /* If the user buffer size is 0, this is just a query for the |
| 1088 | * device table size. Otherwise, if it's smaller than the |
| 1089 | * device table size it's an error. |
| 1090 | */ |
| 1091 | if (user_size < dev_size) |
| 1092 | return user_size == 0 ? 0 : -EINVAL; |
| 1093 | |
| 1094 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1095 | if (!indir) |
| 1096 | return -ENOMEM; |
| 1097 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1098 | ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1099 | if (ret) |
| 1100 | goto out; |
| 1101 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1102 | if (copy_to_user(useraddr + |
| 1103 | offsetof(struct ethtool_rxfh_indir, ring_index[0]), |
| 1104 | indir, dev_size * sizeof(indir[0]))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1105 | ret = -EFAULT; |
| 1106 | |
| 1107 | out: |
| 1108 | kfree(indir); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
| 1112 | static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, |
| 1113 | void __user *useraddr) |
| 1114 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1115 | struct ethtool_rxnfc rx_rings; |
| 1116 | u32 user_size, dev_size, i; |
| 1117 | u32 *indir; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1118 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1119 | int ret; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1120 | u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1121 | |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1122 | if (!ops->get_rxfh_indir_size || !ops->set_rxfh || |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1123 | !ops->get_rxnfc) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1124 | return -EOPNOTSUPP; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1125 | |
| 1126 | dev_size = ops->get_rxfh_indir_size(dev); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1127 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1128 | return -EOPNOTSUPP; |
| 1129 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1130 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1131 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1132 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1133 | return -EFAULT; |
| 1134 | |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1135 | if (user_size != 0 && user_size != dev_size) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1136 | return -EINVAL; |
| 1137 | |
| 1138 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1139 | if (!indir) |
| 1140 | return -ENOMEM; |
| 1141 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1142 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1143 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1144 | if (ret) |
| 1145 | goto out; |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1146 | |
| 1147 | if (user_size == 0) { |
| 1148 | for (i = 0; i < dev_size; i++) |
| 1149 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1150 | } else { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1151 | ret = ethtool_copy_validate_indir(indir, |
| 1152 | useraddr + ringidx_offset, |
| 1153 | &rx_rings, |
| 1154 | dev_size); |
| 1155 | if (ret) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1156 | goto out; |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1159 | ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1160 | if (ret) |
| 1161 | goto out; |
| 1162 | |
| 1163 | /* indicate whether rxfh was set to default */ |
| 1164 | if (user_size == 0) |
| 1165 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1166 | else |
| 1167 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1168 | |
| 1169 | out: |
| 1170 | kfree(indir); |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1174 | static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, |
| 1175 | void __user *useraddr) |
| 1176 | { |
| 1177 | int ret; |
| 1178 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1179 | u32 user_indir_size, user_key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1180 | u32 dev_indir_size = 0, dev_key_size = 0; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1181 | struct ethtool_rxfh rxfh; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1182 | u32 total_size; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1183 | u32 indir_bytes; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1184 | u32 *indir = NULL; |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1185 | u8 dev_hfunc = 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1186 | u8 *hkey = NULL; |
| 1187 | u8 *rss_config; |
| 1188 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1189 | if (!ops->get_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1190 | return -EOPNOTSUPP; |
| 1191 | |
| 1192 | if (ops->get_rxfh_indir_size) |
| 1193 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1194 | if (ops->get_rxfh_key_size) |
| 1195 | dev_key_size = ops->get_rxfh_key_size(dev); |
| 1196 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1197 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1198 | return -EFAULT; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1199 | user_indir_size = rxfh.indir_size; |
| 1200 | user_key_size = rxfh.key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1201 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1202 | /* Check that reserved fields are 0 for now */ |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1203 | if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1204 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1205 | /* Most drivers don't handle rss_context, check it's 0 as well */ |
| 1206 | if (rxfh.rss_context && !ops->get_rxfh_context) |
| 1207 | return -EOPNOTSUPP; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1208 | |
| 1209 | rxfh.indir_size = dev_indir_size; |
| 1210 | rxfh.key_size = dev_key_size; |
| 1211 | if (copy_to_user(useraddr, &rxfh, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1212 | return -EFAULT; |
| 1213 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1214 | if ((user_indir_size && (user_indir_size != dev_indir_size)) || |
| 1215 | (user_key_size && (user_key_size != dev_key_size))) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | indir_bytes = user_indir_size * sizeof(indir[0]); |
| 1219 | total_size = indir_bytes + user_key_size; |
| 1220 | rss_config = kzalloc(total_size, GFP_USER); |
| 1221 | if (!rss_config) |
| 1222 | return -ENOMEM; |
| 1223 | |
| 1224 | if (user_indir_size) |
| 1225 | indir = (u32 *)rss_config; |
| 1226 | |
| 1227 | if (user_key_size) |
| 1228 | hkey = rss_config + indir_bytes; |
| 1229 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1230 | if (rxfh.rss_context) |
| 1231 | ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey, |
| 1232 | &dev_hfunc, |
| 1233 | rxfh.rss_context); |
| 1234 | else |
| 1235 | ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc); |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1236 | if (ret) |
| 1237 | goto out; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1238 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1239 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc), |
| 1240 | &dev_hfunc, sizeof(rxfh.hfunc))) { |
| 1241 | ret = -EFAULT; |
| 1242 | } else if (copy_to_user(useraddr + |
| 1243 | offsetof(struct ethtool_rxfh, rss_config[0]), |
| 1244 | rss_config, total_size)) { |
| 1245 | ret = -EFAULT; |
| 1246 | } |
| 1247 | out: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1248 | kfree(rss_config); |
| 1249 | |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, |
| 1254 | void __user *useraddr) |
| 1255 | { |
| 1256 | int ret; |
| 1257 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1258 | struct ethtool_rxnfc rx_rings; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1259 | struct ethtool_rxfh rxfh; |
| 1260 | u32 dev_indir_size = 0, dev_key_size = 0, i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1261 | u32 *indir = NULL, indir_bytes = 0; |
| 1262 | u8 *hkey = NULL; |
| 1263 | u8 *rss_config; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1264 | u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1265 | bool delete = false; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1266 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1267 | if (!ops->get_rxnfc || !ops->set_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1268 | return -EOPNOTSUPP; |
| 1269 | |
| 1270 | if (ops->get_rxfh_indir_size) |
| 1271 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1272 | if (ops->get_rxfh_key_size) |
Dan Carpenter | d340c86 | 2015-02-20 13:54:05 +0300 | [diff] [blame] | 1273 | dev_key_size = ops->get_rxfh_key_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1274 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1275 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1276 | return -EFAULT; |
| 1277 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1278 | /* Check that reserved fields are 0 for now */ |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1279 | if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1280 | return -EINVAL; |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1281 | /* Most drivers don't handle rss_context, check it's 0 as well */ |
| 1282 | if (rxfh.rss_context && !ops->set_rxfh_context) |
| 1283 | return -EOPNOTSUPP; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1284 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1285 | /* If either indir, hash key or function is valid, proceed further. |
| 1286 | * Must request at least one change: indir size, hash key or function. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1287 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1288 | if ((rxfh.indir_size && |
| 1289 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && |
| 1290 | rxfh.indir_size != dev_indir_size) || |
| 1291 | (rxfh.key_size && (rxfh.key_size != dev_key_size)) || |
| 1292 | (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1293 | rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE)) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1294 | return -EINVAL; |
| 1295 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1296 | if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1297 | indir_bytes = dev_indir_size * sizeof(indir[0]); |
| 1298 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1299 | rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1300 | if (!rss_config) |
| 1301 | return -ENOMEM; |
| 1302 | |
| 1303 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
| 1304 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
| 1305 | if (ret) |
| 1306 | goto out; |
| 1307 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1308 | /* rxfh.indir_size == 0 means reset the indir table to default (master |
| 1309 | * context) or delete the context (other RSS contexts). |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1310 | * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1311 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1312 | if (rxfh.indir_size && |
| 1313 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1314 | indir = (u32 *)rss_config; |
| 1315 | ret = ethtool_copy_validate_indir(indir, |
| 1316 | useraddr + rss_cfg_offset, |
| 1317 | &rx_rings, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1318 | rxfh.indir_size); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1319 | if (ret) |
| 1320 | goto out; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1321 | } else if (rxfh.indir_size == 0) { |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1322 | if (rxfh.rss_context == 0) { |
| 1323 | indir = (u32 *)rss_config; |
| 1324 | for (i = 0; i < dev_indir_size; i++) |
| 1325 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1326 | } else { |
| 1327 | delete = true; |
| 1328 | } |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1329 | } |
| 1330 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1331 | if (rxfh.key_size) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1332 | hkey = rss_config + indir_bytes; |
| 1333 | if (copy_from_user(hkey, |
| 1334 | useraddr + rss_cfg_offset + indir_bytes, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1335 | rxfh.key_size)) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1336 | ret = -EFAULT; |
| 1337 | goto out; |
| 1338 | } |
| 1339 | } |
| 1340 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1341 | if (rxfh.rss_context) |
| 1342 | ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc, |
| 1343 | &rxfh.rss_context, delete); |
| 1344 | else |
| 1345 | ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1346 | if (ret) |
| 1347 | goto out; |
| 1348 | |
Edward Cree | 84a1d9c4 | 2018-03-08 15:45:03 +0000 | [diff] [blame] | 1349 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context), |
| 1350 | &rxfh.rss_context, sizeof(rxfh.rss_context))) |
| 1351 | ret = -EFAULT; |
| 1352 | |
| 1353 | if (!rxfh.rss_context) { |
| 1354 | /* indicate whether rxfh was set to default */ |
| 1355 | if (rxfh.indir_size == 0) |
| 1356 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1357 | else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
| 1358 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
| 1359 | } |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1360 | |
| 1361 | out: |
| 1362 | kfree(rss_config); |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
| 1367 | { |
| 1368 | struct ethtool_regs regs; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1369 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | void *regbuf; |
| 1371 | int reglen, ret; |
| 1372 | |
| 1373 | if (!ops->get_regs || !ops->get_regs_len) |
| 1374 | return -EOPNOTSUPP; |
| 1375 | |
| 1376 | if (copy_from_user(®s, useraddr, sizeof(regs))) |
| 1377 | return -EFAULT; |
| 1378 | |
| 1379 | reglen = ops->get_regs_len(dev); |
Yunsheng Lin | f9fc54d | 2018-12-26 19:51:46 +0800 | [diff] [blame] | 1380 | if (reglen <= 0) |
| 1381 | return reglen; |
| 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | if (regs.len > reglen) |
| 1384 | regs.len = reglen; |
| 1385 | |
Dan Carpenter | ef76c77 | 2019-02-01 11:24:06 +0300 | [diff] [blame] | 1386 | regbuf = vzalloc(reglen); |
| 1387 | if (!regbuf) |
| 1388 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
Vivien Didelot | 0ee4e76 | 2019-06-03 16:57:13 -0400 | [diff] [blame] | 1390 | if (regs.len < reglen) |
| 1391 | reglen = regs.len; |
| 1392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | ops->get_regs(dev, ®s, regbuf); |
| 1394 | |
| 1395 | ret = -EFAULT; |
| 1396 | if (copy_to_user(useraddr, ®s, sizeof(regs))) |
| 1397 | goto out; |
| 1398 | useraddr += offsetof(struct ethtool_regs, data); |
Vivien Didelot | 0ee4e76 | 2019-06-03 16:57:13 -0400 | [diff] [blame] | 1399 | if (copy_to_user(useraddr, regbuf, reglen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | goto out; |
| 1401 | ret = 0; |
| 1402 | |
| 1403 | out: |
Ben Hutchings | a77f5db | 2010-09-20 08:42:17 +0000 | [diff] [blame] | 1404 | vfree(regbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | return ret; |
| 1406 | } |
| 1407 | |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 1408 | static int ethtool_reset(struct net_device *dev, char __user *useraddr) |
| 1409 | { |
| 1410 | struct ethtool_value reset; |
| 1411 | int ret; |
| 1412 | |
| 1413 | if (!dev->ethtool_ops->reset) |
| 1414 | return -EOPNOTSUPP; |
| 1415 | |
| 1416 | if (copy_from_user(&reset, useraddr, sizeof(reset))) |
| 1417 | return -EFAULT; |
| 1418 | |
| 1419 | ret = dev->ethtool_ops->reset(dev, &reset.data); |
| 1420 | if (ret) |
| 1421 | return ret; |
| 1422 | |
| 1423 | if (copy_to_user(useraddr, &reset, sizeof(reset))) |
| 1424 | return -EFAULT; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
| 1429 | { |
zhanglin | 5ff223e8 | 2019-10-26 15:54:16 +0800 | [diff] [blame] | 1430 | struct ethtool_wolinfo wol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
| 1432 | if (!dev->ethtool_ops->get_wol) |
| 1433 | return -EOPNOTSUPP; |
| 1434 | |
zhanglin | 5ff223e8 | 2019-10-26 15:54:16 +0800 | [diff] [blame] | 1435 | memset(&wol, 0, sizeof(struct ethtool_wolinfo)); |
| 1436 | wol.cmd = ETHTOOL_GWOL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | dev->ethtool_ops->get_wol(dev, &wol); |
| 1438 | |
| 1439 | if (copy_to_user(useraddr, &wol, sizeof(wol))) |
| 1440 | return -EFAULT; |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) |
| 1445 | { |
| 1446 | struct ethtool_wolinfo wol; |
Heiner Kallweit | 6194114 | 2018-09-24 21:58:59 +0200 | [diff] [blame] | 1447 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
| 1449 | if (!dev->ethtool_ops->set_wol) |
| 1450 | return -EOPNOTSUPP; |
| 1451 | |
| 1452 | if (copy_from_user(&wol, useraddr, sizeof(wol))) |
| 1453 | return -EFAULT; |
| 1454 | |
Heiner Kallweit | 6194114 | 2018-09-24 21:58:59 +0200 | [diff] [blame] | 1455 | ret = dev->ethtool_ops->set_wol(dev, &wol); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | |
| 1459 | dev->wol_enabled = !!wol.wolopts; |
Michal Kubecek | 67bffa7 | 2020-01-26 23:11:19 +0100 | [diff] [blame] | 1460 | ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL); |
Heiner Kallweit | 6194114 | 2018-09-24 21:58:59 +0200 | [diff] [blame] | 1461 | |
| 1462 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 1465 | static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) |
| 1466 | { |
| 1467 | struct ethtool_eee edata; |
| 1468 | int rc; |
| 1469 | |
| 1470 | if (!dev->ethtool_ops->get_eee) |
| 1471 | return -EOPNOTSUPP; |
| 1472 | |
| 1473 | memset(&edata, 0, sizeof(struct ethtool_eee)); |
| 1474 | edata.cmd = ETHTOOL_GEEE; |
| 1475 | rc = dev->ethtool_ops->get_eee(dev, &edata); |
| 1476 | |
| 1477 | if (rc) |
| 1478 | return rc; |
| 1479 | |
| 1480 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1481 | return -EFAULT; |
| 1482 | |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) |
| 1487 | { |
| 1488 | struct ethtool_eee edata; |
Michal Kubecek | 6c5bc8f | 2020-03-28 00:01:48 +0100 | [diff] [blame] | 1489 | int ret; |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 1490 | |
| 1491 | if (!dev->ethtool_ops->set_eee) |
| 1492 | return -EOPNOTSUPP; |
| 1493 | |
| 1494 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 1495 | return -EFAULT; |
| 1496 | |
Michal Kubecek | 6c5bc8f | 2020-03-28 00:01:48 +0100 | [diff] [blame] | 1497 | ret = dev->ethtool_ops->set_eee(dev, &edata); |
| 1498 | if (!ret) |
| 1499 | ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL); |
| 1500 | return ret; |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | static int ethtool_nway_reset(struct net_device *dev) |
| 1504 | { |
| 1505 | if (!dev->ethtool_ops->nway_reset) |
| 1506 | return -EOPNOTSUPP; |
| 1507 | |
| 1508 | return dev->ethtool_ops->nway_reset(dev); |
| 1509 | } |
| 1510 | |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1511 | static int ethtool_get_link(struct net_device *dev, char __user *useraddr) |
| 1512 | { |
| 1513 | struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 1514 | int link = __ethtool_get_link(dev); |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1515 | |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 1516 | if (link < 0) |
| 1517 | return link; |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1518 | |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 1519 | edata.data = link; |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1520 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1521 | return -EFAULT; |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1525 | static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, |
| 1526 | int (*getter)(struct net_device *, |
| 1527 | struct ethtool_eeprom *, u8 *), |
| 1528 | u32 total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | { |
| 1530 | struct ethtool_eeprom eeprom; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1531 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1532 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1534 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1537 | return -EFAULT; |
| 1538 | |
| 1539 | /* Check for wrap and zero */ |
| 1540 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1541 | return -EINVAL; |
| 1542 | |
| 1543 | /* Check for exceeding total eeprom len */ |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1544 | if (eeprom.offset + eeprom.len > total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | return -EINVAL; |
| 1546 | |
Austin Kim | 80ec82e | 2021-06-09 03:34:25 +0100 | [diff] [blame] | 1547 | data = kzalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | if (!data) |
| 1549 | return -ENOMEM; |
| 1550 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1551 | bytes_remaining = eeprom.len; |
| 1552 | while (bytes_remaining > 0) { |
| 1553 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1555 | ret = getter(dev, &eeprom, data); |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1556 | if (ret) |
| 1557 | break; |
Heiner Kallweit | b9bbc4c | 2021-09-13 21:58:26 +0200 | [diff] [blame] | 1558 | if (!eeprom.len) { |
| 1559 | ret = -EIO; |
| 1560 | break; |
| 1561 | } |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1562 | if (copy_to_user(userbuf, data, eeprom.len)) { |
| 1563 | ret = -EFAULT; |
| 1564 | break; |
| 1565 | } |
| 1566 | userbuf += eeprom.len; |
| 1567 | eeprom.offset += eeprom.len; |
| 1568 | bytes_remaining -= eeprom.len; |
| 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
Mandeep Singh Baines | c5835df | 2008-04-24 20:55:56 -0700 | [diff] [blame] | 1571 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
| 1572 | eeprom.offset -= eeprom.len; |
| 1573 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) |
| 1574 | ret = -EFAULT; |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | kfree(data); |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1580 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
| 1581 | { |
| 1582 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1583 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1584 | if (!ops->get_eeprom || !ops->get_eeprom_len || |
| 1585 | !ops->get_eeprom_len(dev)) |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1586 | return -EOPNOTSUPP; |
| 1587 | |
| 1588 | return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, |
| 1589 | ops->get_eeprom_len(dev)); |
| 1590 | } |
| 1591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) |
| 1593 | { |
| 1594 | struct ethtool_eeprom eeprom; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1595 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1596 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1597 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1599 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1601 | if (!ops->set_eeprom || !ops->get_eeprom_len || |
| 1602 | !ops->get_eeprom_len(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return -EOPNOTSUPP; |
| 1604 | |
| 1605 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1606 | return -EFAULT; |
| 1607 | |
| 1608 | /* Check for wrap and zero */ |
| 1609 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1610 | return -EINVAL; |
| 1611 | |
| 1612 | /* Check for exceeding total eeprom len */ |
| 1613 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) |
| 1614 | return -EINVAL; |
| 1615 | |
Austin Kim | 80ec82e | 2021-06-09 03:34:25 +0100 | [diff] [blame] | 1616 | data = kzalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | if (!data) |
| 1618 | return -ENOMEM; |
| 1619 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1620 | bytes_remaining = eeprom.len; |
| 1621 | while (bytes_remaining > 0) { |
| 1622 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1624 | if (copy_from_user(data, userbuf, eeprom.len)) { |
| 1625 | ret = -EFAULT; |
| 1626 | break; |
| 1627 | } |
| 1628 | ret = ops->set_eeprom(dev, &eeprom, data); |
| 1629 | if (ret) |
| 1630 | break; |
| 1631 | userbuf += eeprom.len; |
| 1632 | eeprom.offset += eeprom.len; |
| 1633 | bytes_remaining -= eeprom.len; |
| 1634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | kfree(data); |
| 1637 | return ret; |
| 1638 | } |
| 1639 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1640 | static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, |
| 1641 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1643 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1644 | struct kernel_ethtool_coalesce kernel_coalesce = {}; |
Heiner Kallweit | 3161071 | 2020-05-23 17:40:25 +0200 | [diff] [blame] | 1645 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
| 1647 | if (!dev->ethtool_ops->get_coalesce) |
| 1648 | return -EOPNOTSUPP; |
| 1649 | |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1650 | ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, |
| 1651 | NULL); |
Heiner Kallweit | 3161071 | 2020-05-23 17:40:25 +0200 | [diff] [blame] | 1652 | if (ret) |
| 1653 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
| 1655 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 1656 | return -EFAULT; |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
Jakub Kicinski | 95cddcb | 2020-03-04 21:15:31 -0800 | [diff] [blame] | 1660 | static bool |
| 1661 | ethtool_set_coalesce_supported(struct net_device *dev, |
| 1662 | struct ethtool_coalesce *coalesce) |
| 1663 | { |
| 1664 | u32 supported_params = dev->ethtool_ops->supported_coalesce_params; |
| 1665 | u32 nonzero_params = 0; |
| 1666 | |
Jakub Kicinski | 95cddcb | 2020-03-04 21:15:31 -0800 | [diff] [blame] | 1667 | if (coalesce->rx_coalesce_usecs) |
| 1668 | nonzero_params |= ETHTOOL_COALESCE_RX_USECS; |
| 1669 | if (coalesce->rx_max_coalesced_frames) |
| 1670 | nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES; |
| 1671 | if (coalesce->rx_coalesce_usecs_irq) |
| 1672 | nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ; |
| 1673 | if (coalesce->rx_max_coalesced_frames_irq) |
| 1674 | nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ; |
| 1675 | if (coalesce->tx_coalesce_usecs) |
| 1676 | nonzero_params |= ETHTOOL_COALESCE_TX_USECS; |
| 1677 | if (coalesce->tx_max_coalesced_frames) |
| 1678 | nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES; |
| 1679 | if (coalesce->tx_coalesce_usecs_irq) |
| 1680 | nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ; |
| 1681 | if (coalesce->tx_max_coalesced_frames_irq) |
| 1682 | nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ; |
| 1683 | if (coalesce->stats_block_coalesce_usecs) |
| 1684 | nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS; |
| 1685 | if (coalesce->use_adaptive_rx_coalesce) |
| 1686 | nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX; |
| 1687 | if (coalesce->use_adaptive_tx_coalesce) |
| 1688 | nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX; |
| 1689 | if (coalesce->pkt_rate_low) |
| 1690 | nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW; |
| 1691 | if (coalesce->rx_coalesce_usecs_low) |
| 1692 | nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW; |
| 1693 | if (coalesce->rx_max_coalesced_frames_low) |
| 1694 | nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW; |
| 1695 | if (coalesce->tx_coalesce_usecs_low) |
| 1696 | nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW; |
| 1697 | if (coalesce->tx_max_coalesced_frames_low) |
| 1698 | nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW; |
| 1699 | if (coalesce->pkt_rate_high) |
| 1700 | nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH; |
| 1701 | if (coalesce->rx_coalesce_usecs_high) |
| 1702 | nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH; |
| 1703 | if (coalesce->rx_max_coalesced_frames_high) |
| 1704 | nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH; |
| 1705 | if (coalesce->tx_coalesce_usecs_high) |
| 1706 | nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH; |
| 1707 | if (coalesce->tx_max_coalesced_frames_high) |
| 1708 | nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH; |
| 1709 | if (coalesce->rate_sample_interval) |
| 1710 | nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL; |
| 1711 | |
| 1712 | return (supported_params & nonzero_params) == nonzero_params; |
| 1713 | } |
| 1714 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1715 | static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, |
| 1716 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1718 | struct kernel_ethtool_coalesce kernel_coalesce = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | struct ethtool_coalesce coalesce; |
Michal Kubecek | 0cf3eac | 2020-03-28 00:01:18 +0100 | [diff] [blame] | 1720 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1722 | if (!dev->ethtool_ops->set_coalesce && !dev->ethtool_ops->get_coalesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return -EOPNOTSUPP; |
| 1724 | |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1725 | ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, |
| 1726 | NULL); |
| 1727 | if (ret) |
| 1728 | return ret; |
| 1729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) |
| 1731 | return -EFAULT; |
| 1732 | |
Jakub Kicinski | 95cddcb | 2020-03-04 21:15:31 -0800 | [diff] [blame] | 1733 | if (!ethtool_set_coalesce_supported(dev, &coalesce)) |
| 1734 | return -EOPNOTSUPP; |
| 1735 | |
Yufeng Mo | f3ccfda1 | 2021-08-20 15:35:18 +0800 | [diff] [blame] | 1736 | ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce, |
| 1737 | NULL); |
Michal Kubecek | 0cf3eac | 2020-03-28 00:01:18 +0100 | [diff] [blame] | 1738 | if (!ret) |
| 1739 | ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL); |
| 1740 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) |
| 1744 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1745 | struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | if (!dev->ethtool_ops->get_ringparam) |
| 1748 | return -EOPNOTSUPP; |
| 1749 | |
| 1750 | dev->ethtool_ops->get_ringparam(dev, &ringparam); |
| 1751 | |
| 1752 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) |
| 1753 | return -EFAULT; |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) |
| 1758 | { |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1759 | struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM }; |
Michal Kubecek | bc9d1c9 | 2020-03-12 21:08:33 +0100 | [diff] [blame] | 1760 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1762 | if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | return -EOPNOTSUPP; |
| 1764 | |
| 1765 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) |
| 1766 | return -EFAULT; |
| 1767 | |
Eugenia Emantayev | 37e2d99 | 2018-01-08 16:00:24 +0200 | [diff] [blame] | 1768 | dev->ethtool_ops->get_ringparam(dev, &max); |
| 1769 | |
| 1770 | /* ensure new ring parameters are within the maximums */ |
| 1771 | if (ringparam.rx_pending > max.rx_max_pending || |
| 1772 | ringparam.rx_mini_pending > max.rx_mini_max_pending || |
| 1773 | ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || |
| 1774 | ringparam.tx_pending > max.tx_max_pending) |
| 1775 | return -EINVAL; |
| 1776 | |
Michal Kubecek | bc9d1c9 | 2020-03-12 21:08:33 +0100 | [diff] [blame] | 1777 | ret = dev->ethtool_ops->set_ringparam(dev, &ringparam); |
| 1778 | if (!ret) |
| 1779 | ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL); |
| 1780 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1783 | static noinline_for_stack int ethtool_get_channels(struct net_device *dev, |
| 1784 | void __user *useraddr) |
| 1785 | { |
| 1786 | struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; |
| 1787 | |
| 1788 | if (!dev->ethtool_ops->get_channels) |
| 1789 | return -EOPNOTSUPP; |
| 1790 | |
| 1791 | dev->ethtool_ops->get_channels(dev, &channels); |
| 1792 | |
| 1793 | if (copy_to_user(useraddr, &channels, sizeof(channels))) |
| 1794 | return -EFAULT; |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static noinline_for_stack int ethtool_set_channels(struct net_device *dev, |
| 1799 | void __user *useraddr) |
| 1800 | { |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1801 | struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS }; |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1802 | u16 from_channel, to_channel; |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1803 | u32 max_rx_in_use = 0; |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1804 | unsigned int i; |
Michal Kubecek | 546379b | 2020-03-12 21:08:48 +0100 | [diff] [blame] | 1805 | int ret; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1806 | |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1807 | if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1808 | return -EOPNOTSUPP; |
| 1809 | |
| 1810 | if (copy_from_user(&channels, useraddr, sizeof(channels))) |
| 1811 | return -EFAULT; |
| 1812 | |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1813 | dev->ethtool_ops->get_channels(dev, &curr); |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1814 | |
Jakub Kicinski | 75c36db | 2020-05-15 12:49:02 -0700 | [diff] [blame] | 1815 | if (channels.rx_count == curr.rx_count && |
| 1816 | channels.tx_count == curr.tx_count && |
| 1817 | channels.combined_count == curr.combined_count && |
| 1818 | channels.other_count == curr.other_count) |
| 1819 | return 0; |
| 1820 | |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1821 | /* ensure new counts are within the maximums */ |
Jakub Kicinski | b8c8a2e | 2018-10-01 14:51:35 +0200 | [diff] [blame] | 1822 | if (channels.rx_count > curr.max_rx || |
| 1823 | channels.tx_count > curr.max_tx || |
| 1824 | channels.combined_count > curr.max_combined || |
| 1825 | channels.other_count > curr.max_other) |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1826 | return -EINVAL; |
| 1827 | |
Jakub Kicinski | 7be9251 | 2020-05-15 12:49:00 -0700 | [diff] [blame] | 1828 | /* ensure there is at least one RX and one TX channel */ |
| 1829 | if (!channels.combined_count && |
| 1830 | (!channels.rx_count || !channels.tx_count)) |
| 1831 | return -EINVAL; |
| 1832 | |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1833 | /* ensure the new Rx count fits within the configured Rx flow |
| 1834 | * indirection table settings */ |
| 1835 | if (netif_is_rxfh_configured(dev) && |
| 1836 | !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && |
| 1837 | (channels.combined_count + channels.rx_count) <= max_rx_in_use) |
| 1838 | return -EINVAL; |
| 1839 | |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1840 | /* Disabling channels, query zero-copy AF_XDP sockets */ |
| 1841 | from_channel = channels.combined_count + |
| 1842 | min(channels.rx_count, channels.tx_count); |
| 1843 | to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count); |
| 1844 | for (i = from_channel; i < to_channel; i++) |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 1845 | if (xsk_get_pool_from_qid(dev, i)) |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 1846 | return -EINVAL; |
| 1847 | |
Michal Kubecek | 546379b | 2020-03-12 21:08:48 +0100 | [diff] [blame] | 1848 | ret = dev->ethtool_ops->set_channels(dev, &channels); |
| 1849 | if (!ret) |
| 1850 | ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL); |
| 1851 | return ret; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1855 | { |
Li RongQing | e83887f | 2019-02-27 20:47:57 +0800 | [diff] [blame] | 1856 | struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | if (!dev->ethtool_ops->get_pauseparam) |
| 1859 | return -EOPNOTSUPP; |
| 1860 | |
| 1861 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); |
| 1862 | |
| 1863 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) |
| 1864 | return -EFAULT; |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1869 | { |
| 1870 | struct ethtool_pauseparam pauseparam; |
Michal Kubecek | bf37faa | 2020-03-28 00:01:33 +0100 | [diff] [blame] | 1871 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
Jeff Garzik | e1b90c4 | 2006-07-17 12:54:40 -0400 | [diff] [blame] | 1873 | if (!dev->ethtool_ops->set_pauseparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | return -EOPNOTSUPP; |
| 1875 | |
| 1876 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) |
| 1877 | return -EFAULT; |
| 1878 | |
Michal Kubecek | bf37faa | 2020-03-28 00:01:33 +0100 | [diff] [blame] | 1879 | ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam); |
| 1880 | if (!ret) |
| 1881 | ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); |
| 1882 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
| 1886 | { |
| 1887 | struct ethtool_test test; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1888 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1890 | int ret, test_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1892 | if (!ops->self_test || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1893 | return -EOPNOTSUPP; |
| 1894 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1895 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1896 | if (test_len < 0) |
| 1897 | return test_len; |
| 1898 | WARN_ON(test_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | if (copy_from_user(&test, useraddr, sizeof(test))) |
| 1901 | return -EFAULT; |
| 1902 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1903 | test.len = test_len; |
Austin Kim | 80ec82e | 2021-06-09 03:34:25 +0100 | [diff] [blame] | 1904 | data = kcalloc(test_len, sizeof(u64), GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | if (!data) |
| 1906 | return -ENOMEM; |
| 1907 | |
Andrew Lunn | 77e9b2a | 2020-04-20 00:11:52 +0200 | [diff] [blame] | 1908 | netif_testing_on(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | ops->self_test(dev, &test, data); |
Andrew Lunn | 77e9b2a | 2020-04-20 00:11:52 +0200 | [diff] [blame] | 1910 | netif_testing_off(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
| 1912 | ret = -EFAULT; |
| 1913 | if (copy_to_user(useraddr, &test, sizeof(test))) |
| 1914 | goto out; |
| 1915 | useraddr += sizeof(test); |
Gustavo A. R. Silva | ed71761 | 2021-09-28 14:57:35 -0500 | [diff] [blame] | 1916 | if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | goto out; |
| 1918 | ret = 0; |
| 1919 | |
| 1920 | out: |
| 1921 | kfree(data); |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) |
| 1926 | { |
| 1927 | struct ethtool_gstrings gstrings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | u8 *data; |
| 1929 | int ret; |
| 1930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) |
| 1932 | return -EFAULT; |
| 1933 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1934 | ret = __ethtool_get_sset_count(dev, gstrings.string_set); |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1935 | if (ret < 0) |
| 1936 | return ret; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1937 | if (ret > S32_MAX / ETH_GSTRING_LEN) |
| 1938 | return -ENOMEM; |
| 1939 | WARN_ON_ONCE(!ret); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1940 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1941 | gstrings.len = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Li RongQing | 3d88302 | 2019-03-29 09:18:02 +0800 | [diff] [blame] | 1943 | if (gstrings.len) { |
| 1944 | data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); |
| 1945 | if (!data) |
| 1946 | return -ENOMEM; |
| 1947 | |
| 1948 | __ethtool_get_strings(dev, gstrings.string_set, data); |
| 1949 | } else { |
| 1950 | data = NULL; |
| 1951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
| 1953 | ret = -EFAULT; |
| 1954 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) |
| 1955 | goto out; |
| 1956 | useraddr += sizeof(gstrings); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1957 | if (gstrings.len && |
Gustavo A. R. Silva | ed71761 | 2021-09-28 14:57:35 -0500 | [diff] [blame] | 1958 | copy_to_user(useraddr, data, |
| 1959 | array_size(gstrings.len, ETH_GSTRING_LEN))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | goto out; |
| 1961 | ret = 0; |
| 1962 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1963 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1964 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | return ret; |
| 1966 | } |
| 1967 | |
Alexander Duyck | 7888fe5 | 2021-03-16 17:30:36 -0700 | [diff] [blame] | 1968 | __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...) |
| 1969 | { |
| 1970 | va_list args; |
| 1971 | |
| 1972 | va_start(args, fmt); |
| 1973 | vsnprintf(*data, ETH_GSTRING_LEN, fmt, args); |
| 1974 | va_end(args); |
| 1975 | |
| 1976 | *data += ETH_GSTRING_LEN; |
| 1977 | } |
| 1978 | EXPORT_SYMBOL(ethtool_sprintf); |
| 1979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) |
| 1981 | { |
| 1982 | struct ethtool_value id; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1983 | static bool busy; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1984 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1985 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1987 | if (!ops->set_phys_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | return -EOPNOTSUPP; |
| 1989 | |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1990 | if (busy) |
| 1991 | return -EBUSY; |
| 1992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | if (copy_from_user(&id, useraddr, sizeof(id))) |
| 1994 | return -EFAULT; |
| 1995 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1996 | rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1997 | if (rc < 0) |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1998 | return rc; |
| 1999 | |
| 2000 | /* Drop the RTNL lock while waiting, but prevent reentry or |
| 2001 | * removal of the device. |
| 2002 | */ |
| 2003 | busy = true; |
| 2004 | dev_hold(dev); |
| 2005 | rtnl_unlock(); |
| 2006 | |
| 2007 | if (rc == 0) { |
| 2008 | /* Driver will handle this itself */ |
| 2009 | schedule_timeout_interruptible( |
Allan, Bruce W | 143780c | 2011-04-11 13:01:59 +0000 | [diff] [blame] | 2010 | id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 2011 | } else { |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 2012 | /* Driver expects to be called at twice the frequency in rc */ |
Edward Cree | 2adc6ed | 2020-09-01 18:52:32 +0100 | [diff] [blame] | 2013 | int n = rc * 2, interval = HZ / n; |
| 2014 | u64 count = n * id.data, i = 0; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 2015 | |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 2016 | do { |
Edward Cree | 2adc6ed | 2020-09-01 18:52:32 +0100 | [diff] [blame] | 2017 | rtnl_lock(); |
| 2018 | rc = ops->set_phys_id(dev, |
| 2019 | (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); |
| 2020 | rtnl_unlock(); |
| 2021 | if (rc) |
| 2022 | break; |
| 2023 | schedule_timeout_interruptible(interval); |
| 2024 | } while (!signal_pending(current) && (!id.data || i < count)); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | rtnl_lock(); |
| 2028 | dev_put(dev); |
| 2029 | busy = false; |
| 2030 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2031 | (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 2032 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) |
| 2036 | { |
| 2037 | struct ethtool_stats stats; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 2038 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 2040 | int ret, n_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 2042 | if (!ops->get_ethtool_stats || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 2043 | return -EOPNOTSUPP; |
| 2044 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 2045 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 2046 | if (n_stats < 0) |
| 2047 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 2048 | if (n_stats > S32_MAX / sizeof(u64)) |
| 2049 | return -ENOMEM; |
| 2050 | WARN_ON_ONCE(!n_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 2052 | return -EFAULT; |
| 2053 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 2054 | stats.n_stats = n_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
Li RongQing | 3d88302 | 2019-03-29 09:18:02 +0800 | [diff] [blame] | 2056 | if (n_stats) { |
| 2057 | data = vzalloc(array_size(n_stats, sizeof(u64))); |
| 2058 | if (!data) |
| 2059 | return -ENOMEM; |
| 2060 | ops->get_ethtool_stats(dev, &stats, data); |
| 2061 | } else { |
| 2062 | data = NULL; |
| 2063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
| 2065 | ret = -EFAULT; |
| 2066 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 2067 | goto out; |
| 2068 | useraddr += sizeof(stats); |
Gustavo A. R. Silva | 3dd1499 | 2020-06-15 18:01:07 -0500 | [diff] [blame] | 2069 | if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | goto out; |
| 2071 | ret = 0; |
| 2072 | |
| 2073 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 2074 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | return ret; |
| 2076 | } |
| 2077 | |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2078 | static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) |
| 2079 | { |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 2080 | const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2081 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2082 | struct phy_device *phydev = dev->phydev; |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2083 | struct ethtool_stats stats; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2084 | u64 *data; |
| 2085 | int ret, n_stats; |
| 2086 | |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2087 | if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count)) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2088 | return -EOPNOTSUPP; |
| 2089 | |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 2090 | if (dev->phydev && !ops->get_ethtool_phy_stats && |
| 2091 | phy_ops && phy_ops->get_sset_count) |
| 2092 | n_stats = phy_ops->get_sset_count(dev->phydev); |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2093 | else |
| 2094 | n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2095 | if (n_stats < 0) |
| 2096 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 2097 | if (n_stats > S32_MAX / sizeof(u64)) |
| 2098 | return -ENOMEM; |
| 2099 | WARN_ON_ONCE(!n_stats); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2100 | |
| 2101 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 2102 | return -EFAULT; |
| 2103 | |
| 2104 | stats.n_stats = n_stats; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2105 | |
Li RongQing | 3d88302 | 2019-03-29 09:18:02 +0800 | [diff] [blame] | 2106 | if (n_stats) { |
| 2107 | data = vzalloc(array_size(n_stats, sizeof(u64))); |
| 2108 | if (!data) |
| 2109 | return -ENOMEM; |
| 2110 | |
Florian Fainelli | 1780951 | 2020-07-08 09:46:25 -0700 | [diff] [blame] | 2111 | if (dev->phydev && !ops->get_ethtool_phy_stats && |
| 2112 | phy_ops && phy_ops->get_stats) { |
| 2113 | ret = phy_ops->get_stats(dev->phydev, &stats, data); |
Li RongQing | 3d88302 | 2019-03-29 09:18:02 +0800 | [diff] [blame] | 2114 | if (ret < 0) |
| 2115 | goto out; |
| 2116 | } else { |
| 2117 | ops->get_ethtool_phy_stats(dev, &stats, data); |
| 2118 | } |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2119 | } else { |
Li RongQing | 3d88302 | 2019-03-29 09:18:02 +0800 | [diff] [blame] | 2120 | data = NULL; |
Florian Fainelli | 9994338 | 2018-04-25 12:12:48 -0700 | [diff] [blame] | 2121 | } |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2122 | |
| 2123 | ret = -EFAULT; |
| 2124 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 2125 | goto out; |
| 2126 | useraddr += sizeof(stats); |
Gustavo A. R. Silva | 3dd1499 | 2020-06-15 18:01:07 -0500 | [diff] [blame] | 2127 | if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2128 | goto out; |
| 2129 | ret = 0; |
| 2130 | |
| 2131 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 2132 | vfree(data); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2133 | return ret; |
| 2134 | } |
| 2135 | |
viro@ftp.linux.org.uk | 0bf0519d | 2005-09-05 03:26:18 +0100 | [diff] [blame] | 2136 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2137 | { |
| 2138 | struct ethtool_perm_addr epaddr; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2139 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2140 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2141 | return -EFAULT; |
| 2142 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2143 | if (epaddr.size < dev->addr_len) |
| 2144 | return -ETOOSMALL; |
| 2145 | epaddr.size = dev->addr_len; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2146 | |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2147 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2148 | return -EFAULT; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2149 | useraddr += sizeof(epaddr); |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2150 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
| 2151 | return -EFAULT; |
| 2152 | return 0; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2155 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
| 2156 | u32 cmd, u32 (*actor)(struct net_device *)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2157 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 2158 | struct ethtool_value edata = { .cmd = cmd }; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2159 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2160 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2161 | return -EOPNOTSUPP; |
| 2162 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2163 | edata.data = actor(dev); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2164 | |
| 2165 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 2166 | return -EFAULT; |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2170 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
| 2171 | void (*actor)(struct net_device *, u32)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2172 | { |
| 2173 | struct ethtool_value edata; |
| 2174 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2175 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2176 | return -EOPNOTSUPP; |
| 2177 | |
| 2178 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2179 | return -EFAULT; |
| 2180 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2181 | actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2182 | return 0; |
| 2183 | } |
| 2184 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2185 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
| 2186 | int (*actor)(struct net_device *, u32)) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2187 | { |
| 2188 | struct ethtool_value edata; |
| 2189 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2190 | if (!actor) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2191 | return -EOPNOTSUPP; |
| 2192 | |
| 2193 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2194 | return -EFAULT; |
| 2195 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2196 | return actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2199 | static int |
| 2200 | ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req) |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2201 | { |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 2202 | if (!dev->ethtool_ops->flash_device) { |
| 2203 | req->devlink = netdev_to_devlink_get(dev); |
| 2204 | return 0; |
| 2205 | } |
Jakub Kicinski | 4eceba1 | 2019-02-14 13:40:45 -0800 | [diff] [blame] | 2206 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2207 | return dev->ethtool_ops->flash_device(dev, &req->efl); |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2210 | static int ethtool_set_dump(struct net_device *dev, |
| 2211 | void __user *useraddr) |
| 2212 | { |
| 2213 | struct ethtool_dump dump; |
| 2214 | |
| 2215 | if (!dev->ethtool_ops->set_dump) |
| 2216 | return -EOPNOTSUPP; |
| 2217 | |
| 2218 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2219 | return -EFAULT; |
| 2220 | |
| 2221 | return dev->ethtool_ops->set_dump(dev, &dump); |
| 2222 | } |
| 2223 | |
| 2224 | static int ethtool_get_dump_flag(struct net_device *dev, |
| 2225 | void __user *useraddr) |
| 2226 | { |
| 2227 | int ret; |
| 2228 | struct ethtool_dump dump; |
| 2229 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2230 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2231 | if (!ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2232 | return -EOPNOTSUPP; |
| 2233 | |
| 2234 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2235 | return -EFAULT; |
| 2236 | |
| 2237 | ret = ops->get_dump_flag(dev, &dump); |
| 2238 | if (ret) |
| 2239 | return ret; |
| 2240 | |
| 2241 | if (copy_to_user(useraddr, &dump, sizeof(dump))) |
| 2242 | return -EFAULT; |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
| 2246 | static int ethtool_get_dump_data(struct net_device *dev, |
| 2247 | void __user *useraddr) |
| 2248 | { |
| 2249 | int ret; |
| 2250 | __u32 len; |
| 2251 | struct ethtool_dump dump, tmp; |
| 2252 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2253 | void *data = NULL; |
| 2254 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2255 | if (!ops->get_dump_data || !ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2256 | return -EOPNOTSUPP; |
| 2257 | |
| 2258 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2259 | return -EFAULT; |
| 2260 | |
| 2261 | memset(&tmp, 0, sizeof(tmp)); |
| 2262 | tmp.cmd = ETHTOOL_GET_DUMP_FLAG; |
| 2263 | ret = ops->get_dump_flag(dev, &tmp); |
| 2264 | if (ret) |
| 2265 | return ret; |
| 2266 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2267 | len = min(tmp.len, dump.len); |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2268 | if (!len) |
| 2269 | return -EFAULT; |
| 2270 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2271 | /* Don't ever let the driver think there's more space available |
| 2272 | * than it requested with .get_dump_flag(). |
| 2273 | */ |
| 2274 | dump.len = len; |
| 2275 | |
| 2276 | /* Always allocate enough space to hold the whole thing so that the |
| 2277 | * driver does not need to check the length and bother with partial |
| 2278 | * dumping. |
| 2279 | */ |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2280 | data = vzalloc(tmp.len); |
| 2281 | if (!data) |
| 2282 | return -ENOMEM; |
| 2283 | ret = ops->get_dump_data(dev, &dump, data); |
| 2284 | if (ret) |
| 2285 | goto out; |
| 2286 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2287 | /* There are two sane possibilities: |
| 2288 | * 1. The driver's .get_dump_data() does not touch dump.len. |
| 2289 | * 2. Or it may set dump.len to how much it really writes, which |
| 2290 | * should be tmp.len (or len if it can do a partial dump). |
| 2291 | * In any case respond to userspace with the actual length of data |
| 2292 | * it's receiving. |
| 2293 | */ |
| 2294 | WARN_ON(dump.len != len && dump.len != tmp.len); |
| 2295 | dump.len = len; |
| 2296 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2297 | if (copy_to_user(useraddr, &dump, sizeof(dump))) { |
| 2298 | ret = -EFAULT; |
| 2299 | goto out; |
| 2300 | } |
| 2301 | useraddr += offsetof(struct ethtool_dump, data); |
| 2302 | if (copy_to_user(useraddr, data, len)) |
| 2303 | ret = -EFAULT; |
| 2304 | out: |
| 2305 | vfree(data); |
| 2306 | return ret; |
| 2307 | } |
| 2308 | |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2309 | static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) |
| 2310 | { |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2311 | struct ethtool_ts_info info; |
Michal Kubecek | 5b071c5 | 2020-03-28 00:01:58 +0100 | [diff] [blame] | 2312 | int err; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2313 | |
Michal Kubecek | 5b071c5 | 2020-03-28 00:01:58 +0100 | [diff] [blame] | 2314 | err = __ethtool_get_ts_info(dev, &info); |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2315 | if (err) |
| 2316 | return err; |
| 2317 | |
| 2318 | if (copy_to_user(useraddr, &info, sizeof(info))) |
Michal Kubecek | 5b071c5 | 2020-03-28 00:01:58 +0100 | [diff] [blame] | 2319 | return -EFAULT; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2320 | |
Michal Kubecek | 5b071c5 | 2020-03-28 00:01:58 +0100 | [diff] [blame] | 2321 | return 0; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Andrew Lunn | 95dfc7e | 2021-04-09 11:06:38 +0300 | [diff] [blame] | 2324 | int ethtool_get_module_info_call(struct net_device *dev, |
| 2325 | struct ethtool_modinfo *modinfo) |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2326 | { |
| 2327 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2328 | struct phy_device *phydev = dev->phydev; |
| 2329 | |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 2330 | if (dev->sfp_bus) |
| 2331 | return sfp_get_module_info(dev->sfp_bus, modinfo); |
| 2332 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2333 | if (phydev && phydev->drv && phydev->drv->module_info) |
| 2334 | return phydev->drv->module_info(phydev, modinfo); |
| 2335 | |
| 2336 | if (ops->get_module_info) |
| 2337 | return ops->get_module_info(dev, modinfo); |
| 2338 | |
| 2339 | return -EOPNOTSUPP; |
| 2340 | } |
| 2341 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2342 | static int ethtool_get_module_info(struct net_device *dev, |
| 2343 | void __user *useraddr) |
| 2344 | { |
| 2345 | int ret; |
| 2346 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2347 | |
| 2348 | if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) |
| 2349 | return -EFAULT; |
| 2350 | |
Andrew Lunn | 95dfc7e | 2021-04-09 11:06:38 +0300 | [diff] [blame] | 2351 | ret = ethtool_get_module_info_call(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2352 | if (ret) |
| 2353 | return ret; |
| 2354 | |
| 2355 | if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) |
| 2356 | return -EFAULT; |
| 2357 | |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
Andrew Lunn | 95dfc7e | 2021-04-09 11:06:38 +0300 | [diff] [blame] | 2361 | int ethtool_get_module_eeprom_call(struct net_device *dev, |
| 2362 | struct ethtool_eeprom *ee, u8 *data) |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2363 | { |
| 2364 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2365 | struct phy_device *phydev = dev->phydev; |
| 2366 | |
Russell King | e679c9c | 2018-03-28 15:44:16 -0700 | [diff] [blame] | 2367 | if (dev->sfp_bus) |
| 2368 | return sfp_get_module_eeprom(dev->sfp_bus, ee, data); |
| 2369 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2370 | if (phydev && phydev->drv && phydev->drv->module_eeprom) |
| 2371 | return phydev->drv->module_eeprom(phydev, ee, data); |
| 2372 | |
| 2373 | if (ops->get_module_eeprom) |
| 2374 | return ops->get_module_eeprom(dev, ee, data); |
| 2375 | |
| 2376 | return -EOPNOTSUPP; |
| 2377 | } |
| 2378 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2379 | static int ethtool_get_module_eeprom(struct net_device *dev, |
| 2380 | void __user *useraddr) |
| 2381 | { |
| 2382 | int ret; |
| 2383 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2384 | |
Andrew Lunn | 95dfc7e | 2021-04-09 11:06:38 +0300 | [diff] [blame] | 2385 | ret = ethtool_get_module_info_call(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2386 | if (ret) |
| 2387 | return ret; |
| 2388 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2389 | return ethtool_get_any_eeprom(dev, useraddr, |
Andrew Lunn | 95dfc7e | 2021-04-09 11:06:38 +0300 | [diff] [blame] | 2390 | ethtool_get_module_eeprom_call, |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2391 | modinfo.eeprom_len); |
| 2392 | } |
| 2393 | |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2394 | static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) |
| 2395 | { |
| 2396 | switch (tuna->id) { |
| 2397 | case ETHTOOL_RX_COPYBREAK: |
Eric Dumazet | 1255a50 | 2014-10-05 12:35:21 +0300 | [diff] [blame] | 2398 | case ETHTOOL_TX_COPYBREAK: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2399 | if (tuna->len != sizeof(u32) || |
| 2400 | tuna->type_id != ETHTOOL_TUNABLE_U32) |
| 2401 | return -EINVAL; |
| 2402 | break; |
Inbar Karmy | e1577c1 | 2017-11-20 16:14:30 +0200 | [diff] [blame] | 2403 | case ETHTOOL_PFC_PREVENTION_TOUT: |
| 2404 | if (tuna->len != sizeof(u16) || |
| 2405 | tuna->type_id != ETHTOOL_TUNABLE_U16) |
| 2406 | return -EINVAL; |
| 2407 | break; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2408 | default: |
| 2409 | return -EINVAL; |
| 2410 | } |
| 2411 | |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
| 2415 | static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) |
| 2416 | { |
| 2417 | int ret; |
| 2418 | struct ethtool_tunable tuna; |
| 2419 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2420 | void *data; |
| 2421 | |
| 2422 | if (!ops->get_tunable) |
| 2423 | return -EOPNOTSUPP; |
| 2424 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2425 | return -EFAULT; |
| 2426 | ret = ethtool_tunable_valid(&tuna); |
| 2427 | if (ret) |
| 2428 | return ret; |
Austin Kim | 80ec82e | 2021-06-09 03:34:25 +0100 | [diff] [blame] | 2429 | data = kzalloc(tuna.len, GFP_USER); |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2430 | if (!data) |
| 2431 | return -ENOMEM; |
| 2432 | ret = ops->get_tunable(dev, &tuna, data); |
| 2433 | if (ret) |
| 2434 | goto out; |
| 2435 | useraddr += sizeof(tuna); |
| 2436 | ret = -EFAULT; |
| 2437 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2438 | goto out; |
| 2439 | ret = 0; |
| 2440 | |
| 2441 | out: |
| 2442 | kfree(data); |
| 2443 | return ret; |
| 2444 | } |
| 2445 | |
| 2446 | static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) |
| 2447 | { |
| 2448 | int ret; |
| 2449 | struct ethtool_tunable tuna; |
| 2450 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2451 | void *data; |
| 2452 | |
| 2453 | if (!ops->set_tunable) |
| 2454 | return -EOPNOTSUPP; |
| 2455 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2456 | return -EFAULT; |
| 2457 | ret = ethtool_tunable_valid(&tuna); |
| 2458 | if (ret) |
| 2459 | return ret; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2460 | useraddr += sizeof(tuna); |
Al Viro | 30e7e3e | 2017-05-13 18:31:26 -0400 | [diff] [blame] | 2461 | data = memdup_user(useraddr, tuna.len); |
| 2462 | if (IS_ERR(data)) |
| 2463 | return PTR_ERR(data); |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2464 | ret = ops->set_tunable(dev, &tuna, data); |
| 2465 | |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2466 | kfree(data); |
| 2467 | return ret; |
| 2468 | } |
| 2469 | |
Arnd Bergmann | 3499e87 | 2019-03-07 16:58:35 +0100 | [diff] [blame] | 2470 | static noinline_for_stack int |
| 2471 | ethtool_get_per_queue_coalesce(struct net_device *dev, |
| 2472 | void __user *useraddr, |
| 2473 | struct ethtool_per_queue_op *per_queue_opt) |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2474 | { |
| 2475 | u32 bit; |
| 2476 | int ret; |
| 2477 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2478 | |
| 2479 | if (!dev->ethtool_ops->get_per_queue_coalesce) |
| 2480 | return -EOPNOTSUPP; |
| 2481 | |
| 2482 | useraddr += sizeof(*per_queue_opt); |
| 2483 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 2484 | bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, |
| 2485 | MAX_NUM_QUEUE); |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2486 | |
| 2487 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2488 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
| 2489 | |
| 2490 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); |
| 2491 | if (ret != 0) |
| 2492 | return ret; |
| 2493 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 2494 | return -EFAULT; |
| 2495 | useraddr += sizeof(coalesce); |
| 2496 | } |
| 2497 | |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
Arnd Bergmann | 3499e87 | 2019-03-07 16:58:35 +0100 | [diff] [blame] | 2501 | static noinline_for_stack int |
| 2502 | ethtool_set_per_queue_coalesce(struct net_device *dev, |
| 2503 | void __user *useraddr, |
| 2504 | struct ethtool_per_queue_op *per_queue_opt) |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2505 | { |
| 2506 | u32 bit; |
| 2507 | int i, ret = 0; |
| 2508 | int n_queue; |
| 2509 | struct ethtool_coalesce *backup = NULL, *tmp = NULL; |
| 2510 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2511 | |
| 2512 | if ((!dev->ethtool_ops->set_per_queue_coalesce) || |
| 2513 | (!dev->ethtool_ops->get_per_queue_coalesce)) |
| 2514 | return -EOPNOTSUPP; |
| 2515 | |
| 2516 | useraddr += sizeof(*per_queue_opt); |
| 2517 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 2518 | bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2519 | n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); |
| 2520 | tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); |
| 2521 | if (!backup) |
| 2522 | return -ENOMEM; |
| 2523 | |
| 2524 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2525 | struct ethtool_coalesce coalesce; |
| 2526 | |
| 2527 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); |
| 2528 | if (ret != 0) |
| 2529 | goto roll_back; |
| 2530 | |
| 2531 | tmp++; |
| 2532 | |
| 2533 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { |
| 2534 | ret = -EFAULT; |
| 2535 | goto roll_back; |
| 2536 | } |
| 2537 | |
Jakub Kicinski | 95cddcb | 2020-03-04 21:15:31 -0800 | [diff] [blame] | 2538 | if (!ethtool_set_coalesce_supported(dev, &coalesce)) { |
| 2539 | ret = -EOPNOTSUPP; |
| 2540 | goto roll_back; |
| 2541 | } |
| 2542 | |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2543 | ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); |
| 2544 | if (ret != 0) |
| 2545 | goto roll_back; |
| 2546 | |
| 2547 | useraddr += sizeof(coalesce); |
| 2548 | } |
| 2549 | |
| 2550 | roll_back: |
| 2551 | if (ret != 0) { |
| 2552 | tmp = backup; |
| 2553 | for_each_set_bit(i, queue_mask, bit) { |
| 2554 | dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); |
| 2555 | tmp++; |
| 2556 | } |
| 2557 | } |
| 2558 | kfree(backup); |
| 2559 | |
| 2560 | return ret; |
| 2561 | } |
| 2562 | |
Arnd Bergmann | 3499e87 | 2019-03-07 16:58:35 +0100 | [diff] [blame] | 2563 | static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2564 | void __user *useraddr, u32 sub_cmd) |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2565 | { |
| 2566 | struct ethtool_per_queue_op per_queue_opt; |
| 2567 | |
| 2568 | if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) |
| 2569 | return -EFAULT; |
| 2570 | |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2571 | if (per_queue_opt.sub_command != sub_cmd) |
| 2572 | return -EINVAL; |
| 2573 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2574 | switch (per_queue_opt.sub_command) { |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2575 | case ETHTOOL_GCOALESCE: |
| 2576 | return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2577 | case ETHTOOL_SCOALESCE: |
| 2578 | return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2579 | default: |
| 2580 | return -EOPNOTSUPP; |
Tom Rix | 9d253c0 | 2020-11-01 07:56:01 -0800 | [diff] [blame] | 2581 | } |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2582 | } |
| 2583 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2584 | static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) |
| 2585 | { |
| 2586 | switch (tuna->id) { |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 2587 | case ETHTOOL_PHY_DOWNSHIFT: |
Heiner Kallweit | 3aeb080 | 2019-03-25 19:34:58 +0100 | [diff] [blame] | 2588 | case ETHTOOL_PHY_FAST_LINK_DOWN: |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 2589 | if (tuna->len != sizeof(u8) || |
| 2590 | tuna->type_id != ETHTOOL_TUNABLE_U8) |
| 2591 | return -EINVAL; |
| 2592 | break; |
Alexandru Ardelean | 9f2f13f | 2019-09-16 10:35:25 +0300 | [diff] [blame] | 2593 | case ETHTOOL_PHY_EDPD: |
| 2594 | if (tuna->len != sizeof(u16) || |
| 2595 | tuna->type_id != ETHTOOL_TUNABLE_U16) |
| 2596 | return -EINVAL; |
| 2597 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2598 | default: |
| 2599 | return -EINVAL; |
| 2600 | } |
| 2601 | |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
| 2605 | static int get_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2606 | { |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2607 | struct phy_device *phydev = dev->phydev; |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2608 | struct ethtool_tunable tuna; |
| 2609 | bool phy_drv_tunable; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2610 | void *data; |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2611 | int ret; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2612 | |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2613 | phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; |
| 2614 | if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2615 | return -EOPNOTSUPP; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2616 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2617 | return -EFAULT; |
| 2618 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2619 | if (ret) |
| 2620 | return ret; |
Austin Kim | 80ec82e | 2021-06-09 03:34:25 +0100 | [diff] [blame] | 2621 | data = kzalloc(tuna.len, GFP_USER); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2622 | if (!data) |
| 2623 | return -ENOMEM; |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2624 | if (phy_drv_tunable) { |
| 2625 | mutex_lock(&phydev->lock); |
| 2626 | ret = phydev->drv->get_tunable(phydev, &tuna, data); |
| 2627 | mutex_unlock(&phydev->lock); |
| 2628 | } else { |
| 2629 | ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); |
| 2630 | } |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2631 | if (ret) |
| 2632 | goto out; |
| 2633 | useraddr += sizeof(tuna); |
| 2634 | ret = -EFAULT; |
| 2635 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2636 | goto out; |
| 2637 | ret = 0; |
| 2638 | |
| 2639 | out: |
| 2640 | kfree(data); |
| 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | static int set_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2645 | { |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2646 | struct phy_device *phydev = dev->phydev; |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2647 | struct ethtool_tunable tuna; |
| 2648 | bool phy_drv_tunable; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2649 | void *data; |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2650 | int ret; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2651 | |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2652 | phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; |
| 2653 | if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2654 | return -EOPNOTSUPP; |
| 2655 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2656 | return -EFAULT; |
| 2657 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2658 | if (ret) |
| 2659 | return ret; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2660 | useraddr += sizeof(tuna); |
Al Viro | 30e7e3e | 2017-05-13 18:31:26 -0400 | [diff] [blame] | 2661 | data = memdup_user(useraddr, tuna.len); |
| 2662 | if (IS_ERR(data)) |
| 2663 | return PTR_ERR(data); |
Igor Russkikh | c6db31f | 2020-10-05 18:39:37 +0300 | [diff] [blame] | 2664 | if (phy_drv_tunable) { |
| 2665 | mutex_lock(&phydev->lock); |
| 2666 | ret = phydev->drv->set_tunable(phydev, &tuna, data); |
| 2667 | mutex_unlock(&phydev->lock); |
| 2668 | } else { |
| 2669 | ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); |
| 2670 | } |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2671 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2672 | kfree(data); |
| 2673 | return ret; |
| 2674 | } |
| 2675 | |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2676 | static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) |
| 2677 | { |
Li RongQing | e83887f | 2019-02-27 20:47:57 +0800 | [diff] [blame] | 2678 | struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; |
Edward Cree | a6d5051 | 2018-02-28 19:15:58 +0000 | [diff] [blame] | 2679 | int rc; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2680 | |
| 2681 | if (!dev->ethtool_ops->get_fecparam) |
| 2682 | return -EOPNOTSUPP; |
| 2683 | |
Edward Cree | a6d5051 | 2018-02-28 19:15:58 +0000 | [diff] [blame] | 2684 | rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); |
| 2685 | if (rc) |
| 2686 | return rc; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2687 | |
Jakub Kicinski | 240e1144 | 2021-03-24 18:11:57 -0700 | [diff] [blame] | 2688 | if (WARN_ON_ONCE(fecparam.reserved)) |
| 2689 | fecparam.reserved = 0; |
| 2690 | |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2691 | if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) |
| 2692 | return -EFAULT; |
| 2693 | return 0; |
| 2694 | } |
| 2695 | |
| 2696 | static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) |
| 2697 | { |
| 2698 | struct ethtool_fecparam fecparam; |
| 2699 | |
| 2700 | if (!dev->ethtool_ops->set_fecparam) |
| 2701 | return -EOPNOTSUPP; |
| 2702 | |
| 2703 | if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) |
| 2704 | return -EFAULT; |
| 2705 | |
Jakub Kicinski | cf2cc0b | 2021-03-26 13:22:22 -0700 | [diff] [blame] | 2706 | if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) |
Jakub Kicinski | 42ce127 | 2021-03-24 18:11:59 -0700 | [diff] [blame] | 2707 | return -EINVAL; |
| 2708 | |
Jakub Kicinski | d3b37fc | 2021-03-24 18:11:58 -0700 | [diff] [blame] | 2709 | fecparam.active_fec = 0; |
Jakub Kicinski | 240e1144 | 2021-03-24 18:11:57 -0700 | [diff] [blame] | 2710 | fecparam.reserved = 0; |
| 2711 | |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2712 | return dev->ethtool_ops->set_fecparam(dev, &fecparam); |
| 2713 | } |
| 2714 | |
Yan Burman | 600fed5 | 2013-06-03 02:03:34 +0000 | [diff] [blame] | 2715 | /* The main entry point in this file. Called from net/core/dev_ioctl.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | |
Jakub Kicinski | f49deaa | 2021-10-30 10:18:48 -0700 | [diff] [blame] | 2717 | static int |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2718 | __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr, |
| 2719 | u32 ethcmd, struct ethtool_devlink_compat *devlink_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | { |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2721 | struct net_device *dev; |
| 2722 | u32 sub_cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | int rc; |
Bjørn Mork | b29d314 | 2013-05-01 23:06:42 +0000 | [diff] [blame] | 2724 | netdev_features_t old_features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2726 | dev = __dev_get_by_name(net, ifr->ifr_name); |
Heiner Kallweit | f32a213 | 2021-08-01 12:36:48 +0200 | [diff] [blame] | 2727 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | return -ENODEV; |
| 2729 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2730 | if (ethcmd == ETHTOOL_PERQUEUE) { |
| 2731 | if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) |
| 2732 | return -EFAULT; |
| 2733 | } else { |
| 2734 | sub_cmd = ethcmd; |
| 2735 | } |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2736 | /* Allow some commands to be done by anyone */ |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2737 | switch (sub_cmd) { |
stephen hemminger | 0fdc100 | 2010-08-23 10:24:18 +0000 | [diff] [blame] | 2738 | case ETHTOOL_GSET: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2739 | case ETHTOOL_GDRVINFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2740 | case ETHTOOL_GMSGLVL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2741 | case ETHTOOL_GLINK: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2742 | case ETHTOOL_GCOALESCE: |
| 2743 | case ETHTOOL_GRINGPARAM: |
| 2744 | case ETHTOOL_GPAUSEPARAM: |
| 2745 | case ETHTOOL_GRXCSUM: |
| 2746 | case ETHTOOL_GTXCSUM: |
| 2747 | case ETHTOOL_GSG: |
Michał Mirosław | f80400a | 2012-01-22 00:20:40 +0000 | [diff] [blame] | 2748 | case ETHTOOL_GSSET_INFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2749 | case ETHTOOL_GSTRINGS: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2750 | case ETHTOOL_GSTATS: |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2751 | case ETHTOOL_GPHYSTATS: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2752 | case ETHTOOL_GTSO: |
| 2753 | case ETHTOOL_GPERMADDR: |
Maciej Żenczykowski | 474ff26 | 2018-09-22 01:34:01 -0700 | [diff] [blame] | 2754 | case ETHTOOL_GUFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2755 | case ETHTOOL_GGSO: |
stephen hemminger | 1cab819 | 2010-02-11 13:48:29 +0000 | [diff] [blame] | 2756 | case ETHTOOL_GGRO: |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2757 | case ETHTOOL_GFLAGS: |
| 2758 | case ETHTOOL_GPFLAGS: |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2759 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2760 | case ETHTOOL_GRXRINGS: |
| 2761 | case ETHTOOL_GRXCLSRLCNT: |
| 2762 | case ETHTOOL_GRXCLSRULE: |
| 2763 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2764 | case ETHTOOL_GRXFHINDIR: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2765 | case ETHTOOL_GRSSH: |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2766 | case ETHTOOL_GFEATURES: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2767 | case ETHTOOL_GCHANNELS: |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2768 | case ETHTOOL_GET_TS_INFO: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2769 | case ETHTOOL_GEEE: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2770 | case ETHTOOL_GTUNABLE: |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2771 | case ETHTOOL_PHY_GTUNABLE: |
Miroslav Lichvar | 8006f6b | 2016-11-24 10:55:06 +0100 | [diff] [blame] | 2772 | case ETHTOOL_GLINKSETTINGS: |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2773 | case ETHTOOL_GFECPARAM: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2774 | break; |
| 2775 | default: |
Eric W. Biederman | 5e1fccc | 2012-11-16 03:03:04 +0000 | [diff] [blame] | 2776 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2777 | return -EPERM; |
| 2778 | } |
| 2779 | |
Heiner Kallweit | f32a213 | 2021-08-01 12:36:48 +0200 | [diff] [blame] | 2780 | if (dev->dev.parent) |
| 2781 | pm_runtime_get_sync(dev->dev.parent); |
| 2782 | |
| 2783 | if (!netif_device_present(dev)) { |
| 2784 | rc = -ENODEV; |
| 2785 | goto out; |
| 2786 | } |
| 2787 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2788 | if (dev->ethtool_ops->begin) { |
| 2789 | rc = dev->ethtool_ops->begin(dev); |
Heiner Kallweit | f32a213 | 2021-08-01 12:36:48 +0200 | [diff] [blame] | 2790 | if (rc < 0) |
| 2791 | goto out; |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2792 | } |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 2793 | old_features = dev->features; |
| 2794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | switch (ethcmd) { |
| 2796 | case ETHTOOL_GSET: |
| 2797 | rc = ethtool_get_settings(dev, useraddr); |
| 2798 | break; |
| 2799 | case ETHTOOL_SSET: |
| 2800 | rc = ethtool_set_settings(dev, useraddr); |
| 2801 | break; |
| 2802 | case ETHTOOL_GDRVINFO: |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2803 | rc = ethtool_get_drvinfo(dev, devlink_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | break; |
| 2805 | case ETHTOOL_GREGS: |
| 2806 | rc = ethtool_get_regs(dev, useraddr); |
| 2807 | break; |
| 2808 | case ETHTOOL_GWOL: |
| 2809 | rc = ethtool_get_wol(dev, useraddr); |
| 2810 | break; |
| 2811 | case ETHTOOL_SWOL: |
| 2812 | rc = ethtool_set_wol(dev, useraddr); |
| 2813 | break; |
| 2814 | case ETHTOOL_GMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2815 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2816 | dev->ethtool_ops->get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case ETHTOOL_SMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2819 | rc = ethtool_set_value_void(dev, useraddr, |
| 2820 | dev->ethtool_ops->set_msglevel); |
Michal Kubecek | 0bda7af | 2020-01-26 23:11:10 +0100 | [diff] [blame] | 2821 | if (!rc) |
| 2822 | ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | break; |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 2824 | case ETHTOOL_GEEE: |
| 2825 | rc = ethtool_get_eee(dev, useraddr); |
| 2826 | break; |
| 2827 | case ETHTOOL_SEEE: |
| 2828 | rc = ethtool_set_eee(dev, useraddr); |
| 2829 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | case ETHTOOL_NWAY_RST: |
| 2831 | rc = ethtool_nway_reset(dev); |
| 2832 | break; |
| 2833 | case ETHTOOL_GLINK: |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 2834 | rc = ethtool_get_link(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | case ETHTOOL_GEEPROM: |
| 2837 | rc = ethtool_get_eeprom(dev, useraddr); |
| 2838 | break; |
| 2839 | case ETHTOOL_SEEPROM: |
| 2840 | rc = ethtool_set_eeprom(dev, useraddr); |
| 2841 | break; |
| 2842 | case ETHTOOL_GCOALESCE: |
| 2843 | rc = ethtool_get_coalesce(dev, useraddr); |
| 2844 | break; |
| 2845 | case ETHTOOL_SCOALESCE: |
| 2846 | rc = ethtool_set_coalesce(dev, useraddr); |
| 2847 | break; |
| 2848 | case ETHTOOL_GRINGPARAM: |
| 2849 | rc = ethtool_get_ringparam(dev, useraddr); |
| 2850 | break; |
| 2851 | case ETHTOOL_SRINGPARAM: |
| 2852 | rc = ethtool_set_ringparam(dev, useraddr); |
| 2853 | break; |
| 2854 | case ETHTOOL_GPAUSEPARAM: |
| 2855 | rc = ethtool_get_pauseparam(dev, useraddr); |
| 2856 | break; |
| 2857 | case ETHTOOL_SPAUSEPARAM: |
| 2858 | rc = ethtool_set_pauseparam(dev, useraddr); |
| 2859 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | case ETHTOOL_TEST: |
| 2861 | rc = ethtool_self_test(dev, useraddr); |
| 2862 | break; |
| 2863 | case ETHTOOL_GSTRINGS: |
| 2864 | rc = ethtool_get_strings(dev, useraddr); |
| 2865 | break; |
| 2866 | case ETHTOOL_PHYS_ID: |
| 2867 | rc = ethtool_phys_id(dev, useraddr); |
| 2868 | break; |
| 2869 | case ETHTOOL_GSTATS: |
| 2870 | rc = ethtool_get_stats(dev, useraddr); |
| 2871 | break; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2872 | case ETHTOOL_GPERMADDR: |
| 2873 | rc = ethtool_get_perm_addr(dev, useraddr); |
| 2874 | break; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2875 | case ETHTOOL_GFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2876 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2877 | __ethtool_get_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2878 | break; |
| 2879 | case ETHTOOL_SFLAGS: |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2880 | rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2881 | break; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2882 | case ETHTOOL_GPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2883 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2884 | dev->ethtool_ops->get_priv_flags); |
Michal Kubecek | 111dcba | 2020-03-12 21:08:18 +0100 | [diff] [blame] | 2885 | if (!rc) |
| 2886 | ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2887 | break; |
| 2888 | case ETHTOOL_SPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2889 | rc = ethtool_set_value(dev, useraddr, |
| 2890 | dev->ethtool_ops->set_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2891 | break; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2892 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2893 | case ETHTOOL_GRXRINGS: |
| 2894 | case ETHTOOL_GRXCLSRLCNT: |
| 2895 | case ETHTOOL_GRXCLSRULE: |
| 2896 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2897 | rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2898 | break; |
| 2899 | case ETHTOOL_SRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2900 | case ETHTOOL_SRXCLSRLDEL: |
| 2901 | case ETHTOOL_SRXCLSRLINS: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2902 | rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2903 | break; |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2904 | case ETHTOOL_FLASHDEV: |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 2905 | rc = ethtool_flash_device(dev, devlink_state); |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2906 | break; |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 2907 | case ETHTOOL_RESET: |
| 2908 | rc = ethtool_reset(dev, useraddr); |
| 2909 | break; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 2910 | case ETHTOOL_GSSET_INFO: |
| 2911 | rc = ethtool_get_sset_info(dev, useraddr); |
| 2912 | break; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 2913 | case ETHTOOL_GRXFHINDIR: |
| 2914 | rc = ethtool_get_rxfh_indir(dev, useraddr); |
| 2915 | break; |
| 2916 | case ETHTOOL_SRXFHINDIR: |
| 2917 | rc = ethtool_set_rxfh_indir(dev, useraddr); |
| 2918 | break; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2919 | case ETHTOOL_GRSSH: |
| 2920 | rc = ethtool_get_rxfh(dev, useraddr); |
| 2921 | break; |
| 2922 | case ETHTOOL_SRSSH: |
| 2923 | rc = ethtool_set_rxfh(dev, useraddr); |
| 2924 | break; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2925 | case ETHTOOL_GFEATURES: |
| 2926 | rc = ethtool_get_features(dev, useraddr); |
| 2927 | break; |
| 2928 | case ETHTOOL_SFEATURES: |
| 2929 | rc = ethtool_set_features(dev, useraddr); |
| 2930 | break; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2931 | case ETHTOOL_GTXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2932 | case ETHTOOL_GRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2933 | case ETHTOOL_GSG: |
| 2934 | case ETHTOOL_GTSO: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2935 | case ETHTOOL_GGSO: |
| 2936 | case ETHTOOL_GGRO: |
| 2937 | rc = ethtool_get_one_feature(dev, useraddr, ethcmd); |
| 2938 | break; |
| 2939 | case ETHTOOL_STXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2940 | case ETHTOOL_SRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2941 | case ETHTOOL_SSG: |
| 2942 | case ETHTOOL_STSO: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2943 | case ETHTOOL_SGSO: |
| 2944 | case ETHTOOL_SGRO: |
| 2945 | rc = ethtool_set_one_feature(dev, useraddr, ethcmd); |
| 2946 | break; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 2947 | case ETHTOOL_GCHANNELS: |
| 2948 | rc = ethtool_get_channels(dev, useraddr); |
| 2949 | break; |
| 2950 | case ETHTOOL_SCHANNELS: |
| 2951 | rc = ethtool_set_channels(dev, useraddr); |
| 2952 | break; |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2953 | case ETHTOOL_SET_DUMP: |
| 2954 | rc = ethtool_set_dump(dev, useraddr); |
| 2955 | break; |
| 2956 | case ETHTOOL_GET_DUMP_FLAG: |
| 2957 | rc = ethtool_get_dump_flag(dev, useraddr); |
| 2958 | break; |
| 2959 | case ETHTOOL_GET_DUMP_DATA: |
| 2960 | rc = ethtool_get_dump_data(dev, useraddr); |
| 2961 | break; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2962 | case ETHTOOL_GET_TS_INFO: |
| 2963 | rc = ethtool_get_ts_info(dev, useraddr); |
| 2964 | break; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2965 | case ETHTOOL_GMODULEINFO: |
| 2966 | rc = ethtool_get_module_info(dev, useraddr); |
| 2967 | break; |
| 2968 | case ETHTOOL_GMODULEEEPROM: |
| 2969 | rc = ethtool_get_module_eeprom(dev, useraddr); |
| 2970 | break; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2971 | case ETHTOOL_GTUNABLE: |
| 2972 | rc = ethtool_get_tunable(dev, useraddr); |
| 2973 | break; |
| 2974 | case ETHTOOL_STUNABLE: |
| 2975 | rc = ethtool_set_tunable(dev, useraddr); |
| 2976 | break; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2977 | case ETHTOOL_GPHYSTATS: |
| 2978 | rc = ethtool_get_phy_stats(dev, useraddr); |
| 2979 | break; |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2980 | case ETHTOOL_PERQUEUE: |
Wenwen Wang | 58f5bbe | 2018-10-08 10:49:35 -0500 | [diff] [blame] | 2981 | rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2982 | break; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 2983 | case ETHTOOL_GLINKSETTINGS: |
| 2984 | rc = ethtool_get_link_ksettings(dev, useraddr); |
| 2985 | break; |
| 2986 | case ETHTOOL_SLINKSETTINGS: |
| 2987 | rc = ethtool_set_link_ksettings(dev, useraddr); |
| 2988 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2989 | case ETHTOOL_PHY_GTUNABLE: |
| 2990 | rc = get_phy_tunable(dev, useraddr); |
| 2991 | break; |
| 2992 | case ETHTOOL_PHY_STUNABLE: |
| 2993 | rc = set_phy_tunable(dev, useraddr); |
| 2994 | break; |
Vidya Sagar Ravipati | 1a5f3da | 2017-07-27 16:47:26 -0700 | [diff] [blame] | 2995 | case ETHTOOL_GFECPARAM: |
| 2996 | rc = ethtool_get_fecparam(dev, useraddr); |
| 2997 | break; |
| 2998 | case ETHTOOL_SFECPARAM: |
| 2999 | rc = ethtool_set_fecparam(dev, useraddr); |
| 3000 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | default: |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 3002 | rc = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3004 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 3005 | if (dev->ethtool_ops->complete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | dev->ethtool_ops->complete(dev); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 3007 | |
| 3008 | if (old_features != dev->features) |
| 3009 | netdev_features_change(dev); |
Heiner Kallweit | f32a213 | 2021-08-01 12:36:48 +0200 | [diff] [blame] | 3010 | out: |
| 3011 | if (dev->dev.parent) |
| 3012 | pm_runtime_put(dev->dev.parent); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 3013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | } |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3016 | |
Jakub Kicinski | f49deaa | 2021-10-30 10:18:48 -0700 | [diff] [blame] | 3017 | int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) |
| 3018 | { |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3019 | struct ethtool_devlink_compat *state; |
| 3020 | u32 ethcmd; |
Jakub Kicinski | f49deaa | 2021-10-30 10:18:48 -0700 | [diff] [blame] | 3021 | int rc; |
| 3022 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3023 | if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) |
| 3024 | return -EFAULT; |
Jakub Kicinski | f49deaa | 2021-10-30 10:18:48 -0700 | [diff] [blame] | 3025 | |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3026 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 3027 | if (!state) |
| 3028 | return -ENOMEM; |
| 3029 | |
| 3030 | switch (ethcmd) { |
| 3031 | case ETHTOOL_FLASHDEV: |
| 3032 | if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) { |
| 3033 | rc = -EFAULT; |
| 3034 | goto exit_free; |
| 3035 | } |
| 3036 | state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; |
| 3037 | break; |
| 3038 | } |
| 3039 | |
| 3040 | rtnl_lock(); |
| 3041 | rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state); |
| 3042 | rtnl_unlock(); |
| 3043 | if (rc) |
| 3044 | goto exit_free; |
| 3045 | |
| 3046 | switch (ethcmd) { |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 3047 | case ETHTOOL_FLASHDEV: |
| 3048 | if (state->devlink) |
| 3049 | rc = devlink_compat_flash_update(state->devlink, |
| 3050 | state->efl.data); |
| 3051 | break; |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3052 | case ETHTOOL_GDRVINFO: |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 3053 | if (state->devlink) |
| 3054 | devlink_compat_running_version(state->devlink, |
| 3055 | state->info.fw_version, |
| 3056 | sizeof(state->info.fw_version)); |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3057 | if (copy_to_user(useraddr, &state->info, sizeof(state->info))) { |
| 3058 | rc = -EFAULT; |
| 3059 | goto exit_free; |
| 3060 | } |
| 3061 | break; |
| 3062 | } |
| 3063 | |
| 3064 | exit_free: |
Jakub Kicinski | 1af0a09 | 2021-10-30 10:18:51 -0700 | [diff] [blame] | 3065 | if (state->devlink) |
| 3066 | devlink_put(state->devlink); |
Jakub Kicinski | 095cfcf | 2021-10-30 10:18:49 -0700 | [diff] [blame] | 3067 | kfree(state); |
Jakub Kicinski | f49deaa | 2021-10-30 10:18:48 -0700 | [diff] [blame] | 3068 | return rc; |
| 3069 | } |
| 3070 | |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3071 | struct ethtool_rx_flow_key { |
| 3072 | struct flow_dissector_key_basic basic; |
| 3073 | union { |
| 3074 | struct flow_dissector_key_ipv4_addrs ipv4; |
| 3075 | struct flow_dissector_key_ipv6_addrs ipv6; |
| 3076 | }; |
| 3077 | struct flow_dissector_key_ports tp; |
| 3078 | struct flow_dissector_key_ip ip; |
| 3079 | struct flow_dissector_key_vlan vlan; |
| 3080 | struct flow_dissector_key_eth_addrs eth_addrs; |
| 3081 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
| 3082 | |
| 3083 | struct ethtool_rx_flow_match { |
| 3084 | struct flow_dissector dissector; |
| 3085 | struct ethtool_rx_flow_key key; |
| 3086 | struct ethtool_rx_flow_key mask; |
| 3087 | }; |
| 3088 | |
| 3089 | struct ethtool_rx_flow_rule * |
| 3090 | ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) |
| 3091 | { |
| 3092 | const struct ethtool_rx_flow_spec *fs = input->fs; |
| 3093 | static struct in6_addr zero_addr = {}; |
| 3094 | struct ethtool_rx_flow_match *match; |
| 3095 | struct ethtool_rx_flow_rule *flow; |
| 3096 | struct flow_action_entry *act; |
| 3097 | |
| 3098 | flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + |
| 3099 | sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); |
| 3100 | if (!flow) |
| 3101 | return ERR_PTR(-ENOMEM); |
| 3102 | |
| 3103 | /* ethtool_rx supports only one single action per rule. */ |
| 3104 | flow->rule = flow_rule_alloc(1); |
| 3105 | if (!flow->rule) { |
| 3106 | kfree(flow); |
| 3107 | return ERR_PTR(-ENOMEM); |
| 3108 | } |
| 3109 | |
| 3110 | match = (struct ethtool_rx_flow_match *)flow->priv; |
| 3111 | flow->rule->match.dissector = &match->dissector; |
| 3112 | flow->rule->match.mask = &match->mask; |
| 3113 | flow->rule->match.key = &match->key; |
| 3114 | |
| 3115 | match->mask.basic.n_proto = htons(0xffff); |
| 3116 | |
| 3117 | switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { |
Maxime Chevallier | 5b9469a | 2019-06-27 10:52:26 +0200 | [diff] [blame] | 3118 | case ETHER_FLOW: { |
| 3119 | const struct ethhdr *ether_spec, *ether_m_spec; |
| 3120 | |
| 3121 | ether_spec = &fs->h_u.ether_spec; |
| 3122 | ether_m_spec = &fs->m_u.ether_spec; |
| 3123 | |
| 3124 | if (!is_zero_ether_addr(ether_m_spec->h_source)) { |
| 3125 | ether_addr_copy(match->key.eth_addrs.src, |
| 3126 | ether_spec->h_source); |
| 3127 | ether_addr_copy(match->mask.eth_addrs.src, |
| 3128 | ether_m_spec->h_source); |
| 3129 | } |
| 3130 | if (!is_zero_ether_addr(ether_m_spec->h_dest)) { |
| 3131 | ether_addr_copy(match->key.eth_addrs.dst, |
| 3132 | ether_spec->h_dest); |
| 3133 | ether_addr_copy(match->mask.eth_addrs.dst, |
| 3134 | ether_m_spec->h_dest); |
| 3135 | } |
| 3136 | if (ether_m_spec->h_proto) { |
| 3137 | match->key.basic.n_proto = ether_spec->h_proto; |
| 3138 | match->mask.basic.n_proto = ether_m_spec->h_proto; |
| 3139 | } |
| 3140 | } |
| 3141 | break; |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3142 | case TCP_V4_FLOW: |
| 3143 | case UDP_V4_FLOW: { |
| 3144 | const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; |
| 3145 | |
| 3146 | match->key.basic.n_proto = htons(ETH_P_IP); |
| 3147 | |
| 3148 | v4_spec = &fs->h_u.tcp_ip4_spec; |
| 3149 | v4_m_spec = &fs->m_u.tcp_ip4_spec; |
| 3150 | |
| 3151 | if (v4_m_spec->ip4src) { |
| 3152 | match->key.ipv4.src = v4_spec->ip4src; |
| 3153 | match->mask.ipv4.src = v4_m_spec->ip4src; |
| 3154 | } |
| 3155 | if (v4_m_spec->ip4dst) { |
| 3156 | match->key.ipv4.dst = v4_spec->ip4dst; |
| 3157 | match->mask.ipv4.dst = v4_m_spec->ip4dst; |
| 3158 | } |
| 3159 | if (v4_m_spec->ip4src || |
| 3160 | v4_m_spec->ip4dst) { |
| 3161 | match->dissector.used_keys |= |
| 3162 | BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS); |
| 3163 | match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = |
| 3164 | offsetof(struct ethtool_rx_flow_key, ipv4); |
| 3165 | } |
| 3166 | if (v4_m_spec->psrc) { |
| 3167 | match->key.tp.src = v4_spec->psrc; |
| 3168 | match->mask.tp.src = v4_m_spec->psrc; |
| 3169 | } |
| 3170 | if (v4_m_spec->pdst) { |
| 3171 | match->key.tp.dst = v4_spec->pdst; |
| 3172 | match->mask.tp.dst = v4_m_spec->pdst; |
| 3173 | } |
| 3174 | if (v4_m_spec->psrc || |
| 3175 | v4_m_spec->pdst) { |
| 3176 | match->dissector.used_keys |= |
| 3177 | BIT(FLOW_DISSECTOR_KEY_PORTS); |
| 3178 | match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = |
| 3179 | offsetof(struct ethtool_rx_flow_key, tp); |
| 3180 | } |
| 3181 | if (v4_m_spec->tos) { |
| 3182 | match->key.ip.tos = v4_spec->tos; |
| 3183 | match->mask.ip.tos = v4_m_spec->tos; |
| 3184 | match->dissector.used_keys |= |
| 3185 | BIT(FLOW_DISSECTOR_KEY_IP); |
| 3186 | match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = |
| 3187 | offsetof(struct ethtool_rx_flow_key, ip); |
| 3188 | } |
| 3189 | } |
| 3190 | break; |
| 3191 | case TCP_V6_FLOW: |
| 3192 | case UDP_V6_FLOW: { |
| 3193 | const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; |
| 3194 | |
| 3195 | match->key.basic.n_proto = htons(ETH_P_IPV6); |
| 3196 | |
| 3197 | v6_spec = &fs->h_u.tcp_ip6_spec; |
| 3198 | v6_m_spec = &fs->m_u.tcp_ip6_spec; |
| 3199 | if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) { |
| 3200 | memcpy(&match->key.ipv6.src, v6_spec->ip6src, |
| 3201 | sizeof(match->key.ipv6.src)); |
| 3202 | memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, |
| 3203 | sizeof(match->mask.ipv6.src)); |
| 3204 | } |
| 3205 | if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { |
| 3206 | memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, |
| 3207 | sizeof(match->key.ipv6.dst)); |
| 3208 | memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, |
| 3209 | sizeof(match->mask.ipv6.dst)); |
| 3210 | } |
| 3211 | if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) || |
Gaurav Singh | 21a739c | 2020-06-21 11:30:17 -0400 | [diff] [blame] | 3212 | memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3213 | match->dissector.used_keys |= |
| 3214 | BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS); |
| 3215 | match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = |
| 3216 | offsetof(struct ethtool_rx_flow_key, ipv6); |
| 3217 | } |
| 3218 | if (v6_m_spec->psrc) { |
| 3219 | match->key.tp.src = v6_spec->psrc; |
| 3220 | match->mask.tp.src = v6_m_spec->psrc; |
| 3221 | } |
| 3222 | if (v6_m_spec->pdst) { |
| 3223 | match->key.tp.dst = v6_spec->pdst; |
| 3224 | match->mask.tp.dst = v6_m_spec->pdst; |
| 3225 | } |
| 3226 | if (v6_m_spec->psrc || |
| 3227 | v6_m_spec->pdst) { |
| 3228 | match->dissector.used_keys |= |
| 3229 | BIT(FLOW_DISSECTOR_KEY_PORTS); |
| 3230 | match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = |
| 3231 | offsetof(struct ethtool_rx_flow_key, tp); |
| 3232 | } |
| 3233 | if (v6_m_spec->tclass) { |
| 3234 | match->key.ip.tos = v6_spec->tclass; |
| 3235 | match->mask.ip.tos = v6_m_spec->tclass; |
| 3236 | match->dissector.used_keys |= |
| 3237 | BIT(FLOW_DISSECTOR_KEY_IP); |
| 3238 | match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = |
| 3239 | offsetof(struct ethtool_rx_flow_key, ip); |
| 3240 | } |
| 3241 | } |
| 3242 | break; |
| 3243 | default: |
| 3244 | ethtool_rx_flow_rule_destroy(flow); |
| 3245 | return ERR_PTR(-EINVAL); |
| 3246 | } |
| 3247 | |
| 3248 | switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { |
| 3249 | case TCP_V4_FLOW: |
| 3250 | case TCP_V6_FLOW: |
| 3251 | match->key.basic.ip_proto = IPPROTO_TCP; |
Vishal Kulkarni | d0a84e1 | 2020-08-19 00:25:03 +0530 | [diff] [blame] | 3252 | match->mask.basic.ip_proto = 0xff; |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3253 | break; |
| 3254 | case UDP_V4_FLOW: |
| 3255 | case UDP_V6_FLOW: |
| 3256 | match->key.basic.ip_proto = IPPROTO_UDP; |
Vishal Kulkarni | d0a84e1 | 2020-08-19 00:25:03 +0530 | [diff] [blame] | 3257 | match->mask.basic.ip_proto = 0xff; |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3258 | break; |
| 3259 | } |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3260 | |
| 3261 | match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC); |
| 3262 | match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = |
| 3263 | offsetof(struct ethtool_rx_flow_key, basic); |
| 3264 | |
| 3265 | if (fs->flow_type & FLOW_EXT) { |
| 3266 | const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; |
| 3267 | const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; |
| 3268 | |
Maxime Chevallier | b73484b | 2019-05-30 16:08:40 +0200 | [diff] [blame] | 3269 | if (ext_m_spec->vlan_etype) { |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3270 | match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; |
| 3271 | match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; |
Maxime Chevallier | b73484b | 2019-05-30 16:08:40 +0200 | [diff] [blame] | 3272 | } |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3273 | |
Maxime Chevallier | b73484b | 2019-05-30 16:08:40 +0200 | [diff] [blame] | 3274 | if (ext_m_spec->vlan_tci) { |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3275 | match->key.vlan.vlan_id = |
| 3276 | ntohs(ext_h_spec->vlan_tci) & 0x0fff; |
| 3277 | match->mask.vlan.vlan_id = |
| 3278 | ntohs(ext_m_spec->vlan_tci) & 0x0fff; |
| 3279 | |
Maxime Chevallier | f0d2ca1 | 2019-06-12 17:18:38 +0200 | [diff] [blame] | 3280 | match->key.vlan.vlan_dei = |
| 3281 | !!(ext_h_spec->vlan_tci & htons(0x1000)); |
| 3282 | match->mask.vlan.vlan_dei = |
| 3283 | !!(ext_m_spec->vlan_tci & htons(0x1000)); |
| 3284 | |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3285 | match->key.vlan.vlan_priority = |
| 3286 | (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; |
| 3287 | match->mask.vlan.vlan_priority = |
| 3288 | (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; |
Maxime Chevallier | b73484b | 2019-05-30 16:08:40 +0200 | [diff] [blame] | 3289 | } |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3290 | |
Maxime Chevallier | b73484b | 2019-05-30 16:08:40 +0200 | [diff] [blame] | 3291 | if (ext_m_spec->vlan_etype || |
| 3292 | ext_m_spec->vlan_tci) { |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3293 | match->dissector.used_keys |= |
| 3294 | BIT(FLOW_DISSECTOR_KEY_VLAN); |
| 3295 | match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = |
| 3296 | offsetof(struct ethtool_rx_flow_key, vlan); |
| 3297 | } |
| 3298 | } |
| 3299 | if (fs->flow_type & FLOW_MAC_EXT) { |
| 3300 | const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; |
| 3301 | const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; |
| 3302 | |
Nathan Chancellor | 8b34ec6 | 2019-02-07 21:46:53 -0700 | [diff] [blame] | 3303 | memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, |
| 3304 | ETH_ALEN); |
| 3305 | memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, |
| 3306 | ETH_ALEN); |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3307 | |
Nathan Chancellor | 8b34ec6 | 2019-02-07 21:46:53 -0700 | [diff] [blame] | 3308 | match->dissector.used_keys |= |
| 3309 | BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS); |
| 3310 | match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = |
| 3311 | offsetof(struct ethtool_rx_flow_key, eth_addrs); |
Pablo Neira Ayuso | eca4205 | 2019-02-02 12:50:51 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | act = &flow->rule->action.entries[0]; |
| 3315 | switch (fs->ring_cookie) { |
| 3316 | case RX_CLS_FLOW_DISC: |
| 3317 | act->id = FLOW_ACTION_DROP; |
| 3318 | break; |
| 3319 | case RX_CLS_FLOW_WAKE: |
| 3320 | act->id = FLOW_ACTION_WAKE; |
| 3321 | break; |
| 3322 | default: |
| 3323 | act->id = FLOW_ACTION_QUEUE; |
| 3324 | if (fs->flow_type & FLOW_RSS) |
| 3325 | act->queue.ctx = input->rss_ctx; |
| 3326 | |
| 3327 | act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); |
| 3328 | act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); |
| 3329 | break; |
| 3330 | } |
| 3331 | |
| 3332 | return flow; |
| 3333 | } |
| 3334 | EXPORT_SYMBOL(ethtool_rx_flow_rule_create); |
| 3335 | |
| 3336 | void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) |
| 3337 | { |
| 3338 | kfree(flow->rule); |
| 3339 | kfree(flow); |
| 3340 | } |
| 3341 | EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); |