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