0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/compat.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 #include <linux/capability.h>
0015 #include <linux/errno.h>
0016 #include <linux/ethtool.h>
0017 #include <linux/netdevice.h>
0018 #include <linux/net_tstamp.h>
0019 #include <linux/phy.h>
0020 #include <linux/bitops.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/vmalloc.h>
0023 #include <linux/sfp.h>
0024 #include <linux/slab.h>
0025 #include <linux/rtnetlink.h>
0026 #include <linux/sched/signal.h>
0027 #include <linux/net.h>
0028 #include <linux/pm_runtime.h>
0029 #include <net/devlink.h>
0030 #include <net/xdp_sock_drv.h>
0031 #include <net/flow_offload.h>
0032 #include <linux/ethtool_netlink.h>
0033 #include <generated/utsrelease.h>
0034 #include "common.h"
0035
0036
0037 struct ethtool_devlink_compat {
0038 struct devlink *devlink;
0039 union {
0040 struct ethtool_flash efl;
0041 struct ethtool_drvinfo info;
0042 };
0043 };
0044
0045 static struct devlink *netdev_to_devlink_get(struct net_device *dev)
0046 {
0047 struct devlink_port *devlink_port;
0048
0049 if (!dev->netdev_ops->ndo_get_devlink_port)
0050 return NULL;
0051
0052 devlink_port = dev->netdev_ops->ndo_get_devlink_port(dev);
0053 if (!devlink_port)
0054 return NULL;
0055
0056 return devlink_try_get(devlink_port->devlink);
0057 }
0058
0059
0060
0061
0062
0063
0064
0065 u32 ethtool_op_get_link(struct net_device *dev)
0066 {
0067 return netif_carrier_ok(dev) ? 1 : 0;
0068 }
0069 EXPORT_SYMBOL(ethtool_op_get_link);
0070
0071 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
0072 {
0073 info->so_timestamping =
0074 SOF_TIMESTAMPING_TX_SOFTWARE |
0075 SOF_TIMESTAMPING_RX_SOFTWARE |
0076 SOF_TIMESTAMPING_SOFTWARE;
0077 info->phc_index = -1;
0078 return 0;
0079 }
0080 EXPORT_SYMBOL(ethtool_op_get_ts_info);
0081
0082
0083
0084 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
0085 {
0086 struct ethtool_gfeatures cmd = {
0087 .cmd = ETHTOOL_GFEATURES,
0088 .size = ETHTOOL_DEV_FEATURE_WORDS,
0089 };
0090 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
0091 u32 __user *sizeaddr;
0092 u32 copy_size;
0093 int i;
0094
0095
0096 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
0097
0098 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
0099 features[i].available = (u32)(dev->hw_features >> (32 * i));
0100 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
0101 features[i].active = (u32)(dev->features >> (32 * i));
0102 features[i].never_changed =
0103 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
0104 }
0105
0106 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
0107 if (get_user(copy_size, sizeaddr))
0108 return -EFAULT;
0109
0110 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
0111 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
0112
0113 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
0114 return -EFAULT;
0115 useraddr += sizeof(cmd);
0116 if (copy_to_user(useraddr, features,
0117 array_size(copy_size, sizeof(*features))))
0118 return -EFAULT;
0119
0120 return 0;
0121 }
0122
0123 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
0124 {
0125 struct ethtool_sfeatures cmd;
0126 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
0127 netdev_features_t wanted = 0, valid = 0;
0128 int i, ret = 0;
0129
0130 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
0131 return -EFAULT;
0132 useraddr += sizeof(cmd);
0133
0134 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
0135 return -EINVAL;
0136
0137 if (copy_from_user(features, useraddr, sizeof(features)))
0138 return -EFAULT;
0139
0140 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
0141 valid |= (netdev_features_t)features[i].valid << (32 * i);
0142 wanted |= (netdev_features_t)features[i].requested << (32 * i);
0143 }
0144
0145 if (valid & ~NETIF_F_ETHTOOL_BITS)
0146 return -EINVAL;
0147
0148 if (valid & ~dev->hw_features) {
0149 valid &= dev->hw_features;
0150 ret |= ETHTOOL_F_UNSUPPORTED;
0151 }
0152
0153 dev->wanted_features &= ~valid;
0154 dev->wanted_features |= wanted & valid;
0155 __netdev_update_features(dev);
0156
0157 if ((dev->wanted_features ^ dev->features) & valid)
0158 ret |= ETHTOOL_F_WISH;
0159
0160 return ret;
0161 }
0162
0163 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
0164 {
0165 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
0166 const struct ethtool_ops *ops = dev->ethtool_ops;
0167
0168 if (sset == ETH_SS_FEATURES)
0169 return ARRAY_SIZE(netdev_features_strings);
0170
0171 if (sset == ETH_SS_RSS_HASH_FUNCS)
0172 return ARRAY_SIZE(rss_hash_func_strings);
0173
0174 if (sset == ETH_SS_TUNABLES)
0175 return ARRAY_SIZE(tunable_strings);
0176
0177 if (sset == ETH_SS_PHY_TUNABLES)
0178 return ARRAY_SIZE(phy_tunable_strings);
0179
0180 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
0181 !ops->get_ethtool_phy_stats &&
0182 phy_ops && phy_ops->get_sset_count)
0183 return phy_ops->get_sset_count(dev->phydev);
0184
0185 if (sset == ETH_SS_LINK_MODES)
0186 return __ETHTOOL_LINK_MODE_MASK_NBITS;
0187
0188 if (ops->get_sset_count && ops->get_strings)
0189 return ops->get_sset_count(dev, sset);
0190 else
0191 return -EOPNOTSUPP;
0192 }
0193
0194 static void __ethtool_get_strings(struct net_device *dev,
0195 u32 stringset, u8 *data)
0196 {
0197 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
0198 const struct ethtool_ops *ops = dev->ethtool_ops;
0199
0200 if (stringset == ETH_SS_FEATURES)
0201 memcpy(data, netdev_features_strings,
0202 sizeof(netdev_features_strings));
0203 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
0204 memcpy(data, rss_hash_func_strings,
0205 sizeof(rss_hash_func_strings));
0206 else if (stringset == ETH_SS_TUNABLES)
0207 memcpy(data, tunable_strings, sizeof(tunable_strings));
0208 else if (stringset == ETH_SS_PHY_TUNABLES)
0209 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
0210 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
0211 !ops->get_ethtool_phy_stats && phy_ops &&
0212 phy_ops->get_strings)
0213 phy_ops->get_strings(dev->phydev, data);
0214 else if (stringset == ETH_SS_LINK_MODES)
0215 memcpy(data, link_mode_names,
0216 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
0217 else
0218
0219 ops->get_strings(dev, stringset, data);
0220 }
0221
0222 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0223 {
0224
0225
0226 switch (eth_cmd) {
0227 case ETHTOOL_GTXCSUM:
0228 case ETHTOOL_STXCSUM:
0229 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
0230 NETIF_F_SCTP_CRC;
0231 case ETHTOOL_GRXCSUM:
0232 case ETHTOOL_SRXCSUM:
0233 return NETIF_F_RXCSUM;
0234 case ETHTOOL_GSG:
0235 case ETHTOOL_SSG:
0236 return NETIF_F_SG | NETIF_F_FRAGLIST;
0237 case ETHTOOL_GTSO:
0238 case ETHTOOL_STSO:
0239 return NETIF_F_ALL_TSO;
0240 case ETHTOOL_GGSO:
0241 case ETHTOOL_SGSO:
0242 return NETIF_F_GSO;
0243 case ETHTOOL_GGRO:
0244 case ETHTOOL_SGRO:
0245 return NETIF_F_GRO;
0246 default:
0247 BUG();
0248 }
0249 }
0250
0251 static int ethtool_get_one_feature(struct net_device *dev,
0252 char __user *useraddr, u32 ethcmd)
0253 {
0254 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0255 struct ethtool_value edata = {
0256 .cmd = ethcmd,
0257 .data = !!(dev->features & mask),
0258 };
0259
0260 if (copy_to_user(useraddr, &edata, sizeof(edata)))
0261 return -EFAULT;
0262 return 0;
0263 }
0264
0265 static int ethtool_set_one_feature(struct net_device *dev,
0266 void __user *useraddr, u32 ethcmd)
0267 {
0268 struct ethtool_value edata;
0269 netdev_features_t mask;
0270
0271 if (copy_from_user(&edata, useraddr, sizeof(edata)))
0272 return -EFAULT;
0273
0274 mask = ethtool_get_feature_mask(ethcmd);
0275 mask &= dev->hw_features;
0276 if (!mask)
0277 return -EOPNOTSUPP;
0278
0279 if (edata.data)
0280 dev->wanted_features |= mask;
0281 else
0282 dev->wanted_features &= ~mask;
0283
0284 __netdev_update_features(dev);
0285
0286 return 0;
0287 }
0288
0289 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
0290 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
0291 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
0292 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
0293 NETIF_F_RXHASH)
0294
0295 static u32 __ethtool_get_flags(struct net_device *dev)
0296 {
0297 u32 flags = 0;
0298
0299 if (dev->features & NETIF_F_LRO)
0300 flags |= ETH_FLAG_LRO;
0301 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
0302 flags |= ETH_FLAG_RXVLAN;
0303 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
0304 flags |= ETH_FLAG_TXVLAN;
0305 if (dev->features & NETIF_F_NTUPLE)
0306 flags |= ETH_FLAG_NTUPLE;
0307 if (dev->features & NETIF_F_RXHASH)
0308 flags |= ETH_FLAG_RXHASH;
0309
0310 return flags;
0311 }
0312
0313 static int __ethtool_set_flags(struct net_device *dev, u32 data)
0314 {
0315 netdev_features_t features = 0, changed;
0316
0317 if (data & ~ETH_ALL_FLAGS)
0318 return -EINVAL;
0319
0320 if (data & ETH_FLAG_LRO)
0321 features |= NETIF_F_LRO;
0322 if (data & ETH_FLAG_RXVLAN)
0323 features |= NETIF_F_HW_VLAN_CTAG_RX;
0324 if (data & ETH_FLAG_TXVLAN)
0325 features |= NETIF_F_HW_VLAN_CTAG_TX;
0326 if (data & ETH_FLAG_NTUPLE)
0327 features |= NETIF_F_NTUPLE;
0328 if (data & ETH_FLAG_RXHASH)
0329 features |= NETIF_F_RXHASH;
0330
0331
0332 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
0333 if (changed & ~dev->hw_features)
0334 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
0335
0336 dev->wanted_features =
0337 (dev->wanted_features & ~changed) | (features & changed);
0338
0339 __netdev_update_features(dev);
0340
0341 return 0;
0342 }
0343
0344
0345 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
0346 struct ethtool_link_ksettings *src)
0347 {
0348 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
0349 unsigned int idx = 0;
0350
0351 for (; idx < size; idx++) {
0352 dst->link_modes.supported[idx] &=
0353 src->link_modes.supported[idx];
0354 dst->link_modes.advertising[idx] &=
0355 src->link_modes.advertising[idx];
0356 }
0357 }
0358 EXPORT_SYMBOL(ethtool_intersect_link_masks);
0359
0360 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
0361 u32 legacy_u32)
0362 {
0363 linkmode_zero(dst);
0364 dst[0] = legacy_u32;
0365 }
0366 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
0367
0368
0369 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
0370 const unsigned long *src)
0371 {
0372 *legacy_u32 = src[0];
0373 return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
0374 __ETHTOOL_LINK_MODE_MASK_NBITS;
0375 }
0376 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
0377
0378
0379
0380
0381 static bool
0382 convert_link_ksettings_to_legacy_settings(
0383 struct ethtool_cmd *legacy_settings,
0384 const struct ethtool_link_ksettings *link_ksettings)
0385 {
0386 bool retval = true;
0387
0388 memset(legacy_settings, 0, sizeof(*legacy_settings));
0389
0390
0391
0392
0393
0394
0395 retval &= ethtool_convert_link_mode_to_legacy_u32(
0396 &legacy_settings->supported,
0397 link_ksettings->link_modes.supported);
0398 retval &= ethtool_convert_link_mode_to_legacy_u32(
0399 &legacy_settings->advertising,
0400 link_ksettings->link_modes.advertising);
0401 retval &= ethtool_convert_link_mode_to_legacy_u32(
0402 &legacy_settings->lp_advertising,
0403 link_ksettings->link_modes.lp_advertising);
0404 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
0405 legacy_settings->duplex
0406 = link_ksettings->base.duplex;
0407 legacy_settings->port
0408 = link_ksettings->base.port;
0409 legacy_settings->phy_address
0410 = link_ksettings->base.phy_address;
0411 legacy_settings->autoneg
0412 = link_ksettings->base.autoneg;
0413 legacy_settings->mdio_support
0414 = link_ksettings->base.mdio_support;
0415 legacy_settings->eth_tp_mdix
0416 = link_ksettings->base.eth_tp_mdix;
0417 legacy_settings->eth_tp_mdix_ctrl
0418 = link_ksettings->base.eth_tp_mdix_ctrl;
0419 legacy_settings->transceiver
0420 = link_ksettings->base.transceiver;
0421 return retval;
0422 }
0423
0424
0425 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
0426 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
0427
0428
0429 struct ethtool_link_usettings {
0430 struct ethtool_link_settings base;
0431 struct {
0432 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
0433 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
0434 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
0435 } link_modes;
0436 };
0437
0438
0439 int __ethtool_get_link_ksettings(struct net_device *dev,
0440 struct ethtool_link_ksettings *link_ksettings)
0441 {
0442 ASSERT_RTNL();
0443
0444 if (!dev->ethtool_ops->get_link_ksettings)
0445 return -EOPNOTSUPP;
0446
0447 memset(link_ksettings, 0, sizeof(*link_ksettings));
0448 return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
0449 }
0450 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
0451
0452
0453
0454
0455 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
0456 const void __user *from)
0457 {
0458 struct ethtool_link_usettings link_usettings;
0459
0460 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
0461 return -EFAULT;
0462
0463 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
0464 bitmap_from_arr32(to->link_modes.supported,
0465 link_usettings.link_modes.supported,
0466 __ETHTOOL_LINK_MODE_MASK_NBITS);
0467 bitmap_from_arr32(to->link_modes.advertising,
0468 link_usettings.link_modes.advertising,
0469 __ETHTOOL_LINK_MODE_MASK_NBITS);
0470 bitmap_from_arr32(to->link_modes.lp_advertising,
0471 link_usettings.link_modes.lp_advertising,
0472 __ETHTOOL_LINK_MODE_MASK_NBITS);
0473
0474 return 0;
0475 }
0476
0477
0478 bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
0479 {
0480 struct ethtool_link_settings base2 = {};
0481
0482 base2.speed = cmd->base.speed;
0483 base2.port = PORT_OTHER;
0484 base2.duplex = cmd->base.duplex;
0485 base2.cmd = cmd->base.cmd;
0486 base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
0487
0488 return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
0489 bitmap_empty(cmd->link_modes.supported,
0490 __ETHTOOL_LINK_MODE_MASK_NBITS) &&
0491 bitmap_empty(cmd->link_modes.lp_advertising,
0492 __ETHTOOL_LINK_MODE_MASK_NBITS);
0493 }
0494
0495
0496
0497
0498
0499 static int
0500 store_link_ksettings_for_user(void __user *to,
0501 const struct ethtool_link_ksettings *from)
0502 {
0503 struct ethtool_link_usettings link_usettings;
0504
0505 memcpy(&link_usettings, from, sizeof(link_usettings));
0506 bitmap_to_arr32(link_usettings.link_modes.supported,
0507 from->link_modes.supported,
0508 __ETHTOOL_LINK_MODE_MASK_NBITS);
0509 bitmap_to_arr32(link_usettings.link_modes.advertising,
0510 from->link_modes.advertising,
0511 __ETHTOOL_LINK_MODE_MASK_NBITS);
0512 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
0513 from->link_modes.lp_advertising,
0514 __ETHTOOL_LINK_MODE_MASK_NBITS);
0515
0516 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
0517 return -EFAULT;
0518
0519 return 0;
0520 }
0521
0522
0523 static int ethtool_get_link_ksettings(struct net_device *dev,
0524 void __user *useraddr)
0525 {
0526 int err = 0;
0527 struct ethtool_link_ksettings link_ksettings;
0528
0529 ASSERT_RTNL();
0530 if (!dev->ethtool_ops->get_link_ksettings)
0531 return -EOPNOTSUPP;
0532
0533
0534 if (copy_from_user(&link_ksettings.base, useraddr,
0535 sizeof(link_ksettings.base)))
0536 return -EFAULT;
0537
0538 if (__ETHTOOL_LINK_MODE_MASK_NU32
0539 != link_ksettings.base.link_mode_masks_nwords) {
0540
0541 memset(&link_ksettings, 0, sizeof(link_ksettings));
0542 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
0543
0544 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
0545 "need too many bits for link modes!");
0546 link_ksettings.base.link_mode_masks_nwords
0547 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
0548
0549
0550
0551
0552 if (copy_to_user(useraddr, &link_ksettings.base,
0553 sizeof(link_ksettings.base)))
0554 return -EFAULT;
0555
0556 return 0;
0557 }
0558
0559
0560
0561
0562
0563 memset(&link_ksettings, 0, sizeof(link_ksettings));
0564 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
0565 if (err < 0)
0566 return err;
0567
0568
0569 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
0570 link_ksettings.base.link_mode_masks_nwords
0571 = __ETHTOOL_LINK_MODE_MASK_NU32;
0572 link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
0573 link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
0574
0575 return store_link_ksettings_for_user(useraddr, &link_ksettings);
0576 }
0577
0578
0579 static int ethtool_set_link_ksettings(struct net_device *dev,
0580 void __user *useraddr)
0581 {
0582 int err;
0583 struct ethtool_link_ksettings link_ksettings;
0584
0585 ASSERT_RTNL();
0586
0587 if (!dev->ethtool_ops->set_link_ksettings)
0588 return -EOPNOTSUPP;
0589
0590
0591 if (copy_from_user(&link_ksettings.base, useraddr,
0592 sizeof(link_ksettings.base)))
0593 return -EFAULT;
0594
0595 if (__ETHTOOL_LINK_MODE_MASK_NU32
0596 != link_ksettings.base.link_mode_masks_nwords)
0597 return -EINVAL;
0598
0599
0600
0601
0602 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
0603 if (err)
0604 return err;
0605
0606
0607 if (__ETHTOOL_LINK_MODE_MASK_NU32
0608 != link_ksettings.base.link_mode_masks_nwords)
0609 return -EINVAL;
0610
0611 if (link_ksettings.base.master_slave_cfg ||
0612 link_ksettings.base.master_slave_state)
0613 return -EINVAL;
0614
0615 err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
0616 if (err >= 0) {
0617 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
0618 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
0619 }
0620 return err;
0621 }
0622
0623 int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
0624 const struct ethtool_link_ksettings *cmd,
0625 u32 *dev_speed, u8 *dev_duplex)
0626 {
0627 u32 speed;
0628 u8 duplex;
0629
0630 speed = cmd->base.speed;
0631 duplex = cmd->base.duplex;
0632
0633 if (!ethtool_validate_speed(speed) ||
0634 !ethtool_validate_duplex(duplex) ||
0635 !ethtool_virtdev_validate_cmd(cmd))
0636 return -EINVAL;
0637 *dev_speed = speed;
0638 *dev_duplex = duplex;
0639
0640 return 0;
0641 }
0642 EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
0654 {
0655 struct ethtool_link_ksettings link_ksettings;
0656 struct ethtool_cmd cmd;
0657 int err;
0658
0659 ASSERT_RTNL();
0660 if (!dev->ethtool_ops->get_link_ksettings)
0661 return -EOPNOTSUPP;
0662
0663 memset(&link_ksettings, 0, sizeof(link_ksettings));
0664 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
0665 if (err < 0)
0666 return err;
0667 convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
0668
0669
0670 cmd.cmd = ETHTOOL_GSET;
0671
0672 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
0673 return -EFAULT;
0674
0675 return 0;
0676 }
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
0687 {
0688 struct ethtool_link_ksettings link_ksettings;
0689 struct ethtool_cmd cmd;
0690 int ret;
0691
0692 ASSERT_RTNL();
0693
0694 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
0695 return -EFAULT;
0696 if (!dev->ethtool_ops->set_link_ksettings)
0697 return -EOPNOTSUPP;
0698
0699 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
0700 return -EINVAL;
0701 link_ksettings.base.link_mode_masks_nwords =
0702 __ETHTOOL_LINK_MODE_MASK_NU32;
0703 ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
0704 if (ret >= 0) {
0705 ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
0706 ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
0707 }
0708 return ret;
0709 }
0710
0711 static int
0712 ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
0713 {
0714 const struct ethtool_ops *ops = dev->ethtool_ops;
0715
0716 rsp->info.cmd = ETHTOOL_GDRVINFO;
0717 strlcpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
0718 if (ops->get_drvinfo) {
0719 ops->get_drvinfo(dev, &rsp->info);
0720 } else if (dev->dev.parent && dev->dev.parent->driver) {
0721 strlcpy(rsp->info.bus_info, dev_name(dev->dev.parent),
0722 sizeof(rsp->info.bus_info));
0723 strlcpy(rsp->info.driver, dev->dev.parent->driver->name,
0724 sizeof(rsp->info.driver));
0725 } else if (dev->rtnl_link_ops) {
0726 strlcpy(rsp->info.driver, dev->rtnl_link_ops->kind,
0727 sizeof(rsp->info.driver));
0728 } else {
0729 return -EOPNOTSUPP;
0730 }
0731
0732
0733
0734
0735
0736 if (ops->get_sset_count) {
0737 int rc;
0738
0739 rc = ops->get_sset_count(dev, ETH_SS_TEST);
0740 if (rc >= 0)
0741 rsp->info.testinfo_len = rc;
0742 rc = ops->get_sset_count(dev, ETH_SS_STATS);
0743 if (rc >= 0)
0744 rsp->info.n_stats = rc;
0745 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
0746 if (rc >= 0)
0747 rsp->info.n_priv_flags = rc;
0748 }
0749 if (ops->get_regs_len) {
0750 int ret = ops->get_regs_len(dev);
0751
0752 if (ret > 0)
0753 rsp->info.regdump_len = ret;
0754 }
0755
0756 if (ops->get_eeprom_len)
0757 rsp->info.eedump_len = ops->get_eeprom_len(dev);
0758
0759 if (!rsp->info.fw_version[0])
0760 rsp->devlink = netdev_to_devlink_get(dev);
0761
0762 return 0;
0763 }
0764
0765 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
0766 void __user *useraddr)
0767 {
0768 struct ethtool_sset_info info;
0769 u64 sset_mask;
0770 int i, idx = 0, n_bits = 0, ret, rc;
0771 u32 *info_buf = NULL;
0772
0773 if (copy_from_user(&info, useraddr, sizeof(info)))
0774 return -EFAULT;
0775
0776
0777 sset_mask = info.sset_mask;
0778 if (!sset_mask)
0779 return 0;
0780
0781
0782 n_bits = hweight64(sset_mask);
0783
0784 memset(&info, 0, sizeof(info));
0785 info.cmd = ETHTOOL_GSSET_INFO;
0786
0787 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
0788 if (!info_buf)
0789 return -ENOMEM;
0790
0791
0792
0793
0794
0795 for (i = 0; i < 64; i++) {
0796 if (!(sset_mask & (1ULL << i)))
0797 continue;
0798
0799 rc = __ethtool_get_sset_count(dev, i);
0800 if (rc >= 0) {
0801 info.sset_mask |= (1ULL << i);
0802 info_buf[idx++] = rc;
0803 }
0804 }
0805
0806 ret = -EFAULT;
0807 if (copy_to_user(useraddr, &info, sizeof(info)))
0808 goto out;
0809
0810 useraddr += offsetof(struct ethtool_sset_info, data);
0811 if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
0812 goto out;
0813
0814 ret = 0;
0815
0816 out:
0817 kfree(info_buf);
0818 return ret;
0819 }
0820
0821 static noinline_for_stack int
0822 ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
0823 const struct compat_ethtool_rxnfc __user *useraddr,
0824 size_t size)
0825 {
0826 struct compat_ethtool_rxnfc crxnfc = {};
0827
0828
0829
0830
0831
0832 BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
0833 sizeof(struct compat_ethtool_rxnfc) !=
0834 sizeof(struct ethtool_rxnfc));
0835 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
0836 sizeof(useraddr->fs.m_ext) !=
0837 offsetof(struct ethtool_rxnfc, fs.m_ext) +
0838 sizeof(rxnfc->fs.m_ext));
0839 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
0840 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
0841 offsetof(struct ethtool_rxnfc, fs.location) -
0842 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
0843
0844 if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
0845 return -EFAULT;
0846
0847 *rxnfc = (struct ethtool_rxnfc) {
0848 .cmd = crxnfc.cmd,
0849 .flow_type = crxnfc.flow_type,
0850 .data = crxnfc.data,
0851 .fs = {
0852 .flow_type = crxnfc.fs.flow_type,
0853 .h_u = crxnfc.fs.h_u,
0854 .h_ext = crxnfc.fs.h_ext,
0855 .m_u = crxnfc.fs.m_u,
0856 .m_ext = crxnfc.fs.m_ext,
0857 .ring_cookie = crxnfc.fs.ring_cookie,
0858 .location = crxnfc.fs.location,
0859 },
0860 .rule_cnt = crxnfc.rule_cnt,
0861 };
0862
0863 return 0;
0864 }
0865
0866 static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
0867 const void __user *useraddr,
0868 size_t size)
0869 {
0870 if (compat_need_64bit_alignment_fixup())
0871 return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
0872
0873 if (copy_from_user(rxnfc, useraddr, size))
0874 return -EFAULT;
0875
0876 return 0;
0877 }
0878
0879 static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
0880 const struct ethtool_rxnfc *rxnfc,
0881 size_t size, const u32 *rule_buf)
0882 {
0883 struct compat_ethtool_rxnfc crxnfc;
0884
0885 memset(&crxnfc, 0, sizeof(crxnfc));
0886 crxnfc = (struct compat_ethtool_rxnfc) {
0887 .cmd = rxnfc->cmd,
0888 .flow_type = rxnfc->flow_type,
0889 .data = rxnfc->data,
0890 .fs = {
0891 .flow_type = rxnfc->fs.flow_type,
0892 .h_u = rxnfc->fs.h_u,
0893 .h_ext = rxnfc->fs.h_ext,
0894 .m_u = rxnfc->fs.m_u,
0895 .m_ext = rxnfc->fs.m_ext,
0896 .ring_cookie = rxnfc->fs.ring_cookie,
0897 .location = rxnfc->fs.location,
0898 },
0899 .rule_cnt = rxnfc->rule_cnt,
0900 };
0901
0902 if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
0903 return -EFAULT;
0904
0905 return 0;
0906 }
0907
0908 static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
0909 const struct ethtool_rxnfc *rxnfc,
0910 size_t size, const u32 *rule_buf)
0911 {
0912 int ret;
0913
0914 if (compat_need_64bit_alignment_fixup()) {
0915 ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
0916 rule_buf);
0917 useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
0918 } else {
0919 ret = copy_to_user(useraddr, rxnfc, size);
0920 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
0921 }
0922
0923 if (ret)
0924 return -EFAULT;
0925
0926 if (rule_buf) {
0927 if (copy_to_user(useraddr, rule_buf,
0928 rxnfc->rule_cnt * sizeof(u32)))
0929 return -EFAULT;
0930 }
0931
0932 return 0;
0933 }
0934
0935 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
0936 u32 cmd, void __user *useraddr)
0937 {
0938 struct ethtool_rxnfc info;
0939 size_t info_size = sizeof(info);
0940 int rc;
0941
0942 if (!dev->ethtool_ops->set_rxnfc)
0943 return -EOPNOTSUPP;
0944
0945
0946
0947
0948
0949 if (cmd == ETHTOOL_SRXFH)
0950 info_size = (offsetof(struct ethtool_rxnfc, data) +
0951 sizeof(info.data));
0952
0953 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
0954 return -EFAULT;
0955
0956 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
0957 if (rc)
0958 return rc;
0959
0960 if (cmd == ETHTOOL_SRXCLSRLINS &&
0961 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
0962 return -EFAULT;
0963
0964 return 0;
0965 }
0966
0967 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
0968 u32 cmd, void __user *useraddr)
0969 {
0970 struct ethtool_rxnfc info;
0971 size_t info_size = sizeof(info);
0972 const struct ethtool_ops *ops = dev->ethtool_ops;
0973 int ret;
0974 void *rule_buf = NULL;
0975
0976 if (!ops->get_rxnfc)
0977 return -EOPNOTSUPP;
0978
0979
0980
0981
0982
0983 if (cmd == ETHTOOL_GRXFH)
0984 info_size = (offsetof(struct ethtool_rxnfc, data) +
0985 sizeof(info.data));
0986
0987 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
0988 return -EFAULT;
0989
0990
0991
0992
0993 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
0994 info_size = sizeof(info);
0995 if (ethtool_rxnfc_copy_from_user(&info, useraddr, info_size))
0996 return -EFAULT;
0997
0998
0999
1000 if (!(info.flow_type & FLOW_RSS))
1001 return -EINVAL;
1002 }
1003
1004 if (info.cmd != cmd)
1005 return -EINVAL;
1006
1007 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1008 if (info.rule_cnt > 0) {
1009 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1010 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1011 GFP_USER);
1012 if (!rule_buf)
1013 return -ENOMEM;
1014 }
1015 }
1016
1017 ret = ops->get_rxnfc(dev, &info, rule_buf);
1018 if (ret < 0)
1019 goto err_out;
1020
1021 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
1022 err_out:
1023 kfree(rule_buf);
1024
1025 return ret;
1026 }
1027
1028 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1029 struct ethtool_rxnfc *rx_rings,
1030 u32 size)
1031 {
1032 int i;
1033
1034 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
1035 return -EFAULT;
1036
1037
1038 for (i = 0; i < size; i++)
1039 if (indir[i] >= rx_rings->data)
1040 return -EINVAL;
1041
1042 return 0;
1043 }
1044
1045 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1046
1047 void netdev_rss_key_fill(void *buffer, size_t len)
1048 {
1049 BUG_ON(len > sizeof(netdev_rss_key));
1050 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1051 memcpy(buffer, netdev_rss_key, len);
1052 }
1053 EXPORT_SYMBOL(netdev_rss_key_fill);
1054
1055 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1056 void __user *useraddr)
1057 {
1058 u32 user_size, dev_size;
1059 u32 *indir;
1060 int ret;
1061
1062 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1063 !dev->ethtool_ops->get_rxfh)
1064 return -EOPNOTSUPP;
1065 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1066 if (dev_size == 0)
1067 return -EOPNOTSUPP;
1068
1069 if (copy_from_user(&user_size,
1070 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1071 sizeof(user_size)))
1072 return -EFAULT;
1073
1074 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1075 &dev_size, sizeof(dev_size)))
1076 return -EFAULT;
1077
1078
1079
1080
1081
1082 if (user_size < dev_size)
1083 return user_size == 0 ? 0 : -EINVAL;
1084
1085 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1086 if (!indir)
1087 return -ENOMEM;
1088
1089 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1090 if (ret)
1091 goto out;
1092
1093 if (copy_to_user(useraddr +
1094 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1095 indir, dev_size * sizeof(indir[0])))
1096 ret = -EFAULT;
1097
1098 out:
1099 kfree(indir);
1100 return ret;
1101 }
1102
1103 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1104 void __user *useraddr)
1105 {
1106 struct ethtool_rxnfc rx_rings;
1107 u32 user_size, dev_size, i;
1108 u32 *indir;
1109 const struct ethtool_ops *ops = dev->ethtool_ops;
1110 int ret;
1111 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1112
1113 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1114 !ops->get_rxnfc)
1115 return -EOPNOTSUPP;
1116
1117 dev_size = ops->get_rxfh_indir_size(dev);
1118 if (dev_size == 0)
1119 return -EOPNOTSUPP;
1120
1121 if (copy_from_user(&user_size,
1122 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1123 sizeof(user_size)))
1124 return -EFAULT;
1125
1126 if (user_size != 0 && user_size != dev_size)
1127 return -EINVAL;
1128
1129 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1130 if (!indir)
1131 return -ENOMEM;
1132
1133 rx_rings.cmd = ETHTOOL_GRXRINGS;
1134 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1135 if (ret)
1136 goto out;
1137
1138 if (user_size == 0) {
1139 for (i = 0; i < dev_size; i++)
1140 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1141 } else {
1142 ret = ethtool_copy_validate_indir(indir,
1143 useraddr + ringidx_offset,
1144 &rx_rings,
1145 dev_size);
1146 if (ret)
1147 goto out;
1148 }
1149
1150 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1151 if (ret)
1152 goto out;
1153
1154
1155 if (user_size == 0)
1156 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1157 else
1158 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1159
1160 out:
1161 kfree(indir);
1162 return ret;
1163 }
1164
1165 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1166 void __user *useraddr)
1167 {
1168 int ret;
1169 const struct ethtool_ops *ops = dev->ethtool_ops;
1170 u32 user_indir_size, user_key_size;
1171 u32 dev_indir_size = 0, dev_key_size = 0;
1172 struct ethtool_rxfh rxfh;
1173 u32 total_size;
1174 u32 indir_bytes;
1175 u32 *indir = NULL;
1176 u8 dev_hfunc = 0;
1177 u8 *hkey = NULL;
1178 u8 *rss_config;
1179
1180 if (!ops->get_rxfh)
1181 return -EOPNOTSUPP;
1182
1183 if (ops->get_rxfh_indir_size)
1184 dev_indir_size = ops->get_rxfh_indir_size(dev);
1185 if (ops->get_rxfh_key_size)
1186 dev_key_size = ops->get_rxfh_key_size(dev);
1187
1188 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1189 return -EFAULT;
1190 user_indir_size = rxfh.indir_size;
1191 user_key_size = rxfh.key_size;
1192
1193
1194 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1195 return -EINVAL;
1196
1197 if (rxfh.rss_context && !ops->get_rxfh_context)
1198 return -EOPNOTSUPP;
1199
1200 rxfh.indir_size = dev_indir_size;
1201 rxfh.key_size = dev_key_size;
1202 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1203 return -EFAULT;
1204
1205 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1206 (user_key_size && (user_key_size != dev_key_size)))
1207 return -EINVAL;
1208
1209 indir_bytes = user_indir_size * sizeof(indir[0]);
1210 total_size = indir_bytes + user_key_size;
1211 rss_config = kzalloc(total_size, GFP_USER);
1212 if (!rss_config)
1213 return -ENOMEM;
1214
1215 if (user_indir_size)
1216 indir = (u32 *)rss_config;
1217
1218 if (user_key_size)
1219 hkey = rss_config + indir_bytes;
1220
1221 if (rxfh.rss_context)
1222 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1223 &dev_hfunc,
1224 rxfh.rss_context);
1225 else
1226 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1227 if (ret)
1228 goto out;
1229
1230 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1231 &dev_hfunc, sizeof(rxfh.hfunc))) {
1232 ret = -EFAULT;
1233 } else if (copy_to_user(useraddr +
1234 offsetof(struct ethtool_rxfh, rss_config[0]),
1235 rss_config, total_size)) {
1236 ret = -EFAULT;
1237 }
1238 out:
1239 kfree(rss_config);
1240
1241 return ret;
1242 }
1243
1244 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1245 void __user *useraddr)
1246 {
1247 int ret;
1248 const struct ethtool_ops *ops = dev->ethtool_ops;
1249 struct ethtool_rxnfc rx_rings;
1250 struct ethtool_rxfh rxfh;
1251 u32 dev_indir_size = 0, dev_key_size = 0, i;
1252 u32 *indir = NULL, indir_bytes = 0;
1253 u8 *hkey = NULL;
1254 u8 *rss_config;
1255 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1256 bool delete = false;
1257
1258 if (!ops->get_rxnfc || !ops->set_rxfh)
1259 return -EOPNOTSUPP;
1260
1261 if (ops->get_rxfh_indir_size)
1262 dev_indir_size = ops->get_rxfh_indir_size(dev);
1263 if (ops->get_rxfh_key_size)
1264 dev_key_size = ops->get_rxfh_key_size(dev);
1265
1266 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1267 return -EFAULT;
1268
1269
1270 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1271 return -EINVAL;
1272
1273 if (rxfh.rss_context && !ops->set_rxfh_context)
1274 return -EOPNOTSUPP;
1275
1276
1277
1278
1279 if ((rxfh.indir_size &&
1280 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1281 rxfh.indir_size != dev_indir_size) ||
1282 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1283 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1284 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1285 return -EINVAL;
1286
1287 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1288 indir_bytes = dev_indir_size * sizeof(indir[0]);
1289
1290 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1291 if (!rss_config)
1292 return -ENOMEM;
1293
1294 rx_rings.cmd = ETHTOOL_GRXRINGS;
1295 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1296 if (ret)
1297 goto out;
1298
1299
1300
1301
1302
1303 if (rxfh.indir_size &&
1304 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1305 indir = (u32 *)rss_config;
1306 ret = ethtool_copy_validate_indir(indir,
1307 useraddr + rss_cfg_offset,
1308 &rx_rings,
1309 rxfh.indir_size);
1310 if (ret)
1311 goto out;
1312 } else if (rxfh.indir_size == 0) {
1313 if (rxfh.rss_context == 0) {
1314 indir = (u32 *)rss_config;
1315 for (i = 0; i < dev_indir_size; i++)
1316 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1317 } else {
1318 delete = true;
1319 }
1320 }
1321
1322 if (rxfh.key_size) {
1323 hkey = rss_config + indir_bytes;
1324 if (copy_from_user(hkey,
1325 useraddr + rss_cfg_offset + indir_bytes,
1326 rxfh.key_size)) {
1327 ret = -EFAULT;
1328 goto out;
1329 }
1330 }
1331
1332 if (rxfh.rss_context)
1333 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1334 &rxfh.rss_context, delete);
1335 else
1336 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1337 if (ret)
1338 goto out;
1339
1340 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1341 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1342 ret = -EFAULT;
1343
1344 if (!rxfh.rss_context) {
1345
1346 if (rxfh.indir_size == 0)
1347 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1348 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1349 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1350 }
1351
1352 out:
1353 kfree(rss_config);
1354 return ret;
1355 }
1356
1357 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1358 {
1359 struct ethtool_regs regs;
1360 const struct ethtool_ops *ops = dev->ethtool_ops;
1361 void *regbuf;
1362 int reglen, ret;
1363
1364 if (!ops->get_regs || !ops->get_regs_len)
1365 return -EOPNOTSUPP;
1366
1367 if (copy_from_user(®s, useraddr, sizeof(regs)))
1368 return -EFAULT;
1369
1370 reglen = ops->get_regs_len(dev);
1371 if (reglen <= 0)
1372 return reglen;
1373
1374 if (regs.len > reglen)
1375 regs.len = reglen;
1376
1377 regbuf = vzalloc(reglen);
1378 if (!regbuf)
1379 return -ENOMEM;
1380
1381 if (regs.len < reglen)
1382 reglen = regs.len;
1383
1384 ops->get_regs(dev, ®s, regbuf);
1385
1386 ret = -EFAULT;
1387 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1388 goto out;
1389 useraddr += offsetof(struct ethtool_regs, data);
1390 if (copy_to_user(useraddr, regbuf, reglen))
1391 goto out;
1392 ret = 0;
1393
1394 out:
1395 vfree(regbuf);
1396 return ret;
1397 }
1398
1399 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1400 {
1401 struct ethtool_value reset;
1402 int ret;
1403
1404 if (!dev->ethtool_ops->reset)
1405 return -EOPNOTSUPP;
1406
1407 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1408 return -EFAULT;
1409
1410 ret = dev->ethtool_ops->reset(dev, &reset.data);
1411 if (ret)
1412 return ret;
1413
1414 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1415 return -EFAULT;
1416 return 0;
1417 }
1418
1419 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1420 {
1421 struct ethtool_wolinfo wol;
1422
1423 if (!dev->ethtool_ops->get_wol)
1424 return -EOPNOTSUPP;
1425
1426 memset(&wol, 0, sizeof(struct ethtool_wolinfo));
1427 wol.cmd = ETHTOOL_GWOL;
1428 dev->ethtool_ops->get_wol(dev, &wol);
1429
1430 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1431 return -EFAULT;
1432 return 0;
1433 }
1434
1435 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1436 {
1437 struct ethtool_wolinfo wol;
1438 int ret;
1439
1440 if (!dev->ethtool_ops->set_wol)
1441 return -EOPNOTSUPP;
1442
1443 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1444 return -EFAULT;
1445
1446 ret = dev->ethtool_ops->set_wol(dev, &wol);
1447 if (ret)
1448 return ret;
1449
1450 dev->wol_enabled = !!wol.wolopts;
1451 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
1452
1453 return 0;
1454 }
1455
1456 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1457 {
1458 struct ethtool_eee edata;
1459 int rc;
1460
1461 if (!dev->ethtool_ops->get_eee)
1462 return -EOPNOTSUPP;
1463
1464 memset(&edata, 0, sizeof(struct ethtool_eee));
1465 edata.cmd = ETHTOOL_GEEE;
1466 rc = dev->ethtool_ops->get_eee(dev, &edata);
1467
1468 if (rc)
1469 return rc;
1470
1471 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1472 return -EFAULT;
1473
1474 return 0;
1475 }
1476
1477 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1478 {
1479 struct ethtool_eee edata;
1480 int ret;
1481
1482 if (!dev->ethtool_ops->set_eee)
1483 return -EOPNOTSUPP;
1484
1485 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1486 return -EFAULT;
1487
1488 ret = dev->ethtool_ops->set_eee(dev, &edata);
1489 if (!ret)
1490 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
1491 return ret;
1492 }
1493
1494 static int ethtool_nway_reset(struct net_device *dev)
1495 {
1496 if (!dev->ethtool_ops->nway_reset)
1497 return -EOPNOTSUPP;
1498
1499 return dev->ethtool_ops->nway_reset(dev);
1500 }
1501
1502 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1503 {
1504 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1505 int link = __ethtool_get_link(dev);
1506
1507 if (link < 0)
1508 return link;
1509
1510 edata.data = link;
1511 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1512 return -EFAULT;
1513 return 0;
1514 }
1515
1516 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1517 int (*getter)(struct net_device *,
1518 struct ethtool_eeprom *, u8 *),
1519 u32 total_len)
1520 {
1521 struct ethtool_eeprom eeprom;
1522 void __user *userbuf = useraddr + sizeof(eeprom);
1523 u32 bytes_remaining;
1524 u8 *data;
1525 int ret = 0;
1526
1527 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1528 return -EFAULT;
1529
1530
1531 if (eeprom.offset + eeprom.len <= eeprom.offset)
1532 return -EINVAL;
1533
1534
1535 if (eeprom.offset + eeprom.len > total_len)
1536 return -EINVAL;
1537
1538 data = kzalloc(PAGE_SIZE, GFP_USER);
1539 if (!data)
1540 return -ENOMEM;
1541
1542 bytes_remaining = eeprom.len;
1543 while (bytes_remaining > 0) {
1544 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1545
1546 ret = getter(dev, &eeprom, data);
1547 if (ret)
1548 break;
1549 if (!eeprom.len) {
1550 ret = -EIO;
1551 break;
1552 }
1553 if (copy_to_user(userbuf, data, eeprom.len)) {
1554 ret = -EFAULT;
1555 break;
1556 }
1557 userbuf += eeprom.len;
1558 eeprom.offset += eeprom.len;
1559 bytes_remaining -= eeprom.len;
1560 }
1561
1562 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1563 eeprom.offset -= eeprom.len;
1564 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1565 ret = -EFAULT;
1566
1567 kfree(data);
1568 return ret;
1569 }
1570
1571 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1572 {
1573 const struct ethtool_ops *ops = dev->ethtool_ops;
1574
1575 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1576 !ops->get_eeprom_len(dev))
1577 return -EOPNOTSUPP;
1578
1579 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1580 ops->get_eeprom_len(dev));
1581 }
1582
1583 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1584 {
1585 struct ethtool_eeprom eeprom;
1586 const struct ethtool_ops *ops = dev->ethtool_ops;
1587 void __user *userbuf = useraddr + sizeof(eeprom);
1588 u32 bytes_remaining;
1589 u8 *data;
1590 int ret = 0;
1591
1592 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1593 !ops->get_eeprom_len(dev))
1594 return -EOPNOTSUPP;
1595
1596 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1597 return -EFAULT;
1598
1599
1600 if (eeprom.offset + eeprom.len <= eeprom.offset)
1601 return -EINVAL;
1602
1603
1604 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1605 return -EINVAL;
1606
1607 data = kzalloc(PAGE_SIZE, GFP_USER);
1608 if (!data)
1609 return -ENOMEM;
1610
1611 bytes_remaining = eeprom.len;
1612 while (bytes_remaining > 0) {
1613 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1614
1615 if (copy_from_user(data, userbuf, eeprom.len)) {
1616 ret = -EFAULT;
1617 break;
1618 }
1619 ret = ops->set_eeprom(dev, &eeprom, data);
1620 if (ret)
1621 break;
1622 userbuf += eeprom.len;
1623 eeprom.offset += eeprom.len;
1624 bytes_remaining -= eeprom.len;
1625 }
1626
1627 kfree(data);
1628 return ret;
1629 }
1630
1631 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1632 void __user *useraddr)
1633 {
1634 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1635 struct kernel_ethtool_coalesce kernel_coalesce = {};
1636 int ret;
1637
1638 if (!dev->ethtool_ops->get_coalesce)
1639 return -EOPNOTSUPP;
1640
1641 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1642 NULL);
1643 if (ret)
1644 return ret;
1645
1646 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1647 return -EFAULT;
1648 return 0;
1649 }
1650
1651 static bool
1652 ethtool_set_coalesce_supported(struct net_device *dev,
1653 struct ethtool_coalesce *coalesce)
1654 {
1655 u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
1656 u32 nonzero_params = 0;
1657
1658 if (coalesce->rx_coalesce_usecs)
1659 nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
1660 if (coalesce->rx_max_coalesced_frames)
1661 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
1662 if (coalesce->rx_coalesce_usecs_irq)
1663 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
1664 if (coalesce->rx_max_coalesced_frames_irq)
1665 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
1666 if (coalesce->tx_coalesce_usecs)
1667 nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
1668 if (coalesce->tx_max_coalesced_frames)
1669 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
1670 if (coalesce->tx_coalesce_usecs_irq)
1671 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
1672 if (coalesce->tx_max_coalesced_frames_irq)
1673 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
1674 if (coalesce->stats_block_coalesce_usecs)
1675 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
1676 if (coalesce->use_adaptive_rx_coalesce)
1677 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
1678 if (coalesce->use_adaptive_tx_coalesce)
1679 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
1680 if (coalesce->pkt_rate_low)
1681 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
1682 if (coalesce->rx_coalesce_usecs_low)
1683 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
1684 if (coalesce->rx_max_coalesced_frames_low)
1685 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
1686 if (coalesce->tx_coalesce_usecs_low)
1687 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
1688 if (coalesce->tx_max_coalesced_frames_low)
1689 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
1690 if (coalesce->pkt_rate_high)
1691 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
1692 if (coalesce->rx_coalesce_usecs_high)
1693 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
1694 if (coalesce->rx_max_coalesced_frames_high)
1695 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
1696 if (coalesce->tx_coalesce_usecs_high)
1697 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
1698 if (coalesce->tx_max_coalesced_frames_high)
1699 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
1700 if (coalesce->rate_sample_interval)
1701 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
1702
1703 return (supported_params & nonzero_params) == nonzero_params;
1704 }
1705
1706 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1707 void __user *useraddr)
1708 {
1709 struct kernel_ethtool_coalesce kernel_coalesce = {};
1710 struct ethtool_coalesce coalesce;
1711 int ret;
1712
1713 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
1714 return -EOPNOTSUPP;
1715
1716 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1717 NULL);
1718 if (ret)
1719 return ret;
1720
1721 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1722 return -EFAULT;
1723
1724 if (!ethtool_set_coalesce_supported(dev, &coalesce))
1725 return -EOPNOTSUPP;
1726
1727 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1728 NULL);
1729 if (!ret)
1730 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
1731 return ret;
1732 }
1733
1734 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1735 {
1736 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1737 struct kernel_ethtool_ringparam kernel_ringparam = {};
1738
1739 if (!dev->ethtool_ops->get_ringparam)
1740 return -EOPNOTSUPP;
1741
1742 dev->ethtool_ops->get_ringparam(dev, &ringparam,
1743 &kernel_ringparam, NULL);
1744
1745 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1746 return -EFAULT;
1747 return 0;
1748 }
1749
1750 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1751 {
1752 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1753 struct kernel_ethtool_ringparam kernel_ringparam;
1754 int ret;
1755
1756 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1757 return -EOPNOTSUPP;
1758
1759 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1760 return -EFAULT;
1761
1762 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
1763
1764
1765 if (ringparam.rx_pending > max.rx_max_pending ||
1766 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1767 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1768 ringparam.tx_pending > max.tx_max_pending)
1769 return -EINVAL;
1770
1771 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
1772 &kernel_ringparam, NULL);
1773 if (!ret)
1774 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1775 return ret;
1776 }
1777
1778 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1779 void __user *useraddr)
1780 {
1781 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1782
1783 if (!dev->ethtool_ops->get_channels)
1784 return -EOPNOTSUPP;
1785
1786 dev->ethtool_ops->get_channels(dev, &channels);
1787
1788 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1789 return -EFAULT;
1790 return 0;
1791 }
1792
1793 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1794 void __user *useraddr)
1795 {
1796 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
1797 u16 from_channel, to_channel;
1798 u32 max_rx_in_use = 0;
1799 unsigned int i;
1800 int ret;
1801
1802 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1803 return -EOPNOTSUPP;
1804
1805 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1806 return -EFAULT;
1807
1808 dev->ethtool_ops->get_channels(dev, &curr);
1809
1810 if (channels.rx_count == curr.rx_count &&
1811 channels.tx_count == curr.tx_count &&
1812 channels.combined_count == curr.combined_count &&
1813 channels.other_count == curr.other_count)
1814 return 0;
1815
1816
1817 if (channels.rx_count > curr.max_rx ||
1818 channels.tx_count > curr.max_tx ||
1819 channels.combined_count > curr.max_combined ||
1820 channels.other_count > curr.max_other)
1821 return -EINVAL;
1822
1823
1824 if (!channels.combined_count &&
1825 (!channels.rx_count || !channels.tx_count))
1826 return -EINVAL;
1827
1828
1829
1830 if (netif_is_rxfh_configured(dev) &&
1831 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1832 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1833 return -EINVAL;
1834
1835
1836 from_channel = channels.combined_count +
1837 min(channels.rx_count, channels.tx_count);
1838 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
1839 for (i = from_channel; i < to_channel; i++)
1840 if (xsk_get_pool_from_qid(dev, i))
1841 return -EINVAL;
1842
1843 ret = dev->ethtool_ops->set_channels(dev, &channels);
1844 if (!ret)
1845 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1846 return ret;
1847 }
1848
1849 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1850 {
1851 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
1852
1853 if (!dev->ethtool_ops->get_pauseparam)
1854 return -EOPNOTSUPP;
1855
1856 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1857
1858 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1859 return -EFAULT;
1860 return 0;
1861 }
1862
1863 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1864 {
1865 struct ethtool_pauseparam pauseparam;
1866 int ret;
1867
1868 if (!dev->ethtool_ops->set_pauseparam)
1869 return -EOPNOTSUPP;
1870
1871 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1872 return -EFAULT;
1873
1874 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1875 if (!ret)
1876 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1877 return ret;
1878 }
1879
1880 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1881 {
1882 struct ethtool_test test;
1883 const struct ethtool_ops *ops = dev->ethtool_ops;
1884 u64 *data;
1885 int ret, test_len;
1886
1887 if (!ops->self_test || !ops->get_sset_count)
1888 return -EOPNOTSUPP;
1889
1890 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1891 if (test_len < 0)
1892 return test_len;
1893 WARN_ON(test_len == 0);
1894
1895 if (copy_from_user(&test, useraddr, sizeof(test)))
1896 return -EFAULT;
1897
1898 test.len = test_len;
1899 data = kcalloc(test_len, sizeof(u64), GFP_USER);
1900 if (!data)
1901 return -ENOMEM;
1902
1903 netif_testing_on(dev);
1904 ops->self_test(dev, &test, data);
1905 netif_testing_off(dev);
1906
1907 ret = -EFAULT;
1908 if (copy_to_user(useraddr, &test, sizeof(test)))
1909 goto out;
1910 useraddr += sizeof(test);
1911 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
1912 goto out;
1913 ret = 0;
1914
1915 out:
1916 kfree(data);
1917 return ret;
1918 }
1919
1920 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1921 {
1922 struct ethtool_gstrings gstrings;
1923 u8 *data;
1924 int ret;
1925
1926 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1927 return -EFAULT;
1928
1929 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1930 if (ret < 0)
1931 return ret;
1932 if (ret > S32_MAX / ETH_GSTRING_LEN)
1933 return -ENOMEM;
1934 WARN_ON_ONCE(!ret);
1935
1936 gstrings.len = ret;
1937
1938 if (gstrings.len) {
1939 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1940 if (!data)
1941 return -ENOMEM;
1942
1943 __ethtool_get_strings(dev, gstrings.string_set, data);
1944 } else {
1945 data = NULL;
1946 }
1947
1948 ret = -EFAULT;
1949 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1950 goto out;
1951 useraddr += sizeof(gstrings);
1952 if (gstrings.len &&
1953 copy_to_user(useraddr, data,
1954 array_size(gstrings.len, ETH_GSTRING_LEN)))
1955 goto out;
1956 ret = 0;
1957
1958 out:
1959 vfree(data);
1960 return ret;
1961 }
1962
1963 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
1964 {
1965 va_list args;
1966
1967 va_start(args, fmt);
1968 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
1969 va_end(args);
1970
1971 *data += ETH_GSTRING_LEN;
1972 }
1973 EXPORT_SYMBOL(ethtool_sprintf);
1974
1975 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1976 {
1977 struct ethtool_value id;
1978 static bool busy;
1979 const struct ethtool_ops *ops = dev->ethtool_ops;
1980 netdevice_tracker dev_tracker;
1981 int rc;
1982
1983 if (!ops->set_phys_id)
1984 return -EOPNOTSUPP;
1985
1986 if (busy)
1987 return -EBUSY;
1988
1989 if (copy_from_user(&id, useraddr, sizeof(id)))
1990 return -EFAULT;
1991
1992 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1993 if (rc < 0)
1994 return rc;
1995
1996
1997
1998
1999 busy = true;
2000 netdev_hold(dev, &dev_tracker, GFP_KERNEL);
2001 rtnl_unlock();
2002
2003 if (rc == 0) {
2004
2005 schedule_timeout_interruptible(
2006 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
2007 } else {
2008
2009 int n = rc * 2, interval = HZ / n;
2010 u64 count = n * id.data, i = 0;
2011
2012 do {
2013 rtnl_lock();
2014 rc = ops->set_phys_id(dev,
2015 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
2016 rtnl_unlock();
2017 if (rc)
2018 break;
2019 schedule_timeout_interruptible(interval);
2020 } while (!signal_pending(current) && (!id.data || i < count));
2021 }
2022
2023 rtnl_lock();
2024 netdev_put(dev, &dev_tracker);
2025 busy = false;
2026
2027 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
2028 return rc;
2029 }
2030
2031 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
2032 {
2033 struct ethtool_stats stats;
2034 const struct ethtool_ops *ops = dev->ethtool_ops;
2035 u64 *data;
2036 int ret, n_stats;
2037
2038 if (!ops->get_ethtool_stats || !ops->get_sset_count)
2039 return -EOPNOTSUPP;
2040
2041 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
2042 if (n_stats < 0)
2043 return n_stats;
2044 if (n_stats > S32_MAX / sizeof(u64))
2045 return -ENOMEM;
2046 WARN_ON_ONCE(!n_stats);
2047 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2048 return -EFAULT;
2049
2050 stats.n_stats = n_stats;
2051
2052 if (n_stats) {
2053 data = vzalloc(array_size(n_stats, sizeof(u64)));
2054 if (!data)
2055 return -ENOMEM;
2056 ops->get_ethtool_stats(dev, &stats, data);
2057 } else {
2058 data = NULL;
2059 }
2060
2061 ret = -EFAULT;
2062 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2063 goto out;
2064 useraddr += sizeof(stats);
2065 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2066 goto out;
2067 ret = 0;
2068
2069 out:
2070 vfree(data);
2071 return ret;
2072 }
2073
2074 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2075 {
2076 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2077 const struct ethtool_ops *ops = dev->ethtool_ops;
2078 struct phy_device *phydev = dev->phydev;
2079 struct ethtool_stats stats;
2080 u64 *data;
2081 int ret, n_stats;
2082
2083 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
2084 return -EOPNOTSUPP;
2085
2086 if (phydev && !ops->get_ethtool_phy_stats &&
2087 phy_ops && phy_ops->get_sset_count)
2088 n_stats = phy_ops->get_sset_count(phydev);
2089 else
2090 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2091 if (n_stats < 0)
2092 return n_stats;
2093 if (n_stats > S32_MAX / sizeof(u64))
2094 return -ENOMEM;
2095 WARN_ON_ONCE(!n_stats);
2096
2097 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2098 return -EFAULT;
2099
2100 stats.n_stats = n_stats;
2101
2102 if (n_stats) {
2103 data = vzalloc(array_size(n_stats, sizeof(u64)));
2104 if (!data)
2105 return -ENOMEM;
2106
2107 if (phydev && !ops->get_ethtool_phy_stats &&
2108 phy_ops && phy_ops->get_stats) {
2109 ret = phy_ops->get_stats(phydev, &stats, data);
2110 if (ret < 0)
2111 goto out;
2112 } else {
2113 ops->get_ethtool_phy_stats(dev, &stats, data);
2114 }
2115 } else {
2116 data = NULL;
2117 }
2118
2119 ret = -EFAULT;
2120 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2121 goto out;
2122 useraddr += sizeof(stats);
2123 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2124 goto out;
2125 ret = 0;
2126
2127 out:
2128 vfree(data);
2129 return ret;
2130 }
2131
2132 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2133 {
2134 struct ethtool_perm_addr epaddr;
2135
2136 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2137 return -EFAULT;
2138
2139 if (epaddr.size < dev->addr_len)
2140 return -ETOOSMALL;
2141 epaddr.size = dev->addr_len;
2142
2143 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2144 return -EFAULT;
2145 useraddr += sizeof(epaddr);
2146 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2147 return -EFAULT;
2148 return 0;
2149 }
2150
2151 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2152 u32 cmd, u32 (*actor)(struct net_device *))
2153 {
2154 struct ethtool_value edata = { .cmd = cmd };
2155
2156 if (!actor)
2157 return -EOPNOTSUPP;
2158
2159 edata.data = actor(dev);
2160
2161 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2162 return -EFAULT;
2163 return 0;
2164 }
2165
2166 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2167 void (*actor)(struct net_device *, u32))
2168 {
2169 struct ethtool_value edata;
2170
2171 if (!actor)
2172 return -EOPNOTSUPP;
2173
2174 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2175 return -EFAULT;
2176
2177 actor(dev, edata.data);
2178 return 0;
2179 }
2180
2181 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2182 int (*actor)(struct net_device *, u32))
2183 {
2184 struct ethtool_value edata;
2185
2186 if (!actor)
2187 return -EOPNOTSUPP;
2188
2189 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2190 return -EFAULT;
2191
2192 return actor(dev, edata.data);
2193 }
2194
2195 static int
2196 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
2197 {
2198 if (!dev->ethtool_ops->flash_device) {
2199 req->devlink = netdev_to_devlink_get(dev);
2200 return 0;
2201 }
2202
2203 return dev->ethtool_ops->flash_device(dev, &req->efl);
2204 }
2205
2206 static int ethtool_set_dump(struct net_device *dev,
2207 void __user *useraddr)
2208 {
2209 struct ethtool_dump dump;
2210
2211 if (!dev->ethtool_ops->set_dump)
2212 return -EOPNOTSUPP;
2213
2214 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2215 return -EFAULT;
2216
2217 return dev->ethtool_ops->set_dump(dev, &dump);
2218 }
2219
2220 static int ethtool_get_dump_flag(struct net_device *dev,
2221 void __user *useraddr)
2222 {
2223 int ret;
2224 struct ethtool_dump dump;
2225 const struct ethtool_ops *ops = dev->ethtool_ops;
2226
2227 if (!ops->get_dump_flag)
2228 return -EOPNOTSUPP;
2229
2230 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2231 return -EFAULT;
2232
2233 ret = ops->get_dump_flag(dev, &dump);
2234 if (ret)
2235 return ret;
2236
2237 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2238 return -EFAULT;
2239 return 0;
2240 }
2241
2242 static int ethtool_get_dump_data(struct net_device *dev,
2243 void __user *useraddr)
2244 {
2245 int ret;
2246 __u32 len;
2247 struct ethtool_dump dump, tmp;
2248 const struct ethtool_ops *ops = dev->ethtool_ops;
2249 void *data = NULL;
2250
2251 if (!ops->get_dump_data || !ops->get_dump_flag)
2252 return -EOPNOTSUPP;
2253
2254 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2255 return -EFAULT;
2256
2257 memset(&tmp, 0, sizeof(tmp));
2258 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2259 ret = ops->get_dump_flag(dev, &tmp);
2260 if (ret)
2261 return ret;
2262
2263 len = min(tmp.len, dump.len);
2264 if (!len)
2265 return -EFAULT;
2266
2267
2268
2269
2270 dump.len = len;
2271
2272
2273
2274
2275
2276 data = vzalloc(tmp.len);
2277 if (!data)
2278 return -ENOMEM;
2279 ret = ops->get_dump_data(dev, &dump, data);
2280 if (ret)
2281 goto out;
2282
2283
2284
2285
2286
2287
2288
2289
2290 WARN_ON(dump.len != len && dump.len != tmp.len);
2291 dump.len = len;
2292
2293 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2294 ret = -EFAULT;
2295 goto out;
2296 }
2297 useraddr += offsetof(struct ethtool_dump, data);
2298 if (copy_to_user(useraddr, data, len))
2299 ret = -EFAULT;
2300 out:
2301 vfree(data);
2302 return ret;
2303 }
2304
2305 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2306 {
2307 struct ethtool_ts_info info;
2308 int err;
2309
2310 err = __ethtool_get_ts_info(dev, &info);
2311 if (err)
2312 return err;
2313
2314 if (copy_to_user(useraddr, &info, sizeof(info)))
2315 return -EFAULT;
2316
2317 return 0;
2318 }
2319
2320 int ethtool_get_module_info_call(struct net_device *dev,
2321 struct ethtool_modinfo *modinfo)
2322 {
2323 const struct ethtool_ops *ops = dev->ethtool_ops;
2324 struct phy_device *phydev = dev->phydev;
2325
2326 if (dev->sfp_bus)
2327 return sfp_get_module_info(dev->sfp_bus, modinfo);
2328
2329 if (phydev && phydev->drv && phydev->drv->module_info)
2330 return phydev->drv->module_info(phydev, modinfo);
2331
2332 if (ops->get_module_info)
2333 return ops->get_module_info(dev, modinfo);
2334
2335 return -EOPNOTSUPP;
2336 }
2337
2338 static int ethtool_get_module_info(struct net_device *dev,
2339 void __user *useraddr)
2340 {
2341 int ret;
2342 struct ethtool_modinfo modinfo;
2343
2344 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2345 return -EFAULT;
2346
2347 ret = ethtool_get_module_info_call(dev, &modinfo);
2348 if (ret)
2349 return ret;
2350
2351 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2352 return -EFAULT;
2353
2354 return 0;
2355 }
2356
2357 int ethtool_get_module_eeprom_call(struct net_device *dev,
2358 struct ethtool_eeprom *ee, u8 *data)
2359 {
2360 const struct ethtool_ops *ops = dev->ethtool_ops;
2361 struct phy_device *phydev = dev->phydev;
2362
2363 if (dev->sfp_bus)
2364 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2365
2366 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2367 return phydev->drv->module_eeprom(phydev, ee, data);
2368
2369 if (ops->get_module_eeprom)
2370 return ops->get_module_eeprom(dev, ee, data);
2371
2372 return -EOPNOTSUPP;
2373 }
2374
2375 static int ethtool_get_module_eeprom(struct net_device *dev,
2376 void __user *useraddr)
2377 {
2378 int ret;
2379 struct ethtool_modinfo modinfo;
2380
2381 ret = ethtool_get_module_info_call(dev, &modinfo);
2382 if (ret)
2383 return ret;
2384
2385 return ethtool_get_any_eeprom(dev, useraddr,
2386 ethtool_get_module_eeprom_call,
2387 modinfo.eeprom_len);
2388 }
2389
2390 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2391 {
2392 switch (tuna->id) {
2393 case ETHTOOL_RX_COPYBREAK:
2394 case ETHTOOL_TX_COPYBREAK:
2395 case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
2396 if (tuna->len != sizeof(u32) ||
2397 tuna->type_id != ETHTOOL_TUNABLE_U32)
2398 return -EINVAL;
2399 break;
2400 case ETHTOOL_PFC_PREVENTION_TOUT:
2401 if (tuna->len != sizeof(u16) ||
2402 tuna->type_id != ETHTOOL_TUNABLE_U16)
2403 return -EINVAL;
2404 break;
2405 default:
2406 return -EINVAL;
2407 }
2408
2409 return 0;
2410 }
2411
2412 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2413 {
2414 int ret;
2415 struct ethtool_tunable tuna;
2416 const struct ethtool_ops *ops = dev->ethtool_ops;
2417 void *data;
2418
2419 if (!ops->get_tunable)
2420 return -EOPNOTSUPP;
2421 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2422 return -EFAULT;
2423 ret = ethtool_tunable_valid(&tuna);
2424 if (ret)
2425 return ret;
2426 data = kzalloc(tuna.len, GFP_USER);
2427 if (!data)
2428 return -ENOMEM;
2429 ret = ops->get_tunable(dev, &tuna, data);
2430 if (ret)
2431 goto out;
2432 useraddr += sizeof(tuna);
2433 ret = -EFAULT;
2434 if (copy_to_user(useraddr, data, tuna.len))
2435 goto out;
2436 ret = 0;
2437
2438 out:
2439 kfree(data);
2440 return ret;
2441 }
2442
2443 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2444 {
2445 int ret;
2446 struct ethtool_tunable tuna;
2447 const struct ethtool_ops *ops = dev->ethtool_ops;
2448 void *data;
2449
2450 if (!ops->set_tunable)
2451 return -EOPNOTSUPP;
2452 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2453 return -EFAULT;
2454 ret = ethtool_tunable_valid(&tuna);
2455 if (ret)
2456 return ret;
2457 useraddr += sizeof(tuna);
2458 data = memdup_user(useraddr, tuna.len);
2459 if (IS_ERR(data))
2460 return PTR_ERR(data);
2461 ret = ops->set_tunable(dev, &tuna, data);
2462
2463 kfree(data);
2464 return ret;
2465 }
2466
2467 static noinline_for_stack int
2468 ethtool_get_per_queue_coalesce(struct net_device *dev,
2469 void __user *useraddr,
2470 struct ethtool_per_queue_op *per_queue_opt)
2471 {
2472 u32 bit;
2473 int ret;
2474 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2475
2476 if (!dev->ethtool_ops->get_per_queue_coalesce)
2477 return -EOPNOTSUPP;
2478
2479 useraddr += sizeof(*per_queue_opt);
2480
2481 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2482 MAX_NUM_QUEUE);
2483
2484 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2485 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2486
2487 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2488 if (ret != 0)
2489 return ret;
2490 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2491 return -EFAULT;
2492 useraddr += sizeof(coalesce);
2493 }
2494
2495 return 0;
2496 }
2497
2498 static noinline_for_stack int
2499 ethtool_set_per_queue_coalesce(struct net_device *dev,
2500 void __user *useraddr,
2501 struct ethtool_per_queue_op *per_queue_opt)
2502 {
2503 u32 bit;
2504 int i, ret = 0;
2505 int n_queue;
2506 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2507 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2508
2509 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2510 (!dev->ethtool_ops->get_per_queue_coalesce))
2511 return -EOPNOTSUPP;
2512
2513 useraddr += sizeof(*per_queue_opt);
2514
2515 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2516 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2517 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2518 if (!backup)
2519 return -ENOMEM;
2520
2521 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2522 struct ethtool_coalesce coalesce;
2523
2524 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2525 if (ret != 0)
2526 goto roll_back;
2527
2528 tmp++;
2529
2530 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2531 ret = -EFAULT;
2532 goto roll_back;
2533 }
2534
2535 if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
2536 ret = -EOPNOTSUPP;
2537 goto roll_back;
2538 }
2539
2540 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2541 if (ret != 0)
2542 goto roll_back;
2543
2544 useraddr += sizeof(coalesce);
2545 }
2546
2547 roll_back:
2548 if (ret != 0) {
2549 tmp = backup;
2550 for_each_set_bit(i, queue_mask, bit) {
2551 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2552 tmp++;
2553 }
2554 }
2555 kfree(backup);
2556
2557 return ret;
2558 }
2559
2560 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
2561 void __user *useraddr, u32 sub_cmd)
2562 {
2563 struct ethtool_per_queue_op per_queue_opt;
2564
2565 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2566 return -EFAULT;
2567
2568 if (per_queue_opt.sub_command != sub_cmd)
2569 return -EINVAL;
2570
2571 switch (per_queue_opt.sub_command) {
2572 case ETHTOOL_GCOALESCE:
2573 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2574 case ETHTOOL_SCOALESCE:
2575 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2576 default:
2577 return -EOPNOTSUPP;
2578 }
2579 }
2580
2581 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2582 {
2583 switch (tuna->id) {
2584 case ETHTOOL_PHY_DOWNSHIFT:
2585 case ETHTOOL_PHY_FAST_LINK_DOWN:
2586 if (tuna->len != sizeof(u8) ||
2587 tuna->type_id != ETHTOOL_TUNABLE_U8)
2588 return -EINVAL;
2589 break;
2590 case ETHTOOL_PHY_EDPD:
2591 if (tuna->len != sizeof(u16) ||
2592 tuna->type_id != ETHTOOL_TUNABLE_U16)
2593 return -EINVAL;
2594 break;
2595 default:
2596 return -EINVAL;
2597 }
2598
2599 return 0;
2600 }
2601
2602 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2603 {
2604 struct phy_device *phydev = dev->phydev;
2605 struct ethtool_tunable tuna;
2606 bool phy_drv_tunable;
2607 void *data;
2608 int ret;
2609
2610 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2611 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
2612 return -EOPNOTSUPP;
2613 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2614 return -EFAULT;
2615 ret = ethtool_phy_tunable_valid(&tuna);
2616 if (ret)
2617 return ret;
2618 data = kzalloc(tuna.len, GFP_USER);
2619 if (!data)
2620 return -ENOMEM;
2621 if (phy_drv_tunable) {
2622 mutex_lock(&phydev->lock);
2623 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2624 mutex_unlock(&phydev->lock);
2625 } else {
2626 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2627 }
2628 if (ret)
2629 goto out;
2630 useraddr += sizeof(tuna);
2631 ret = -EFAULT;
2632 if (copy_to_user(useraddr, data, tuna.len))
2633 goto out;
2634 ret = 0;
2635
2636 out:
2637 kfree(data);
2638 return ret;
2639 }
2640
2641 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2642 {
2643 struct phy_device *phydev = dev->phydev;
2644 struct ethtool_tunable tuna;
2645 bool phy_drv_tunable;
2646 void *data;
2647 int ret;
2648
2649 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2650 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
2651 return -EOPNOTSUPP;
2652 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2653 return -EFAULT;
2654 ret = ethtool_phy_tunable_valid(&tuna);
2655 if (ret)
2656 return ret;
2657 useraddr += sizeof(tuna);
2658 data = memdup_user(useraddr, tuna.len);
2659 if (IS_ERR(data))
2660 return PTR_ERR(data);
2661 if (phy_drv_tunable) {
2662 mutex_lock(&phydev->lock);
2663 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2664 mutex_unlock(&phydev->lock);
2665 } else {
2666 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2667 }
2668
2669 kfree(data);
2670 return ret;
2671 }
2672
2673 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2674 {
2675 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
2676 int rc;
2677
2678 if (!dev->ethtool_ops->get_fecparam)
2679 return -EOPNOTSUPP;
2680
2681 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2682 if (rc)
2683 return rc;
2684
2685 if (WARN_ON_ONCE(fecparam.reserved))
2686 fecparam.reserved = 0;
2687
2688 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2689 return -EFAULT;
2690 return 0;
2691 }
2692
2693 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2694 {
2695 struct ethtool_fecparam fecparam;
2696
2697 if (!dev->ethtool_ops->set_fecparam)
2698 return -EOPNOTSUPP;
2699
2700 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2701 return -EFAULT;
2702
2703 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
2704 return -EINVAL;
2705
2706 fecparam.active_fec = 0;
2707 fecparam.reserved = 0;
2708
2709 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2710 }
2711
2712
2713
2714 static int
2715 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2716 u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
2717 {
2718 struct net_device *dev;
2719 u32 sub_cmd;
2720 int rc;
2721 netdev_features_t old_features;
2722
2723 dev = __dev_get_by_name(net, ifr->ifr_name);
2724 if (!dev)
2725 return -ENODEV;
2726
2727 if (ethcmd == ETHTOOL_PERQUEUE) {
2728 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2729 return -EFAULT;
2730 } else {
2731 sub_cmd = ethcmd;
2732 }
2733
2734 switch (sub_cmd) {
2735 case ETHTOOL_GSET:
2736 case ETHTOOL_GDRVINFO:
2737 case ETHTOOL_GMSGLVL:
2738 case ETHTOOL_GLINK:
2739 case ETHTOOL_GCOALESCE:
2740 case ETHTOOL_GRINGPARAM:
2741 case ETHTOOL_GPAUSEPARAM:
2742 case ETHTOOL_GRXCSUM:
2743 case ETHTOOL_GTXCSUM:
2744 case ETHTOOL_GSG:
2745 case ETHTOOL_GSSET_INFO:
2746 case ETHTOOL_GSTRINGS:
2747 case ETHTOOL_GSTATS:
2748 case ETHTOOL_GPHYSTATS:
2749 case ETHTOOL_GTSO:
2750 case ETHTOOL_GPERMADDR:
2751 case ETHTOOL_GUFO:
2752 case ETHTOOL_GGSO:
2753 case ETHTOOL_GGRO:
2754 case ETHTOOL_GFLAGS:
2755 case ETHTOOL_GPFLAGS:
2756 case ETHTOOL_GRXFH:
2757 case ETHTOOL_GRXRINGS:
2758 case ETHTOOL_GRXCLSRLCNT:
2759 case ETHTOOL_GRXCLSRULE:
2760 case ETHTOOL_GRXCLSRLALL:
2761 case ETHTOOL_GRXFHINDIR:
2762 case ETHTOOL_GRSSH:
2763 case ETHTOOL_GFEATURES:
2764 case ETHTOOL_GCHANNELS:
2765 case ETHTOOL_GET_TS_INFO:
2766 case ETHTOOL_GEEE:
2767 case ETHTOOL_GTUNABLE:
2768 case ETHTOOL_PHY_GTUNABLE:
2769 case ETHTOOL_GLINKSETTINGS:
2770 case ETHTOOL_GFECPARAM:
2771 break;
2772 default:
2773 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2774 return -EPERM;
2775 }
2776
2777 if (dev->dev.parent)
2778 pm_runtime_get_sync(dev->dev.parent);
2779
2780 if (!netif_device_present(dev)) {
2781 rc = -ENODEV;
2782 goto out;
2783 }
2784
2785 if (dev->ethtool_ops->begin) {
2786 rc = dev->ethtool_ops->begin(dev);
2787 if (rc < 0)
2788 goto out;
2789 }
2790 old_features = dev->features;
2791
2792 switch (ethcmd) {
2793 case ETHTOOL_GSET:
2794 rc = ethtool_get_settings(dev, useraddr);
2795 break;
2796 case ETHTOOL_SSET:
2797 rc = ethtool_set_settings(dev, useraddr);
2798 break;
2799 case ETHTOOL_GDRVINFO:
2800 rc = ethtool_get_drvinfo(dev, devlink_state);
2801 break;
2802 case ETHTOOL_GREGS:
2803 rc = ethtool_get_regs(dev, useraddr);
2804 break;
2805 case ETHTOOL_GWOL:
2806 rc = ethtool_get_wol(dev, useraddr);
2807 break;
2808 case ETHTOOL_SWOL:
2809 rc = ethtool_set_wol(dev, useraddr);
2810 break;
2811 case ETHTOOL_GMSGLVL:
2812 rc = ethtool_get_value(dev, useraddr, ethcmd,
2813 dev->ethtool_ops->get_msglevel);
2814 break;
2815 case ETHTOOL_SMSGLVL:
2816 rc = ethtool_set_value_void(dev, useraddr,
2817 dev->ethtool_ops->set_msglevel);
2818 if (!rc)
2819 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
2820 break;
2821 case ETHTOOL_GEEE:
2822 rc = ethtool_get_eee(dev, useraddr);
2823 break;
2824 case ETHTOOL_SEEE:
2825 rc = ethtool_set_eee(dev, useraddr);
2826 break;
2827 case ETHTOOL_NWAY_RST:
2828 rc = ethtool_nway_reset(dev);
2829 break;
2830 case ETHTOOL_GLINK:
2831 rc = ethtool_get_link(dev, useraddr);
2832 break;
2833 case ETHTOOL_GEEPROM:
2834 rc = ethtool_get_eeprom(dev, useraddr);
2835 break;
2836 case ETHTOOL_SEEPROM:
2837 rc = ethtool_set_eeprom(dev, useraddr);
2838 break;
2839 case ETHTOOL_GCOALESCE:
2840 rc = ethtool_get_coalesce(dev, useraddr);
2841 break;
2842 case ETHTOOL_SCOALESCE:
2843 rc = ethtool_set_coalesce(dev, useraddr);
2844 break;
2845 case ETHTOOL_GRINGPARAM:
2846 rc = ethtool_get_ringparam(dev, useraddr);
2847 break;
2848 case ETHTOOL_SRINGPARAM:
2849 rc = ethtool_set_ringparam(dev, useraddr);
2850 break;
2851 case ETHTOOL_GPAUSEPARAM:
2852 rc = ethtool_get_pauseparam(dev, useraddr);
2853 break;
2854 case ETHTOOL_SPAUSEPARAM:
2855 rc = ethtool_set_pauseparam(dev, useraddr);
2856 break;
2857 case ETHTOOL_TEST:
2858 rc = ethtool_self_test(dev, useraddr);
2859 break;
2860 case ETHTOOL_GSTRINGS:
2861 rc = ethtool_get_strings(dev, useraddr);
2862 break;
2863 case ETHTOOL_PHYS_ID:
2864 rc = ethtool_phys_id(dev, useraddr);
2865 break;
2866 case ETHTOOL_GSTATS:
2867 rc = ethtool_get_stats(dev, useraddr);
2868 break;
2869 case ETHTOOL_GPERMADDR:
2870 rc = ethtool_get_perm_addr(dev, useraddr);
2871 break;
2872 case ETHTOOL_GFLAGS:
2873 rc = ethtool_get_value(dev, useraddr, ethcmd,
2874 __ethtool_get_flags);
2875 break;
2876 case ETHTOOL_SFLAGS:
2877 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2878 break;
2879 case ETHTOOL_GPFLAGS:
2880 rc = ethtool_get_value(dev, useraddr, ethcmd,
2881 dev->ethtool_ops->get_priv_flags);
2882 if (!rc)
2883 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
2884 break;
2885 case ETHTOOL_SPFLAGS:
2886 rc = ethtool_set_value(dev, useraddr,
2887 dev->ethtool_ops->set_priv_flags);
2888 break;
2889 case ETHTOOL_GRXFH:
2890 case ETHTOOL_GRXRINGS:
2891 case ETHTOOL_GRXCLSRLCNT:
2892 case ETHTOOL_GRXCLSRULE:
2893 case ETHTOOL_GRXCLSRLALL:
2894 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2895 break;
2896 case ETHTOOL_SRXFH:
2897 case ETHTOOL_SRXCLSRLDEL:
2898 case ETHTOOL_SRXCLSRLINS:
2899 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2900 break;
2901 case ETHTOOL_FLASHDEV:
2902 rc = ethtool_flash_device(dev, devlink_state);
2903 break;
2904 case ETHTOOL_RESET:
2905 rc = ethtool_reset(dev, useraddr);
2906 break;
2907 case ETHTOOL_GSSET_INFO:
2908 rc = ethtool_get_sset_info(dev, useraddr);
2909 break;
2910 case ETHTOOL_GRXFHINDIR:
2911 rc = ethtool_get_rxfh_indir(dev, useraddr);
2912 break;
2913 case ETHTOOL_SRXFHINDIR:
2914 rc = ethtool_set_rxfh_indir(dev, useraddr);
2915 break;
2916 case ETHTOOL_GRSSH:
2917 rc = ethtool_get_rxfh(dev, useraddr);
2918 break;
2919 case ETHTOOL_SRSSH:
2920 rc = ethtool_set_rxfh(dev, useraddr);
2921 break;
2922 case ETHTOOL_GFEATURES:
2923 rc = ethtool_get_features(dev, useraddr);
2924 break;
2925 case ETHTOOL_SFEATURES:
2926 rc = ethtool_set_features(dev, useraddr);
2927 break;
2928 case ETHTOOL_GTXCSUM:
2929 case ETHTOOL_GRXCSUM:
2930 case ETHTOOL_GSG:
2931 case ETHTOOL_GTSO:
2932 case ETHTOOL_GGSO:
2933 case ETHTOOL_GGRO:
2934 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2935 break;
2936 case ETHTOOL_STXCSUM:
2937 case ETHTOOL_SRXCSUM:
2938 case ETHTOOL_SSG:
2939 case ETHTOOL_STSO:
2940 case ETHTOOL_SGSO:
2941 case ETHTOOL_SGRO:
2942 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2943 break;
2944 case ETHTOOL_GCHANNELS:
2945 rc = ethtool_get_channels(dev, useraddr);
2946 break;
2947 case ETHTOOL_SCHANNELS:
2948 rc = ethtool_set_channels(dev, useraddr);
2949 break;
2950 case ETHTOOL_SET_DUMP:
2951 rc = ethtool_set_dump(dev, useraddr);
2952 break;
2953 case ETHTOOL_GET_DUMP_FLAG:
2954 rc = ethtool_get_dump_flag(dev, useraddr);
2955 break;
2956 case ETHTOOL_GET_DUMP_DATA:
2957 rc = ethtool_get_dump_data(dev, useraddr);
2958 break;
2959 case ETHTOOL_GET_TS_INFO:
2960 rc = ethtool_get_ts_info(dev, useraddr);
2961 break;
2962 case ETHTOOL_GMODULEINFO:
2963 rc = ethtool_get_module_info(dev, useraddr);
2964 break;
2965 case ETHTOOL_GMODULEEEPROM:
2966 rc = ethtool_get_module_eeprom(dev, useraddr);
2967 break;
2968 case ETHTOOL_GTUNABLE:
2969 rc = ethtool_get_tunable(dev, useraddr);
2970 break;
2971 case ETHTOOL_STUNABLE:
2972 rc = ethtool_set_tunable(dev, useraddr);
2973 break;
2974 case ETHTOOL_GPHYSTATS:
2975 rc = ethtool_get_phy_stats(dev, useraddr);
2976 break;
2977 case ETHTOOL_PERQUEUE:
2978 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
2979 break;
2980 case ETHTOOL_GLINKSETTINGS:
2981 rc = ethtool_get_link_ksettings(dev, useraddr);
2982 break;
2983 case ETHTOOL_SLINKSETTINGS:
2984 rc = ethtool_set_link_ksettings(dev, useraddr);
2985 break;
2986 case ETHTOOL_PHY_GTUNABLE:
2987 rc = get_phy_tunable(dev, useraddr);
2988 break;
2989 case ETHTOOL_PHY_STUNABLE:
2990 rc = set_phy_tunable(dev, useraddr);
2991 break;
2992 case ETHTOOL_GFECPARAM:
2993 rc = ethtool_get_fecparam(dev, useraddr);
2994 break;
2995 case ETHTOOL_SFECPARAM:
2996 rc = ethtool_set_fecparam(dev, useraddr);
2997 break;
2998 default:
2999 rc = -EOPNOTSUPP;
3000 }
3001
3002 if (dev->ethtool_ops->complete)
3003 dev->ethtool_ops->complete(dev);
3004
3005 if (old_features != dev->features)
3006 netdev_features_change(dev);
3007 out:
3008 if (dev->dev.parent)
3009 pm_runtime_put(dev->dev.parent);
3010
3011 return rc;
3012 }
3013
3014 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3015 {
3016 struct ethtool_devlink_compat *state;
3017 u32 ethcmd;
3018 int rc;
3019
3020 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
3021 return -EFAULT;
3022
3023 state = kzalloc(sizeof(*state), GFP_KERNEL);
3024 if (!state)
3025 return -ENOMEM;
3026
3027 switch (ethcmd) {
3028 case ETHTOOL_FLASHDEV:
3029 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3030 rc = -EFAULT;
3031 goto exit_free;
3032 }
3033 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3034 break;
3035 }
3036
3037 rtnl_lock();
3038 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3039 rtnl_unlock();
3040 if (rc)
3041 goto exit_free;
3042
3043 switch (ethcmd) {
3044 case ETHTOOL_FLASHDEV:
3045 if (state->devlink)
3046 rc = devlink_compat_flash_update(state->devlink,
3047 state->efl.data);
3048 break;
3049 case ETHTOOL_GDRVINFO:
3050 if (state->devlink)
3051 devlink_compat_running_version(state->devlink,
3052 state->info.fw_version,
3053 sizeof(state->info.fw_version));
3054 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3055 rc = -EFAULT;
3056 goto exit_free;
3057 }
3058 break;
3059 }
3060
3061 exit_free:
3062 if (state->devlink)
3063 devlink_put(state->devlink);
3064 kfree(state);
3065 return rc;
3066 }
3067
3068 struct ethtool_rx_flow_key {
3069 struct flow_dissector_key_basic basic;
3070 union {
3071 struct flow_dissector_key_ipv4_addrs ipv4;
3072 struct flow_dissector_key_ipv6_addrs ipv6;
3073 };
3074 struct flow_dissector_key_ports tp;
3075 struct flow_dissector_key_ip ip;
3076 struct flow_dissector_key_vlan vlan;
3077 struct flow_dissector_key_eth_addrs eth_addrs;
3078 } __aligned(BITS_PER_LONG / 8);
3079
3080 struct ethtool_rx_flow_match {
3081 struct flow_dissector dissector;
3082 struct ethtool_rx_flow_key key;
3083 struct ethtool_rx_flow_key mask;
3084 };
3085
3086 struct ethtool_rx_flow_rule *
3087 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
3088 {
3089 const struct ethtool_rx_flow_spec *fs = input->fs;
3090 static struct in6_addr zero_addr = {};
3091 struct ethtool_rx_flow_match *match;
3092 struct ethtool_rx_flow_rule *flow;
3093 struct flow_action_entry *act;
3094
3095 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
3096 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
3097 if (!flow)
3098 return ERR_PTR(-ENOMEM);
3099
3100
3101 flow->rule = flow_rule_alloc(1);
3102 if (!flow->rule) {
3103 kfree(flow);
3104 return ERR_PTR(-ENOMEM);
3105 }
3106
3107 match = (struct ethtool_rx_flow_match *)flow->priv;
3108 flow->rule->match.dissector = &match->dissector;
3109 flow->rule->match.mask = &match->mask;
3110 flow->rule->match.key = &match->key;
3111
3112 match->mask.basic.n_proto = htons(0xffff);
3113
3114 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3115 case ETHER_FLOW: {
3116 const struct ethhdr *ether_spec, *ether_m_spec;
3117
3118 ether_spec = &fs->h_u.ether_spec;
3119 ether_m_spec = &fs->m_u.ether_spec;
3120
3121 if (!is_zero_ether_addr(ether_m_spec->h_source)) {
3122 ether_addr_copy(match->key.eth_addrs.src,
3123 ether_spec->h_source);
3124 ether_addr_copy(match->mask.eth_addrs.src,
3125 ether_m_spec->h_source);
3126 }
3127 if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
3128 ether_addr_copy(match->key.eth_addrs.dst,
3129 ether_spec->h_dest);
3130 ether_addr_copy(match->mask.eth_addrs.dst,
3131 ether_m_spec->h_dest);
3132 }
3133 if (ether_m_spec->h_proto) {
3134 match->key.basic.n_proto = ether_spec->h_proto;
3135 match->mask.basic.n_proto = ether_m_spec->h_proto;
3136 }
3137 }
3138 break;
3139 case TCP_V4_FLOW:
3140 case UDP_V4_FLOW: {
3141 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
3142
3143 match->key.basic.n_proto = htons(ETH_P_IP);
3144
3145 v4_spec = &fs->h_u.tcp_ip4_spec;
3146 v4_m_spec = &fs->m_u.tcp_ip4_spec;
3147
3148 if (v4_m_spec->ip4src) {
3149 match->key.ipv4.src = v4_spec->ip4src;
3150 match->mask.ipv4.src = v4_m_spec->ip4src;
3151 }
3152 if (v4_m_spec->ip4dst) {
3153 match->key.ipv4.dst = v4_spec->ip4dst;
3154 match->mask.ipv4.dst = v4_m_spec->ip4dst;
3155 }
3156 if (v4_m_spec->ip4src ||
3157 v4_m_spec->ip4dst) {
3158 match->dissector.used_keys |=
3159 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
3160 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
3161 offsetof(struct ethtool_rx_flow_key, ipv4);
3162 }
3163 if (v4_m_spec->psrc) {
3164 match->key.tp.src = v4_spec->psrc;
3165 match->mask.tp.src = v4_m_spec->psrc;
3166 }
3167 if (v4_m_spec->pdst) {
3168 match->key.tp.dst = v4_spec->pdst;
3169 match->mask.tp.dst = v4_m_spec->pdst;
3170 }
3171 if (v4_m_spec->psrc ||
3172 v4_m_spec->pdst) {
3173 match->dissector.used_keys |=
3174 BIT(FLOW_DISSECTOR_KEY_PORTS);
3175 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3176 offsetof(struct ethtool_rx_flow_key, tp);
3177 }
3178 if (v4_m_spec->tos) {
3179 match->key.ip.tos = v4_spec->tos;
3180 match->mask.ip.tos = v4_m_spec->tos;
3181 match->dissector.used_keys |=
3182 BIT(FLOW_DISSECTOR_KEY_IP);
3183 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3184 offsetof(struct ethtool_rx_flow_key, ip);
3185 }
3186 }
3187 break;
3188 case TCP_V6_FLOW:
3189 case UDP_V6_FLOW: {
3190 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
3191
3192 match->key.basic.n_proto = htons(ETH_P_IPV6);
3193
3194 v6_spec = &fs->h_u.tcp_ip6_spec;
3195 v6_m_spec = &fs->m_u.tcp_ip6_spec;
3196 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
3197 memcpy(&match->key.ipv6.src, v6_spec->ip6src,
3198 sizeof(match->key.ipv6.src));
3199 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
3200 sizeof(match->mask.ipv6.src));
3201 }
3202 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3203 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
3204 sizeof(match->key.ipv6.dst));
3205 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
3206 sizeof(match->mask.ipv6.dst));
3207 }
3208 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
3209 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
3210 match->dissector.used_keys |=
3211 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
3212 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
3213 offsetof(struct ethtool_rx_flow_key, ipv6);
3214 }
3215 if (v6_m_spec->psrc) {
3216 match->key.tp.src = v6_spec->psrc;
3217 match->mask.tp.src = v6_m_spec->psrc;
3218 }
3219 if (v6_m_spec->pdst) {
3220 match->key.tp.dst = v6_spec->pdst;
3221 match->mask.tp.dst = v6_m_spec->pdst;
3222 }
3223 if (v6_m_spec->psrc ||
3224 v6_m_spec->pdst) {
3225 match->dissector.used_keys |=
3226 BIT(FLOW_DISSECTOR_KEY_PORTS);
3227 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
3228 offsetof(struct ethtool_rx_flow_key, tp);
3229 }
3230 if (v6_m_spec->tclass) {
3231 match->key.ip.tos = v6_spec->tclass;
3232 match->mask.ip.tos = v6_m_spec->tclass;
3233 match->dissector.used_keys |=
3234 BIT(FLOW_DISSECTOR_KEY_IP);
3235 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
3236 offsetof(struct ethtool_rx_flow_key, ip);
3237 }
3238 }
3239 break;
3240 default:
3241 ethtool_rx_flow_rule_destroy(flow);
3242 return ERR_PTR(-EINVAL);
3243 }
3244
3245 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
3246 case TCP_V4_FLOW:
3247 case TCP_V6_FLOW:
3248 match->key.basic.ip_proto = IPPROTO_TCP;
3249 match->mask.basic.ip_proto = 0xff;
3250 break;
3251 case UDP_V4_FLOW:
3252 case UDP_V6_FLOW:
3253 match->key.basic.ip_proto = IPPROTO_UDP;
3254 match->mask.basic.ip_proto = 0xff;
3255 break;
3256 }
3257
3258 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
3259 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
3260 offsetof(struct ethtool_rx_flow_key, basic);
3261
3262 if (fs->flow_type & FLOW_EXT) {
3263 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3264 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3265
3266 if (ext_m_spec->vlan_etype) {
3267 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
3268 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
3269 }
3270
3271 if (ext_m_spec->vlan_tci) {
3272 match->key.vlan.vlan_id =
3273 ntohs(ext_h_spec->vlan_tci) & 0x0fff;
3274 match->mask.vlan.vlan_id =
3275 ntohs(ext_m_spec->vlan_tci) & 0x0fff;
3276
3277 match->key.vlan.vlan_dei =
3278 !!(ext_h_spec->vlan_tci & htons(0x1000));
3279 match->mask.vlan.vlan_dei =
3280 !!(ext_m_spec->vlan_tci & htons(0x1000));
3281
3282 match->key.vlan.vlan_priority =
3283 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
3284 match->mask.vlan.vlan_priority =
3285 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
3286 }
3287
3288 if (ext_m_spec->vlan_etype ||
3289 ext_m_spec->vlan_tci) {
3290 match->dissector.used_keys |=
3291 BIT(FLOW_DISSECTOR_KEY_VLAN);
3292 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
3293 offsetof(struct ethtool_rx_flow_key, vlan);
3294 }
3295 }
3296 if (fs->flow_type & FLOW_MAC_EXT) {
3297 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
3298 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
3299
3300 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
3301 ETH_ALEN);
3302 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
3303 ETH_ALEN);
3304
3305 match->dissector.used_keys |=
3306 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
3307 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
3308 offsetof(struct ethtool_rx_flow_key, eth_addrs);
3309 }
3310
3311 act = &flow->rule->action.entries[0];
3312 switch (fs->ring_cookie) {
3313 case RX_CLS_FLOW_DISC:
3314 act->id = FLOW_ACTION_DROP;
3315 break;
3316 case RX_CLS_FLOW_WAKE:
3317 act->id = FLOW_ACTION_WAKE;
3318 break;
3319 default:
3320 act->id = FLOW_ACTION_QUEUE;
3321 if (fs->flow_type & FLOW_RSS)
3322 act->queue.ctx = input->rss_ctx;
3323
3324 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
3325 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
3326 break;
3327 }
3328
3329 return flow;
3330 }
3331 EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
3332
3333 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
3334 {
3335 kfree(flow->rule);
3336 kfree(flow);
3337 }
3338 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);