Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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