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