Linux Audio

Check our new training course

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