at v4.18-rc8 2871 lines 75 kB view raw
1/* 2 * net/core/ethtool.c - Ethtool ioctl handler 3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx> 4 * 5 * This file is where we call all the ethtool_ops commands to get 6 * the information ethtool needs. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 */ 13 14#include <linux/module.h> 15#include <linux/types.h> 16#include <linux/capability.h> 17#include <linux/errno.h> 18#include <linux/ethtool.h> 19#include <linux/netdevice.h> 20#include <linux/net_tstamp.h> 21#include <linux/phy.h> 22#include <linux/bitops.h> 23#include <linux/uaccess.h> 24#include <linux/vmalloc.h> 25#include <linux/sfp.h> 26#include <linux/slab.h> 27#include <linux/rtnetlink.h> 28#include <linux/sched/signal.h> 29#include <linux/net.h> 30 31/* 32 * Some useful ethtool_ops methods that're device independent. 33 * If we find that all drivers want to do the same thing here, 34 * we can turn these into dev_() function calls. 35 */ 36 37u32 ethtool_op_get_link(struct net_device *dev) 38{ 39 return netif_carrier_ok(dev) ? 1 : 0; 40} 41EXPORT_SYMBOL(ethtool_op_get_link); 42 43int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) 44{ 45 info->so_timestamping = 46 SOF_TIMESTAMPING_TX_SOFTWARE | 47 SOF_TIMESTAMPING_RX_SOFTWARE | 48 SOF_TIMESTAMPING_SOFTWARE; 49 info->phc_index = -1; 50 return 0; 51} 52EXPORT_SYMBOL(ethtool_op_get_ts_info); 53 54/* Handlers for each ethtool command */ 55 56#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32) 57 58static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = { 59 [NETIF_F_SG_BIT] = "tx-scatter-gather", 60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4", 61 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic", 62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6", 63 [NETIF_F_HIGHDMA_BIT] = "highdma", 64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist", 65 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert", 66 67 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse", 68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter", 69 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert", 70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse", 71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter", 72 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged", 73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation", 74 [NETIF_F_LLTX_BIT] = "tx-lockless", 75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local", 76 [NETIF_F_GRO_BIT] = "rx-gro", 77 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw", 78 [NETIF_F_LRO_BIT] = "rx-lro", 79 80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation", 81 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust", 82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation", 83 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation", 84 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation", 85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation", 86 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation", 87 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation", 88 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation", 89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation", 90 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation", 91 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation", 92 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial", 93 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation", 94 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation", 95 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation", 96 97 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc", 98 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp", 99 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu", 100 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter", 101 [NETIF_F_RXHASH_BIT] = "rx-hashing", 102 [NETIF_F_RXCSUM_BIT] = "rx-checksum", 103 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy", 104 [NETIF_F_LOOPBACK_BIT] = "loopback", 105 [NETIF_F_RXFCS_BIT] = "rx-fcs", 106 [NETIF_F_RXALL_BIT] = "rx-all", 107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload", 108 [NETIF_F_HW_TC_BIT] = "hw-tc-offload", 109 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload", 110 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload", 111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload", 112 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record", 113 [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload", 114}; 115 116static const char 117rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = { 118 [ETH_RSS_HASH_TOP_BIT] = "toeplitz", 119 [ETH_RSS_HASH_XOR_BIT] = "xor", 120 [ETH_RSS_HASH_CRC32_BIT] = "crc32", 121}; 122 123static const char 124tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = { 125 [ETHTOOL_ID_UNSPEC] = "Unspec", 126 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak", 127 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak", 128 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout", 129}; 130 131static const char 132phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { 133 [ETHTOOL_ID_UNSPEC] = "Unspec", 134 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift", 135}; 136 137static int ethtool_get_features(struct net_device *dev, void __user *useraddr) 138{ 139 struct ethtool_gfeatures cmd = { 140 .cmd = ETHTOOL_GFEATURES, 141 .size = ETHTOOL_DEV_FEATURE_WORDS, 142 }; 143 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; 144 u32 __user *sizeaddr; 145 u32 copy_size; 146 int i; 147 148 /* in case feature bits run out again */ 149 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t)); 150 151 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { 152 features[i].available = (u32)(dev->hw_features >> (32 * i)); 153 features[i].requested = (u32)(dev->wanted_features >> (32 * i)); 154 features[i].active = (u32)(dev->features >> (32 * i)); 155 features[i].never_changed = 156 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i)); 157 } 158 159 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size); 160 if (get_user(copy_size, sizeaddr)) 161 return -EFAULT; 162 163 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS) 164 copy_size = ETHTOOL_DEV_FEATURE_WORDS; 165 166 if (copy_to_user(useraddr, &cmd, sizeof(cmd))) 167 return -EFAULT; 168 useraddr += sizeof(cmd); 169 if (copy_to_user(useraddr, features, copy_size * sizeof(*features))) 170 return -EFAULT; 171 172 return 0; 173} 174 175static int ethtool_set_features(struct net_device *dev, void __user *useraddr) 176{ 177 struct ethtool_sfeatures cmd; 178 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; 179 netdev_features_t wanted = 0, valid = 0; 180 int i, ret = 0; 181 182 if (copy_from_user(&cmd, useraddr, sizeof(cmd))) 183 return -EFAULT; 184 useraddr += sizeof(cmd); 185 186 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) 187 return -EINVAL; 188 189 if (copy_from_user(features, useraddr, sizeof(features))) 190 return -EFAULT; 191 192 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { 193 valid |= (netdev_features_t)features[i].valid << (32 * i); 194 wanted |= (netdev_features_t)features[i].requested << (32 * i); 195 } 196 197 if (valid & ~NETIF_F_ETHTOOL_BITS) 198 return -EINVAL; 199 200 if (valid & ~dev->hw_features) { 201 valid &= dev->hw_features; 202 ret |= ETHTOOL_F_UNSUPPORTED; 203 } 204 205 dev->wanted_features &= ~valid; 206 dev->wanted_features |= wanted & valid; 207 __netdev_update_features(dev); 208 209 if ((dev->wanted_features ^ dev->features) & valid) 210 ret |= ETHTOOL_F_WISH; 211 212 return ret; 213} 214 215static int __ethtool_get_sset_count(struct net_device *dev, int sset) 216{ 217 const struct ethtool_ops *ops = dev->ethtool_ops; 218 219 if (sset == ETH_SS_FEATURES) 220 return ARRAY_SIZE(netdev_features_strings); 221 222 if (sset == ETH_SS_RSS_HASH_FUNCS) 223 return ARRAY_SIZE(rss_hash_func_strings); 224 225 if (sset == ETH_SS_TUNABLES) 226 return ARRAY_SIZE(tunable_strings); 227 228 if (sset == ETH_SS_PHY_TUNABLES) 229 return ARRAY_SIZE(phy_tunable_strings); 230 231 if (sset == ETH_SS_PHY_STATS && dev->phydev && 232 !ops->get_ethtool_phy_stats) 233 return phy_ethtool_get_sset_count(dev->phydev); 234 235 if (ops->get_sset_count && ops->get_strings) 236 return ops->get_sset_count(dev, sset); 237 else 238 return -EOPNOTSUPP; 239} 240 241static void __ethtool_get_strings(struct net_device *dev, 242 u32 stringset, u8 *data) 243{ 244 const struct ethtool_ops *ops = dev->ethtool_ops; 245 246 if (stringset == ETH_SS_FEATURES) 247 memcpy(data, netdev_features_strings, 248 sizeof(netdev_features_strings)); 249 else if (stringset == ETH_SS_RSS_HASH_FUNCS) 250 memcpy(data, rss_hash_func_strings, 251 sizeof(rss_hash_func_strings)); 252 else if (stringset == ETH_SS_TUNABLES) 253 memcpy(data, tunable_strings, sizeof(tunable_strings)); 254 else if (stringset == ETH_SS_PHY_TUNABLES) 255 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings)); 256 else if (stringset == ETH_SS_PHY_STATS && dev->phydev && 257 !ops->get_ethtool_phy_stats) 258 phy_ethtool_get_strings(dev->phydev, data); 259 else 260 /* ops->get_strings is valid because checked earlier */ 261 ops->get_strings(dev, stringset, data); 262} 263 264static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) 265{ 266 /* feature masks of legacy discrete ethtool ops */ 267 268 switch (eth_cmd) { 269 case ETHTOOL_GTXCSUM: 270 case ETHTOOL_STXCSUM: 271 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC; 272 case ETHTOOL_GRXCSUM: 273 case ETHTOOL_SRXCSUM: 274 return NETIF_F_RXCSUM; 275 case ETHTOOL_GSG: 276 case ETHTOOL_SSG: 277 return NETIF_F_SG; 278 case ETHTOOL_GTSO: 279 case ETHTOOL_STSO: 280 return NETIF_F_ALL_TSO; 281 case ETHTOOL_GGSO: 282 case ETHTOOL_SGSO: 283 return NETIF_F_GSO; 284 case ETHTOOL_GGRO: 285 case ETHTOOL_SGRO: 286 return NETIF_F_GRO; 287 default: 288 BUG(); 289 } 290} 291 292static int ethtool_get_one_feature(struct net_device *dev, 293 char __user *useraddr, u32 ethcmd) 294{ 295 netdev_features_t mask = ethtool_get_feature_mask(ethcmd); 296 struct ethtool_value edata = { 297 .cmd = ethcmd, 298 .data = !!(dev->features & mask), 299 }; 300 301 if (copy_to_user(useraddr, &edata, sizeof(edata))) 302 return -EFAULT; 303 return 0; 304} 305 306static int ethtool_set_one_feature(struct net_device *dev, 307 void __user *useraddr, u32 ethcmd) 308{ 309 struct ethtool_value edata; 310 netdev_features_t mask; 311 312 if (copy_from_user(&edata, useraddr, sizeof(edata))) 313 return -EFAULT; 314 315 mask = ethtool_get_feature_mask(ethcmd); 316 mask &= dev->hw_features; 317 if (!mask) 318 return -EOPNOTSUPP; 319 320 if (edata.data) 321 dev->wanted_features |= mask; 322 else 323 dev->wanted_features &= ~mask; 324 325 __netdev_update_features(dev); 326 327 return 0; 328} 329 330#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ 331 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) 332#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ 333 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ 334 NETIF_F_RXHASH) 335 336static u32 __ethtool_get_flags(struct net_device *dev) 337{ 338 u32 flags = 0; 339 340 if (dev->features & NETIF_F_LRO) 341 flags |= ETH_FLAG_LRO; 342 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) 343 flags |= ETH_FLAG_RXVLAN; 344 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) 345 flags |= ETH_FLAG_TXVLAN; 346 if (dev->features & NETIF_F_NTUPLE) 347 flags |= ETH_FLAG_NTUPLE; 348 if (dev->features & NETIF_F_RXHASH) 349 flags |= ETH_FLAG_RXHASH; 350 351 return flags; 352} 353 354static int __ethtool_set_flags(struct net_device *dev, u32 data) 355{ 356 netdev_features_t features = 0, changed; 357 358 if (data & ~ETH_ALL_FLAGS) 359 return -EINVAL; 360 361 if (data & ETH_FLAG_LRO) 362 features |= NETIF_F_LRO; 363 if (data & ETH_FLAG_RXVLAN) 364 features |= NETIF_F_HW_VLAN_CTAG_RX; 365 if (data & ETH_FLAG_TXVLAN) 366 features |= NETIF_F_HW_VLAN_CTAG_TX; 367 if (data & ETH_FLAG_NTUPLE) 368 features |= NETIF_F_NTUPLE; 369 if (data & ETH_FLAG_RXHASH) 370 features |= NETIF_F_RXHASH; 371 372 /* allow changing only bits set in hw_features */ 373 changed = (features ^ dev->features) & ETH_ALL_FEATURES; 374 if (changed & ~dev->hw_features) 375 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; 376 377 dev->wanted_features = 378 (dev->wanted_features & ~changed) | (features & changed); 379 380 __netdev_update_features(dev); 381 382 return 0; 383} 384 385/* Given two link masks, AND them together and save the result in dst. */ 386void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst, 387 struct ethtool_link_ksettings *src) 388{ 389 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS); 390 unsigned int idx = 0; 391 392 for (; idx < size; idx++) { 393 dst->link_modes.supported[idx] &= 394 src->link_modes.supported[idx]; 395 dst->link_modes.advertising[idx] &= 396 src->link_modes.advertising[idx]; 397 } 398} 399EXPORT_SYMBOL(ethtool_intersect_link_masks); 400 401void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst, 402 u32 legacy_u32) 403{ 404 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); 405 dst[0] = legacy_u32; 406} 407EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode); 408 409/* return false if src had higher bits set. lower bits always updated. */ 410bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32, 411 const unsigned long *src) 412{ 413 bool retval = true; 414 415 /* TODO: following test will soon always be true */ 416 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) { 417 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext); 418 419 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS); 420 bitmap_fill(ext, 32); 421 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS); 422 if (bitmap_intersects(ext, src, 423 __ETHTOOL_LINK_MODE_MASK_NBITS)) { 424 /* src mask goes beyond bit 31 */ 425 retval = false; 426 } 427 } 428 *legacy_u32 = src[0]; 429 return retval; 430} 431EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32); 432 433/* return false if legacy contained non-0 deprecated fields 434 * maxtxpkt/maxrxpkt. rest of ksettings always updated 435 */ 436static bool 437convert_legacy_settings_to_link_ksettings( 438 struct ethtool_link_ksettings *link_ksettings, 439 const struct ethtool_cmd *legacy_settings) 440{ 441 bool retval = true; 442 443 memset(link_ksettings, 0, sizeof(*link_ksettings)); 444 445 /* This is used to tell users that driver is still using these 446 * deprecated legacy fields, and they should not use 447 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS 448 */ 449 if (legacy_settings->maxtxpkt || 450 legacy_settings->maxrxpkt) 451 retval = false; 452 453 ethtool_convert_legacy_u32_to_link_mode( 454 link_ksettings->link_modes.supported, 455 legacy_settings->supported); 456 ethtool_convert_legacy_u32_to_link_mode( 457 link_ksettings->link_modes.advertising, 458 legacy_settings->advertising); 459 ethtool_convert_legacy_u32_to_link_mode( 460 link_ksettings->link_modes.lp_advertising, 461 legacy_settings->lp_advertising); 462 link_ksettings->base.speed 463 = ethtool_cmd_speed(legacy_settings); 464 link_ksettings->base.duplex 465 = legacy_settings->duplex; 466 link_ksettings->base.port 467 = legacy_settings->port; 468 link_ksettings->base.phy_address 469 = legacy_settings->phy_address; 470 link_ksettings->base.autoneg 471 = legacy_settings->autoneg; 472 link_ksettings->base.mdio_support 473 = legacy_settings->mdio_support; 474 link_ksettings->base.eth_tp_mdix 475 = legacy_settings->eth_tp_mdix; 476 link_ksettings->base.eth_tp_mdix_ctrl 477 = legacy_settings->eth_tp_mdix_ctrl; 478 return retval; 479} 480 481/* return false if ksettings link modes had higher bits 482 * set. legacy_settings always updated (best effort) 483 */ 484static bool 485convert_link_ksettings_to_legacy_settings( 486 struct ethtool_cmd *legacy_settings, 487 const struct ethtool_link_ksettings *link_ksettings) 488{ 489 bool retval = true; 490 491 memset(legacy_settings, 0, sizeof(*legacy_settings)); 492 /* this also clears the deprecated fields in legacy structure: 493 * __u8 transceiver; 494 * __u32 maxtxpkt; 495 * __u32 maxrxpkt; 496 */ 497 498 retval &= ethtool_convert_link_mode_to_legacy_u32( 499 &legacy_settings->supported, 500 link_ksettings->link_modes.supported); 501 retval &= ethtool_convert_link_mode_to_legacy_u32( 502 &legacy_settings->advertising, 503 link_ksettings->link_modes.advertising); 504 retval &= ethtool_convert_link_mode_to_legacy_u32( 505 &legacy_settings->lp_advertising, 506 link_ksettings->link_modes.lp_advertising); 507 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed); 508 legacy_settings->duplex 509 = link_ksettings->base.duplex; 510 legacy_settings->port 511 = link_ksettings->base.port; 512 legacy_settings->phy_address 513 = link_ksettings->base.phy_address; 514 legacy_settings->autoneg 515 = link_ksettings->base.autoneg; 516 legacy_settings->mdio_support 517 = link_ksettings->base.mdio_support; 518 legacy_settings->eth_tp_mdix 519 = link_ksettings->base.eth_tp_mdix; 520 legacy_settings->eth_tp_mdix_ctrl 521 = link_ksettings->base.eth_tp_mdix_ctrl; 522 legacy_settings->transceiver 523 = link_ksettings->base.transceiver; 524 return retval; 525} 526 527/* number of 32-bit words to store the user's link mode bitmaps */ 528#define __ETHTOOL_LINK_MODE_MASK_NU32 \ 529 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32) 530 531/* layout of the struct passed from/to userland */ 532struct ethtool_link_usettings { 533 struct ethtool_link_settings base; 534 struct { 535 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32]; 536 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; 537 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; 538 } link_modes; 539}; 540 541/* Internal kernel helper to query a device ethtool_link_settings. 542 * 543 * Backward compatibility note: for compatibility with legacy drivers 544 * that implement only the ethtool_cmd API, this has to work with both 545 * drivers implementing get_link_ksettings API and drivers 546 * implementing get_settings API. When drivers implement get_settings 547 * and report ethtool_cmd deprecated fields 548 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored 549 * because the resulting struct ethtool_link_settings does not report them. 550 */ 551int __ethtool_get_link_ksettings(struct net_device *dev, 552 struct ethtool_link_ksettings *link_ksettings) 553{ 554 int err; 555 struct ethtool_cmd cmd; 556 557 ASSERT_RTNL(); 558 559 if (dev->ethtool_ops->get_link_ksettings) { 560 memset(link_ksettings, 0, sizeof(*link_ksettings)); 561 return dev->ethtool_ops->get_link_ksettings(dev, 562 link_ksettings); 563 } 564 565 /* driver doesn't support %ethtool_link_ksettings API. revert to 566 * legacy %ethtool_cmd API, unless it's not supported either. 567 * TODO: remove when ethtool_ops::get_settings disappears internally 568 */ 569 if (!dev->ethtool_ops->get_settings) 570 return -EOPNOTSUPP; 571 572 memset(&cmd, 0, sizeof(cmd)); 573 cmd.cmd = ETHTOOL_GSET; 574 err = dev->ethtool_ops->get_settings(dev, &cmd); 575 if (err < 0) 576 return err; 577 578 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt 579 */ 580 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd); 581 return err; 582} 583EXPORT_SYMBOL(__ethtool_get_link_ksettings); 584 585/* convert ethtool_link_usettings in user space to a kernel internal 586 * ethtool_link_ksettings. return 0 on success, errno on error. 587 */ 588static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to, 589 const void __user *from) 590{ 591 struct ethtool_link_usettings link_usettings; 592 593 if (copy_from_user(&link_usettings, from, sizeof(link_usettings))) 594 return -EFAULT; 595 596 memcpy(&to->base, &link_usettings.base, sizeof(to->base)); 597 bitmap_from_arr32(to->link_modes.supported, 598 link_usettings.link_modes.supported, 599 __ETHTOOL_LINK_MODE_MASK_NBITS); 600 bitmap_from_arr32(to->link_modes.advertising, 601 link_usettings.link_modes.advertising, 602 __ETHTOOL_LINK_MODE_MASK_NBITS); 603 bitmap_from_arr32(to->link_modes.lp_advertising, 604 link_usettings.link_modes.lp_advertising, 605 __ETHTOOL_LINK_MODE_MASK_NBITS); 606 607 return 0; 608} 609 610/* convert a kernel internal ethtool_link_ksettings to 611 * ethtool_link_usettings in user space. return 0 on success, errno on 612 * error. 613 */ 614static int 615store_link_ksettings_for_user(void __user *to, 616 const struct ethtool_link_ksettings *from) 617{ 618 struct ethtool_link_usettings link_usettings; 619 620 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings)); 621 bitmap_to_arr32(link_usettings.link_modes.supported, 622 from->link_modes.supported, 623 __ETHTOOL_LINK_MODE_MASK_NBITS); 624 bitmap_to_arr32(link_usettings.link_modes.advertising, 625 from->link_modes.advertising, 626 __ETHTOOL_LINK_MODE_MASK_NBITS); 627 bitmap_to_arr32(link_usettings.link_modes.lp_advertising, 628 from->link_modes.lp_advertising, 629 __ETHTOOL_LINK_MODE_MASK_NBITS); 630 631 if (copy_to_user(to, &link_usettings, sizeof(link_usettings))) 632 return -EFAULT; 633 634 return 0; 635} 636 637/* Query device for its ethtool_link_settings. 638 * 639 * Backward compatibility note: this function must fail when driver 640 * does not implement ethtool::get_link_ksettings, even if legacy 641 * ethtool_ops::get_settings is implemented. This tells new versions 642 * of ethtool that they should use the legacy API %ETHTOOL_GSET for 643 * this driver, so that they can correctly access the ethtool_cmd 644 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver 645 * implements ethtool_ops::get_settings anymore. 646 */ 647static int ethtool_get_link_ksettings(struct net_device *dev, 648 void __user *useraddr) 649{ 650 int err = 0; 651 struct ethtool_link_ksettings link_ksettings; 652 653 ASSERT_RTNL(); 654 655 if (!dev->ethtool_ops->get_link_ksettings) 656 return -EOPNOTSUPP; 657 658 /* handle bitmap nbits handshake */ 659 if (copy_from_user(&link_ksettings.base, useraddr, 660 sizeof(link_ksettings.base))) 661 return -EFAULT; 662 663 if (__ETHTOOL_LINK_MODE_MASK_NU32 664 != link_ksettings.base.link_mode_masks_nwords) { 665 /* wrong link mode nbits requested */ 666 memset(&link_ksettings, 0, sizeof(link_ksettings)); 667 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; 668 /* send back number of words required as negative val */ 669 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX, 670 "need too many bits for link modes!"); 671 link_ksettings.base.link_mode_masks_nwords 672 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32); 673 674 /* copy the base fields back to user, not the link 675 * mode bitmaps 676 */ 677 if (copy_to_user(useraddr, &link_ksettings.base, 678 sizeof(link_ksettings.base))) 679 return -EFAULT; 680 681 return 0; 682 } 683 684 /* handshake successful: user/kernel agree on 685 * link_mode_masks_nwords 686 */ 687 688 memset(&link_ksettings, 0, sizeof(link_ksettings)); 689 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); 690 if (err < 0) 691 return err; 692 693 /* make sure we tell the right values to user */ 694 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; 695 link_ksettings.base.link_mode_masks_nwords 696 = __ETHTOOL_LINK_MODE_MASK_NU32; 697 698 return store_link_ksettings_for_user(useraddr, &link_ksettings); 699} 700 701/* Update device ethtool_link_settings. 702 * 703 * Backward compatibility note: this function must fail when driver 704 * does not implement ethtool::set_link_ksettings, even if legacy 705 * ethtool_ops::set_settings is implemented. This tells new versions 706 * of ethtool that they should use the legacy API %ETHTOOL_SSET for 707 * this driver, so that they can correctly update the ethtool_cmd 708 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver 709 * implements ethtool_ops::get_settings anymore. 710 */ 711static int ethtool_set_link_ksettings(struct net_device *dev, 712 void __user *useraddr) 713{ 714 int err; 715 struct ethtool_link_ksettings link_ksettings; 716 717 ASSERT_RTNL(); 718 719 if (!dev->ethtool_ops->set_link_ksettings) 720 return -EOPNOTSUPP; 721 722 /* make sure nbits field has expected value */ 723 if (copy_from_user(&link_ksettings.base, useraddr, 724 sizeof(link_ksettings.base))) 725 return -EFAULT; 726 727 if (__ETHTOOL_LINK_MODE_MASK_NU32 728 != link_ksettings.base.link_mode_masks_nwords) 729 return -EINVAL; 730 731 /* copy the whole structure, now that we know it has expected 732 * format 733 */ 734 err = load_link_ksettings_from_user(&link_ksettings, useraddr); 735 if (err) 736 return err; 737 738 /* re-check nwords field, just in case */ 739 if (__ETHTOOL_LINK_MODE_MASK_NU32 740 != link_ksettings.base.link_mode_masks_nwords) 741 return -EINVAL; 742 743 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); 744} 745 746/* Query device for its ethtool_cmd settings. 747 * 748 * Backward compatibility note: for compatibility with legacy ethtool, 749 * this has to work with both drivers implementing get_link_ksettings 750 * API and drivers implementing get_settings API. When drivers 751 * implement get_link_ksettings and report higher link mode bits, a 752 * kernel warning is logged once (with name of 1st driver/device) to 753 * recommend user to upgrade ethtool, but the command is successful 754 * (only the lower link mode bits reported back to user). 755 */ 756static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) 757{ 758 struct ethtool_cmd cmd; 759 760 ASSERT_RTNL(); 761 762 if (dev->ethtool_ops->get_link_ksettings) { 763 /* First, use link_ksettings API if it is supported */ 764 int err; 765 struct ethtool_link_ksettings link_ksettings; 766 767 memset(&link_ksettings, 0, sizeof(link_ksettings)); 768 err = dev->ethtool_ops->get_link_ksettings(dev, 769 &link_ksettings); 770 if (err < 0) 771 return err; 772 convert_link_ksettings_to_legacy_settings(&cmd, 773 &link_ksettings); 774 775 /* send a sensible cmd tag back to user */ 776 cmd.cmd = ETHTOOL_GSET; 777 } else { 778 /* driver doesn't support %ethtool_link_ksettings 779 * API. revert to legacy %ethtool_cmd API, unless it's 780 * not supported either. 781 */ 782 int err; 783 784 if (!dev->ethtool_ops->get_settings) 785 return -EOPNOTSUPP; 786 787 memset(&cmd, 0, sizeof(cmd)); 788 cmd.cmd = ETHTOOL_GSET; 789 err = dev->ethtool_ops->get_settings(dev, &cmd); 790 if (err < 0) 791 return err; 792 } 793 794 if (copy_to_user(useraddr, &cmd, sizeof(cmd))) 795 return -EFAULT; 796 797 return 0; 798} 799 800/* Update device link settings with given ethtool_cmd. 801 * 802 * Backward compatibility note: for compatibility with legacy ethtool, 803 * this has to work with both drivers implementing set_link_ksettings 804 * API and drivers implementing set_settings API. When drivers 805 * implement set_link_ksettings and user's request updates deprecated 806 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel 807 * warning is logged once (with name of 1st driver/device) to 808 * recommend user to upgrade ethtool, and the request is rejected. 809 */ 810static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) 811{ 812 struct ethtool_cmd cmd; 813 814 ASSERT_RTNL(); 815 816 if (copy_from_user(&cmd, useraddr, sizeof(cmd))) 817 return -EFAULT; 818 819 /* first, try new %ethtool_link_ksettings API. */ 820 if (dev->ethtool_ops->set_link_ksettings) { 821 struct ethtool_link_ksettings link_ksettings; 822 823 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, 824 &cmd)) 825 return -EINVAL; 826 827 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS; 828 link_ksettings.base.link_mode_masks_nwords 829 = __ETHTOOL_LINK_MODE_MASK_NU32; 830 return dev->ethtool_ops->set_link_ksettings(dev, 831 &link_ksettings); 832 } 833 834 /* legacy %ethtool_cmd API */ 835 836 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings 837 * disappears internally 838 */ 839 840 if (!dev->ethtool_ops->set_settings) 841 return -EOPNOTSUPP; 842 843 return dev->ethtool_ops->set_settings(dev, &cmd); 844} 845 846static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev, 847 void __user *useraddr) 848{ 849 struct ethtool_drvinfo info; 850 const struct ethtool_ops *ops = dev->ethtool_ops; 851 852 memset(&info, 0, sizeof(info)); 853 info.cmd = ETHTOOL_GDRVINFO; 854 if (ops->get_drvinfo) { 855 ops->get_drvinfo(dev, &info); 856 } else if (dev->dev.parent && dev->dev.parent->driver) { 857 strlcpy(info.bus_info, dev_name(dev->dev.parent), 858 sizeof(info.bus_info)); 859 strlcpy(info.driver, dev->dev.parent->driver->name, 860 sizeof(info.driver)); 861 } else { 862 return -EOPNOTSUPP; 863 } 864 865 /* 866 * this method of obtaining string set info is deprecated; 867 * Use ETHTOOL_GSSET_INFO instead. 868 */ 869 if (ops->get_sset_count) { 870 int rc; 871 872 rc = ops->get_sset_count(dev, ETH_SS_TEST); 873 if (rc >= 0) 874 info.testinfo_len = rc; 875 rc = ops->get_sset_count(dev, ETH_SS_STATS); 876 if (rc >= 0) 877 info.n_stats = rc; 878 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); 879 if (rc >= 0) 880 info.n_priv_flags = rc; 881 } 882 if (ops->get_regs_len) 883 info.regdump_len = ops->get_regs_len(dev); 884 if (ops->get_eeprom_len) 885 info.eedump_len = ops->get_eeprom_len(dev); 886 887 if (copy_to_user(useraddr, &info, sizeof(info))) 888 return -EFAULT; 889 return 0; 890} 891 892static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev, 893 void __user *useraddr) 894{ 895 struct ethtool_sset_info info; 896 u64 sset_mask; 897 int i, idx = 0, n_bits = 0, ret, rc; 898 u32 *info_buf = NULL; 899 900 if (copy_from_user(&info, useraddr, sizeof(info))) 901 return -EFAULT; 902 903 /* store copy of mask, because we zero struct later on */ 904 sset_mask = info.sset_mask; 905 if (!sset_mask) 906 return 0; 907 908 /* calculate size of return buffer */ 909 n_bits = hweight64(sset_mask); 910 911 memset(&info, 0, sizeof(info)); 912 info.cmd = ETHTOOL_GSSET_INFO; 913 914 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER); 915 if (!info_buf) 916 return -ENOMEM; 917 918 /* 919 * fill return buffer based on input bitmask and successful 920 * get_sset_count return 921 */ 922 for (i = 0; i < 64; i++) { 923 if (!(sset_mask & (1ULL << i))) 924 continue; 925 926 rc = __ethtool_get_sset_count(dev, i); 927 if (rc >= 0) { 928 info.sset_mask |= (1ULL << i); 929 info_buf[idx++] = rc; 930 } 931 } 932 933 ret = -EFAULT; 934 if (copy_to_user(useraddr, &info, sizeof(info))) 935 goto out; 936 937 useraddr += offsetof(struct ethtool_sset_info, data); 938 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32))) 939 goto out; 940 941 ret = 0; 942 943out: 944 kfree(info_buf); 945 return ret; 946} 947 948static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, 949 u32 cmd, void __user *useraddr) 950{ 951 struct ethtool_rxnfc info; 952 size_t info_size = sizeof(info); 953 int rc; 954 955 if (!dev->ethtool_ops->set_rxnfc) 956 return -EOPNOTSUPP; 957 958 /* struct ethtool_rxnfc was originally defined for 959 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data 960 * members. User-space might still be using that 961 * definition. */ 962 if (cmd == ETHTOOL_SRXFH) 963 info_size = (offsetof(struct ethtool_rxnfc, data) + 964 sizeof(info.data)); 965 966 if (copy_from_user(&info, useraddr, info_size)) 967 return -EFAULT; 968 969 rc = dev->ethtool_ops->set_rxnfc(dev, &info); 970 if (rc) 971 return rc; 972 973 if (cmd == ETHTOOL_SRXCLSRLINS && 974 copy_to_user(useraddr, &info, info_size)) 975 return -EFAULT; 976 977 return 0; 978} 979 980static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, 981 u32 cmd, void __user *useraddr) 982{ 983 struct ethtool_rxnfc info; 984 size_t info_size = sizeof(info); 985 const struct ethtool_ops *ops = dev->ethtool_ops; 986 int ret; 987 void *rule_buf = NULL; 988 989 if (!ops->get_rxnfc) 990 return -EOPNOTSUPP; 991 992 /* struct ethtool_rxnfc was originally defined for 993 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data 994 * members. User-space might still be using that 995 * definition. */ 996 if (cmd == ETHTOOL_GRXFH) 997 info_size = (offsetof(struct ethtool_rxnfc, data) + 998 sizeof(info.data)); 999 1000 if (copy_from_user(&info, useraddr, info_size)) 1001 return -EFAULT; 1002 1003 /* If FLOW_RSS was requested then user-space must be using the 1004 * new definition, as FLOW_RSS is newer. 1005 */ 1006 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) { 1007 info_size = sizeof(info); 1008 if (copy_from_user(&info, useraddr, info_size)) 1009 return -EFAULT; 1010 /* Since malicious users may modify the original data, 1011 * we need to check whether FLOW_RSS is still requested. 1012 */ 1013 if (!(info.flow_type & FLOW_RSS)) 1014 return -EINVAL; 1015 } 1016 1017 if (info.cmd == ETHTOOL_GRXCLSRLALL) { 1018 if (info.rule_cnt > 0) { 1019 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) 1020 rule_buf = kcalloc(info.rule_cnt, sizeof(u32), 1021 GFP_USER); 1022 if (!rule_buf) 1023 return -ENOMEM; 1024 } 1025 } 1026 1027 ret = ops->get_rxnfc(dev, &info, rule_buf); 1028 if (ret < 0) 1029 goto err_out; 1030 1031 ret = -EFAULT; 1032 if (copy_to_user(useraddr, &info, info_size)) 1033 goto err_out; 1034 1035 if (rule_buf) { 1036 useraddr += offsetof(struct ethtool_rxnfc, rule_locs); 1037 if (copy_to_user(useraddr, rule_buf, 1038 info.rule_cnt * sizeof(u32))) 1039 goto err_out; 1040 } 1041 ret = 0; 1042 1043err_out: 1044 kfree(rule_buf); 1045 1046 return ret; 1047} 1048 1049static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, 1050 struct ethtool_rxnfc *rx_rings, 1051 u32 size) 1052{ 1053 int i; 1054 1055 if (copy_from_user(indir, useraddr, size * sizeof(indir[0]))) 1056 return -EFAULT; 1057 1058 /* Validate ring indices */ 1059 for (i = 0; i < size; i++) 1060 if (indir[i] >= rx_rings->data) 1061 return -EINVAL; 1062 1063 return 0; 1064} 1065 1066u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly; 1067 1068void netdev_rss_key_fill(void *buffer, size_t len) 1069{ 1070 BUG_ON(len > sizeof(netdev_rss_key)); 1071 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key)); 1072 memcpy(buffer, netdev_rss_key, len); 1073} 1074EXPORT_SYMBOL(netdev_rss_key_fill); 1075 1076static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max) 1077{ 1078 u32 dev_size, current_max = 0; 1079 u32 *indir; 1080 int ret; 1081 1082 if (!dev->ethtool_ops->get_rxfh_indir_size || 1083 !dev->ethtool_ops->get_rxfh) 1084 return -EOPNOTSUPP; 1085 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); 1086 if (dev_size == 0) 1087 return -EOPNOTSUPP; 1088 1089 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); 1090 if (!indir) 1091 return -ENOMEM; 1092 1093 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); 1094 if (ret) 1095 goto out; 1096 1097 while (dev_size--) 1098 current_max = max(current_max, indir[dev_size]); 1099 1100 *max = current_max; 1101 1102out: 1103 kfree(indir); 1104 return ret; 1105} 1106 1107static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, 1108 void __user *useraddr) 1109{ 1110 u32 user_size, dev_size; 1111 u32 *indir; 1112 int ret; 1113 1114 if (!dev->ethtool_ops->get_rxfh_indir_size || 1115 !dev->ethtool_ops->get_rxfh) 1116 return -EOPNOTSUPP; 1117 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); 1118 if (dev_size == 0) 1119 return -EOPNOTSUPP; 1120 1121 if (copy_from_user(&user_size, 1122 useraddr + offsetof(struct ethtool_rxfh_indir, size), 1123 sizeof(user_size))) 1124 return -EFAULT; 1125 1126 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), 1127 &dev_size, sizeof(dev_size))) 1128 return -EFAULT; 1129 1130 /* If the user buffer size is 0, this is just a query for the 1131 * device table size. Otherwise, if it's smaller than the 1132 * device table size it's an error. 1133 */ 1134 if (user_size < dev_size) 1135 return user_size == 0 ? 0 : -EINVAL; 1136 1137 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); 1138 if (!indir) 1139 return -ENOMEM; 1140 1141 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); 1142 if (ret) 1143 goto out; 1144 1145 if (copy_to_user(useraddr + 1146 offsetof(struct ethtool_rxfh_indir, ring_index[0]), 1147 indir, dev_size * sizeof(indir[0]))) 1148 ret = -EFAULT; 1149 1150out: 1151 kfree(indir); 1152 return ret; 1153} 1154 1155static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, 1156 void __user *useraddr) 1157{ 1158 struct ethtool_rxnfc rx_rings; 1159 u32 user_size, dev_size, i; 1160 u32 *indir; 1161 const struct ethtool_ops *ops = dev->ethtool_ops; 1162 int ret; 1163 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); 1164 1165 if (!ops->get_rxfh_indir_size || !ops->set_rxfh || 1166 !ops->get_rxnfc) 1167 return -EOPNOTSUPP; 1168 1169 dev_size = ops->get_rxfh_indir_size(dev); 1170 if (dev_size == 0) 1171 return -EOPNOTSUPP; 1172 1173 if (copy_from_user(&user_size, 1174 useraddr + offsetof(struct ethtool_rxfh_indir, size), 1175 sizeof(user_size))) 1176 return -EFAULT; 1177 1178 if (user_size != 0 && user_size != dev_size) 1179 return -EINVAL; 1180 1181 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); 1182 if (!indir) 1183 return -ENOMEM; 1184 1185 rx_rings.cmd = ETHTOOL_GRXRINGS; 1186 ret = ops->get_rxnfc(dev, &rx_rings, NULL); 1187 if (ret) 1188 goto out; 1189 1190 if (user_size == 0) { 1191 for (i = 0; i < dev_size; i++) 1192 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); 1193 } else { 1194 ret = ethtool_copy_validate_indir(indir, 1195 useraddr + ringidx_offset, 1196 &rx_rings, 1197 dev_size); 1198 if (ret) 1199 goto out; 1200 } 1201 1202 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE); 1203 if (ret) 1204 goto out; 1205 1206 /* indicate whether rxfh was set to default */ 1207 if (user_size == 0) 1208 dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 1209 else 1210 dev->priv_flags |= IFF_RXFH_CONFIGURED; 1211 1212out: 1213 kfree(indir); 1214 return ret; 1215} 1216 1217static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, 1218 void __user *useraddr) 1219{ 1220 int ret; 1221 const struct ethtool_ops *ops = dev->ethtool_ops; 1222 u32 user_indir_size, user_key_size; 1223 u32 dev_indir_size = 0, dev_key_size = 0; 1224 struct ethtool_rxfh rxfh; 1225 u32 total_size; 1226 u32 indir_bytes; 1227 u32 *indir = NULL; 1228 u8 dev_hfunc = 0; 1229 u8 *hkey = NULL; 1230 u8 *rss_config; 1231 1232 if (!ops->get_rxfh) 1233 return -EOPNOTSUPP; 1234 1235 if (ops->get_rxfh_indir_size) 1236 dev_indir_size = ops->get_rxfh_indir_size(dev); 1237 if (ops->get_rxfh_key_size) 1238 dev_key_size = ops->get_rxfh_key_size(dev); 1239 1240 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) 1241 return -EFAULT; 1242 user_indir_size = rxfh.indir_size; 1243 user_key_size = rxfh.key_size; 1244 1245 /* Check that reserved fields are 0 for now */ 1246 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) 1247 return -EINVAL; 1248 /* Most drivers don't handle rss_context, check it's 0 as well */ 1249 if (rxfh.rss_context && !ops->get_rxfh_context) 1250 return -EOPNOTSUPP; 1251 1252 rxfh.indir_size = dev_indir_size; 1253 rxfh.key_size = dev_key_size; 1254 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh))) 1255 return -EFAULT; 1256 1257 if ((user_indir_size && (user_indir_size != dev_indir_size)) || 1258 (user_key_size && (user_key_size != dev_key_size))) 1259 return -EINVAL; 1260 1261 indir_bytes = user_indir_size * sizeof(indir[0]); 1262 total_size = indir_bytes + user_key_size; 1263 rss_config = kzalloc(total_size, GFP_USER); 1264 if (!rss_config) 1265 return -ENOMEM; 1266 1267 if (user_indir_size) 1268 indir = (u32 *)rss_config; 1269 1270 if (user_key_size) 1271 hkey = rss_config + indir_bytes; 1272 1273 if (rxfh.rss_context) 1274 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey, 1275 &dev_hfunc, 1276 rxfh.rss_context); 1277 else 1278 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc); 1279 if (ret) 1280 goto out; 1281 1282 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc), 1283 &dev_hfunc, sizeof(rxfh.hfunc))) { 1284 ret = -EFAULT; 1285 } else if (copy_to_user(useraddr + 1286 offsetof(struct ethtool_rxfh, rss_config[0]), 1287 rss_config, total_size)) { 1288 ret = -EFAULT; 1289 } 1290out: 1291 kfree(rss_config); 1292 1293 return ret; 1294} 1295 1296static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, 1297 void __user *useraddr) 1298{ 1299 int ret; 1300 const struct ethtool_ops *ops = dev->ethtool_ops; 1301 struct ethtool_rxnfc rx_rings; 1302 struct ethtool_rxfh rxfh; 1303 u32 dev_indir_size = 0, dev_key_size = 0, i; 1304 u32 *indir = NULL, indir_bytes = 0; 1305 u8 *hkey = NULL; 1306 u8 *rss_config; 1307 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); 1308 bool delete = false; 1309 1310 if (!ops->get_rxnfc || !ops->set_rxfh) 1311 return -EOPNOTSUPP; 1312 1313 if (ops->get_rxfh_indir_size) 1314 dev_indir_size = ops->get_rxfh_indir_size(dev); 1315 if (ops->get_rxfh_key_size) 1316 dev_key_size = ops->get_rxfh_key_size(dev); 1317 1318 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) 1319 return -EFAULT; 1320 1321 /* Check that reserved fields are 0 for now */ 1322 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32) 1323 return -EINVAL; 1324 /* Most drivers don't handle rss_context, check it's 0 as well */ 1325 if (rxfh.rss_context && !ops->set_rxfh_context) 1326 return -EOPNOTSUPP; 1327 1328 /* If either indir, hash key or function is valid, proceed further. 1329 * Must request at least one change: indir size, hash key or function. 1330 */ 1331 if ((rxfh.indir_size && 1332 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && 1333 rxfh.indir_size != dev_indir_size) || 1334 (rxfh.key_size && (rxfh.key_size != dev_key_size)) || 1335 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && 1336 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE)) 1337 return -EINVAL; 1338 1339 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) 1340 indir_bytes = dev_indir_size * sizeof(indir[0]); 1341 1342 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER); 1343 if (!rss_config) 1344 return -ENOMEM; 1345 1346 rx_rings.cmd = ETHTOOL_GRXRINGS; 1347 ret = ops->get_rxnfc(dev, &rx_rings, NULL); 1348 if (ret) 1349 goto out; 1350 1351 /* rxfh.indir_size == 0 means reset the indir table to default (master 1352 * context) or delete the context (other RSS contexts). 1353 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. 1354 */ 1355 if (rxfh.indir_size && 1356 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { 1357 indir = (u32 *)rss_config; 1358 ret = ethtool_copy_validate_indir(indir, 1359 useraddr + rss_cfg_offset, 1360 &rx_rings, 1361 rxfh.indir_size); 1362 if (ret) 1363 goto out; 1364 } else if (rxfh.indir_size == 0) { 1365 if (rxfh.rss_context == 0) { 1366 indir = (u32 *)rss_config; 1367 for (i = 0; i < dev_indir_size; i++) 1368 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); 1369 } else { 1370 delete = true; 1371 } 1372 } 1373 1374 if (rxfh.key_size) { 1375 hkey = rss_config + indir_bytes; 1376 if (copy_from_user(hkey, 1377 useraddr + rss_cfg_offset + indir_bytes, 1378 rxfh.key_size)) { 1379 ret = -EFAULT; 1380 goto out; 1381 } 1382 } 1383 1384 if (rxfh.rss_context) 1385 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc, 1386 &rxfh.rss_context, delete); 1387 else 1388 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc); 1389 if (ret) 1390 goto out; 1391 1392 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context), 1393 &rxfh.rss_context, sizeof(rxfh.rss_context))) 1394 ret = -EFAULT; 1395 1396 if (!rxfh.rss_context) { 1397 /* indicate whether rxfh was set to default */ 1398 if (rxfh.indir_size == 0) 1399 dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 1400 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) 1401 dev->priv_flags |= IFF_RXFH_CONFIGURED; 1402 } 1403 1404out: 1405 kfree(rss_config); 1406 return ret; 1407} 1408 1409static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) 1410{ 1411 struct ethtool_regs regs; 1412 const struct ethtool_ops *ops = dev->ethtool_ops; 1413 void *regbuf; 1414 int reglen, ret; 1415 1416 if (!ops->get_regs || !ops->get_regs_len) 1417 return -EOPNOTSUPP; 1418 1419 if (copy_from_user(&regs, useraddr, sizeof(regs))) 1420 return -EFAULT; 1421 1422 reglen = ops->get_regs_len(dev); 1423 if (regs.len > reglen) 1424 regs.len = reglen; 1425 1426 regbuf = NULL; 1427 if (reglen) { 1428 regbuf = vzalloc(reglen); 1429 if (!regbuf) 1430 return -ENOMEM; 1431 } 1432 1433 ops->get_regs(dev, &regs, regbuf); 1434 1435 ret = -EFAULT; 1436 if (copy_to_user(useraddr, &regs, sizeof(regs))) 1437 goto out; 1438 useraddr += offsetof(struct ethtool_regs, data); 1439 if (regbuf && copy_to_user(useraddr, regbuf, regs.len)) 1440 goto out; 1441 ret = 0; 1442 1443 out: 1444 vfree(regbuf); 1445 return ret; 1446} 1447 1448static int ethtool_reset(struct net_device *dev, char __user *useraddr) 1449{ 1450 struct ethtool_value reset; 1451 int ret; 1452 1453 if (!dev->ethtool_ops->reset) 1454 return -EOPNOTSUPP; 1455 1456 if (copy_from_user(&reset, useraddr, sizeof(reset))) 1457 return -EFAULT; 1458 1459 ret = dev->ethtool_ops->reset(dev, &reset.data); 1460 if (ret) 1461 return ret; 1462 1463 if (copy_to_user(useraddr, &reset, sizeof(reset))) 1464 return -EFAULT; 1465 return 0; 1466} 1467 1468static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) 1469{ 1470 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 1471 1472 if (!dev->ethtool_ops->get_wol) 1473 return -EOPNOTSUPP; 1474 1475 dev->ethtool_ops->get_wol(dev, &wol); 1476 1477 if (copy_to_user(useraddr, &wol, sizeof(wol))) 1478 return -EFAULT; 1479 return 0; 1480} 1481 1482static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) 1483{ 1484 struct ethtool_wolinfo wol; 1485 1486 if (!dev->ethtool_ops->set_wol) 1487 return -EOPNOTSUPP; 1488 1489 if (copy_from_user(&wol, useraddr, sizeof(wol))) 1490 return -EFAULT; 1491 1492 return dev->ethtool_ops->set_wol(dev, &wol); 1493} 1494 1495static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) 1496{ 1497 struct ethtool_eee edata; 1498 int rc; 1499 1500 if (!dev->ethtool_ops->get_eee) 1501 return -EOPNOTSUPP; 1502 1503 memset(&edata, 0, sizeof(struct ethtool_eee)); 1504 edata.cmd = ETHTOOL_GEEE; 1505 rc = dev->ethtool_ops->get_eee(dev, &edata); 1506 1507 if (rc) 1508 return rc; 1509 1510 if (copy_to_user(useraddr, &edata, sizeof(edata))) 1511 return -EFAULT; 1512 1513 return 0; 1514} 1515 1516static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) 1517{ 1518 struct ethtool_eee edata; 1519 1520 if (!dev->ethtool_ops->set_eee) 1521 return -EOPNOTSUPP; 1522 1523 if (copy_from_user(&edata, useraddr, sizeof(edata))) 1524 return -EFAULT; 1525 1526 return dev->ethtool_ops->set_eee(dev, &edata); 1527} 1528 1529static int ethtool_nway_reset(struct net_device *dev) 1530{ 1531 if (!dev->ethtool_ops->nway_reset) 1532 return -EOPNOTSUPP; 1533 1534 return dev->ethtool_ops->nway_reset(dev); 1535} 1536 1537static int ethtool_get_link(struct net_device *dev, char __user *useraddr) 1538{ 1539 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; 1540 1541 if (!dev->ethtool_ops->get_link) 1542 return -EOPNOTSUPP; 1543 1544 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev); 1545 1546 if (copy_to_user(useraddr, &edata, sizeof(edata))) 1547 return -EFAULT; 1548 return 0; 1549} 1550 1551static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, 1552 int (*getter)(struct net_device *, 1553 struct ethtool_eeprom *, u8 *), 1554 u32 total_len) 1555{ 1556 struct ethtool_eeprom eeprom; 1557 void __user *userbuf = useraddr + sizeof(eeprom); 1558 u32 bytes_remaining; 1559 u8 *data; 1560 int ret = 0; 1561 1562 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1563 return -EFAULT; 1564 1565 /* Check for wrap and zero */ 1566 if (eeprom.offset + eeprom.len <= eeprom.offset) 1567 return -EINVAL; 1568 1569 /* Check for exceeding total eeprom len */ 1570 if (eeprom.offset + eeprom.len > total_len) 1571 return -EINVAL; 1572 1573 data = kmalloc(PAGE_SIZE, GFP_USER); 1574 if (!data) 1575 return -ENOMEM; 1576 1577 bytes_remaining = eeprom.len; 1578 while (bytes_remaining > 0) { 1579 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1580 1581 ret = getter(dev, &eeprom, data); 1582 if (ret) 1583 break; 1584 if (copy_to_user(userbuf, data, eeprom.len)) { 1585 ret = -EFAULT; 1586 break; 1587 } 1588 userbuf += eeprom.len; 1589 eeprom.offset += eeprom.len; 1590 bytes_remaining -= eeprom.len; 1591 } 1592 1593 eeprom.len = userbuf - (useraddr + sizeof(eeprom)); 1594 eeprom.offset -= eeprom.len; 1595 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) 1596 ret = -EFAULT; 1597 1598 kfree(data); 1599 return ret; 1600} 1601 1602static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) 1603{ 1604 const struct ethtool_ops *ops = dev->ethtool_ops; 1605 1606 if (!ops->get_eeprom || !ops->get_eeprom_len || 1607 !ops->get_eeprom_len(dev)) 1608 return -EOPNOTSUPP; 1609 1610 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, 1611 ops->get_eeprom_len(dev)); 1612} 1613 1614static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) 1615{ 1616 struct ethtool_eeprom eeprom; 1617 const struct ethtool_ops *ops = dev->ethtool_ops; 1618 void __user *userbuf = useraddr + sizeof(eeprom); 1619 u32 bytes_remaining; 1620 u8 *data; 1621 int ret = 0; 1622 1623 if (!ops->set_eeprom || !ops->get_eeprom_len || 1624 !ops->get_eeprom_len(dev)) 1625 return -EOPNOTSUPP; 1626 1627 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1628 return -EFAULT; 1629 1630 /* Check for wrap and zero */ 1631 if (eeprom.offset + eeprom.len <= eeprom.offset) 1632 return -EINVAL; 1633 1634 /* Check for exceeding total eeprom len */ 1635 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) 1636 return -EINVAL; 1637 1638 data = kmalloc(PAGE_SIZE, GFP_USER); 1639 if (!data) 1640 return -ENOMEM; 1641 1642 bytes_remaining = eeprom.len; 1643 while (bytes_remaining > 0) { 1644 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1645 1646 if (copy_from_user(data, userbuf, eeprom.len)) { 1647 ret = -EFAULT; 1648 break; 1649 } 1650 ret = ops->set_eeprom(dev, &eeprom, data); 1651 if (ret) 1652 break; 1653 userbuf += eeprom.len; 1654 eeprom.offset += eeprom.len; 1655 bytes_remaining -= eeprom.len; 1656 } 1657 1658 kfree(data); 1659 return ret; 1660} 1661 1662static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, 1663 void __user *useraddr) 1664{ 1665 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 1666 1667 if (!dev->ethtool_ops->get_coalesce) 1668 return -EOPNOTSUPP; 1669 1670 dev->ethtool_ops->get_coalesce(dev, &coalesce); 1671 1672 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 1673 return -EFAULT; 1674 return 0; 1675} 1676 1677static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, 1678 void __user *useraddr) 1679{ 1680 struct ethtool_coalesce coalesce; 1681 1682 if (!dev->ethtool_ops->set_coalesce) 1683 return -EOPNOTSUPP; 1684 1685 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) 1686 return -EFAULT; 1687 1688 return dev->ethtool_ops->set_coalesce(dev, &coalesce); 1689} 1690 1691static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) 1692{ 1693 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; 1694 1695 if (!dev->ethtool_ops->get_ringparam) 1696 return -EOPNOTSUPP; 1697 1698 dev->ethtool_ops->get_ringparam(dev, &ringparam); 1699 1700 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) 1701 return -EFAULT; 1702 return 0; 1703} 1704 1705static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) 1706{ 1707 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM }; 1708 1709 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) 1710 return -EOPNOTSUPP; 1711 1712 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) 1713 return -EFAULT; 1714 1715 dev->ethtool_ops->get_ringparam(dev, &max); 1716 1717 /* ensure new ring parameters are within the maximums */ 1718 if (ringparam.rx_pending > max.rx_max_pending || 1719 ringparam.rx_mini_pending > max.rx_mini_max_pending || 1720 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || 1721 ringparam.tx_pending > max.tx_max_pending) 1722 return -EINVAL; 1723 1724 return dev->ethtool_ops->set_ringparam(dev, &ringparam); 1725} 1726 1727static noinline_for_stack int ethtool_get_channels(struct net_device *dev, 1728 void __user *useraddr) 1729{ 1730 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; 1731 1732 if (!dev->ethtool_ops->get_channels) 1733 return -EOPNOTSUPP; 1734 1735 dev->ethtool_ops->get_channels(dev, &channels); 1736 1737 if (copy_to_user(useraddr, &channels, sizeof(channels))) 1738 return -EFAULT; 1739 return 0; 1740} 1741 1742static noinline_for_stack int ethtool_set_channels(struct net_device *dev, 1743 void __user *useraddr) 1744{ 1745 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS }; 1746 u32 max_rx_in_use = 0; 1747 1748 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) 1749 return -EOPNOTSUPP; 1750 1751 if (copy_from_user(&channels, useraddr, sizeof(channels))) 1752 return -EFAULT; 1753 1754 dev->ethtool_ops->get_channels(dev, &max); 1755 1756 /* ensure new counts are within the maximums */ 1757 if ((channels.rx_count > max.max_rx) || 1758 (channels.tx_count > max.max_tx) || 1759 (channels.combined_count > max.max_combined) || 1760 (channels.other_count > max.max_other)) 1761 return -EINVAL; 1762 1763 /* ensure the new Rx count fits within the configured Rx flow 1764 * indirection table settings */ 1765 if (netif_is_rxfh_configured(dev) && 1766 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && 1767 (channels.combined_count + channels.rx_count) <= max_rx_in_use) 1768 return -EINVAL; 1769 1770 return dev->ethtool_ops->set_channels(dev, &channels); 1771} 1772 1773static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) 1774{ 1775 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; 1776 1777 if (!dev->ethtool_ops->get_pauseparam) 1778 return -EOPNOTSUPP; 1779 1780 dev->ethtool_ops->get_pauseparam(dev, &pauseparam); 1781 1782 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) 1783 return -EFAULT; 1784 return 0; 1785} 1786 1787static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) 1788{ 1789 struct ethtool_pauseparam pauseparam; 1790 1791 if (!dev->ethtool_ops->set_pauseparam) 1792 return -EOPNOTSUPP; 1793 1794 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) 1795 return -EFAULT; 1796 1797 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); 1798} 1799 1800static int ethtool_self_test(struct net_device *dev, char __user *useraddr) 1801{ 1802 struct ethtool_test test; 1803 const struct ethtool_ops *ops = dev->ethtool_ops; 1804 u64 *data; 1805 int ret, test_len; 1806 1807 if (!ops->self_test || !ops->get_sset_count) 1808 return -EOPNOTSUPP; 1809 1810 test_len = ops->get_sset_count(dev, ETH_SS_TEST); 1811 if (test_len < 0) 1812 return test_len; 1813 WARN_ON(test_len == 0); 1814 1815 if (copy_from_user(&test, useraddr, sizeof(test))) 1816 return -EFAULT; 1817 1818 test.len = test_len; 1819 data = kmalloc_array(test_len, sizeof(u64), GFP_USER); 1820 if (!data) 1821 return -ENOMEM; 1822 1823 ops->self_test(dev, &test, data); 1824 1825 ret = -EFAULT; 1826 if (copy_to_user(useraddr, &test, sizeof(test))) 1827 goto out; 1828 useraddr += sizeof(test); 1829 if (copy_to_user(useraddr, data, test.len * sizeof(u64))) 1830 goto out; 1831 ret = 0; 1832 1833 out: 1834 kfree(data); 1835 return ret; 1836} 1837 1838static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) 1839{ 1840 struct ethtool_gstrings gstrings; 1841 u8 *data; 1842 int ret; 1843 1844 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) 1845 return -EFAULT; 1846 1847 ret = __ethtool_get_sset_count(dev, gstrings.string_set); 1848 if (ret < 0) 1849 return ret; 1850 if (ret > S32_MAX / ETH_GSTRING_LEN) 1851 return -ENOMEM; 1852 WARN_ON_ONCE(!ret); 1853 1854 gstrings.len = ret; 1855 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); 1856 if (gstrings.len && !data) 1857 return -ENOMEM; 1858 1859 __ethtool_get_strings(dev, gstrings.string_set, data); 1860 1861 ret = -EFAULT; 1862 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) 1863 goto out; 1864 useraddr += sizeof(gstrings); 1865 if (gstrings.len && 1866 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) 1867 goto out; 1868 ret = 0; 1869 1870out: 1871 vfree(data); 1872 return ret; 1873} 1874 1875static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) 1876{ 1877 struct ethtool_value id; 1878 static bool busy; 1879 const struct ethtool_ops *ops = dev->ethtool_ops; 1880 int rc; 1881 1882 if (!ops->set_phys_id) 1883 return -EOPNOTSUPP; 1884 1885 if (busy) 1886 return -EBUSY; 1887 1888 if (copy_from_user(&id, useraddr, sizeof(id))) 1889 return -EFAULT; 1890 1891 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); 1892 if (rc < 0) 1893 return rc; 1894 1895 /* Drop the RTNL lock while waiting, but prevent reentry or 1896 * removal of the device. 1897 */ 1898 busy = true; 1899 dev_hold(dev); 1900 rtnl_unlock(); 1901 1902 if (rc == 0) { 1903 /* Driver will handle this itself */ 1904 schedule_timeout_interruptible( 1905 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 1906 } else { 1907 /* Driver expects to be called at twice the frequency in rc */ 1908 int n = rc * 2, i, interval = HZ / n; 1909 1910 /* Count down seconds */ 1911 do { 1912 /* Count down iterations per second */ 1913 i = n; 1914 do { 1915 rtnl_lock(); 1916 rc = ops->set_phys_id(dev, 1917 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 1918 rtnl_unlock(); 1919 if (rc) 1920 break; 1921 schedule_timeout_interruptible(interval); 1922 } while (!signal_pending(current) && --i != 0); 1923 } while (!signal_pending(current) && 1924 (id.data == 0 || --id.data != 0)); 1925 } 1926 1927 rtnl_lock(); 1928 dev_put(dev); 1929 busy = false; 1930 1931 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 1932 return rc; 1933} 1934 1935static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 1936{ 1937 struct ethtool_stats stats; 1938 const struct ethtool_ops *ops = dev->ethtool_ops; 1939 u64 *data; 1940 int ret, n_stats; 1941 1942 if (!ops->get_ethtool_stats || !ops->get_sset_count) 1943 return -EOPNOTSUPP; 1944 1945 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 1946 if (n_stats < 0) 1947 return n_stats; 1948 if (n_stats > S32_MAX / sizeof(u64)) 1949 return -ENOMEM; 1950 WARN_ON_ONCE(!n_stats); 1951 if (copy_from_user(&stats, useraddr, sizeof(stats))) 1952 return -EFAULT; 1953 1954 stats.n_stats = n_stats; 1955 data = vzalloc(array_size(n_stats, sizeof(u64))); 1956 if (n_stats && !data) 1957 return -ENOMEM; 1958 1959 ops->get_ethtool_stats(dev, &stats, data); 1960 1961 ret = -EFAULT; 1962 if (copy_to_user(useraddr, &stats, sizeof(stats))) 1963 goto out; 1964 useraddr += sizeof(stats); 1965 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) 1966 goto out; 1967 ret = 0; 1968 1969 out: 1970 vfree(data); 1971 return ret; 1972} 1973 1974static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 1975{ 1976 const struct ethtool_ops *ops = dev->ethtool_ops; 1977 struct phy_device *phydev = dev->phydev; 1978 struct ethtool_stats stats; 1979 u64 *data; 1980 int ret, n_stats; 1981 1982 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count)) 1983 return -EOPNOTSUPP; 1984 1985 if (dev->phydev && !ops->get_ethtool_phy_stats) 1986 n_stats = phy_ethtool_get_sset_count(dev->phydev); 1987 else 1988 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 1989 if (n_stats < 0) 1990 return n_stats; 1991 if (n_stats > S32_MAX / sizeof(u64)) 1992 return -ENOMEM; 1993 WARN_ON_ONCE(!n_stats); 1994 1995 if (copy_from_user(&stats, useraddr, sizeof(stats))) 1996 return -EFAULT; 1997 1998 stats.n_stats = n_stats; 1999 data = vzalloc(array_size(n_stats, sizeof(u64))); 2000 if (n_stats && !data) 2001 return -ENOMEM; 2002 2003 if (dev->phydev && !ops->get_ethtool_phy_stats) { 2004 ret = phy_ethtool_get_stats(dev->phydev, &stats, data); 2005 if (ret < 0) 2006 return ret; 2007 } else { 2008 ops->get_ethtool_phy_stats(dev, &stats, data); 2009 } 2010 2011 ret = -EFAULT; 2012 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2013 goto out; 2014 useraddr += sizeof(stats); 2015 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) 2016 goto out; 2017 ret = 0; 2018 2019 out: 2020 vfree(data); 2021 return ret; 2022} 2023 2024static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2025{ 2026 struct ethtool_perm_addr epaddr; 2027 2028 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2029 return -EFAULT; 2030 2031 if (epaddr.size < dev->addr_len) 2032 return -ETOOSMALL; 2033 epaddr.size = dev->addr_len; 2034 2035 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2036 return -EFAULT; 2037 useraddr += sizeof(epaddr); 2038 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2039 return -EFAULT; 2040 return 0; 2041} 2042 2043static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2044 u32 cmd, u32 (*actor)(struct net_device *)) 2045{ 2046 struct ethtool_value edata = { .cmd = cmd }; 2047 2048 if (!actor) 2049 return -EOPNOTSUPP; 2050 2051 edata.data = actor(dev); 2052 2053 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2054 return -EFAULT; 2055 return 0; 2056} 2057 2058static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2059 void (*actor)(struct net_device *, u32)) 2060{ 2061 struct ethtool_value edata; 2062 2063 if (!actor) 2064 return -EOPNOTSUPP; 2065 2066 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2067 return -EFAULT; 2068 2069 actor(dev, edata.data); 2070 return 0; 2071} 2072 2073static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2074 int (*actor)(struct net_device *, u32)) 2075{ 2076 struct ethtool_value edata; 2077 2078 if (!actor) 2079 return -EOPNOTSUPP; 2080 2081 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2082 return -EFAULT; 2083 2084 return actor(dev, edata.data); 2085} 2086 2087static noinline_for_stack int ethtool_flash_device(struct net_device *dev, 2088 char __user *useraddr) 2089{ 2090 struct ethtool_flash efl; 2091 2092 if (copy_from_user(&efl, useraddr, sizeof(efl))) 2093 return -EFAULT; 2094 2095 if (!dev->ethtool_ops->flash_device) 2096 return -EOPNOTSUPP; 2097 2098 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 2099 2100 return dev->ethtool_ops->flash_device(dev, &efl); 2101} 2102 2103static int ethtool_set_dump(struct net_device *dev, 2104 void __user *useraddr) 2105{ 2106 struct ethtool_dump dump; 2107 2108 if (!dev->ethtool_ops->set_dump) 2109 return -EOPNOTSUPP; 2110 2111 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2112 return -EFAULT; 2113 2114 return dev->ethtool_ops->set_dump(dev, &dump); 2115} 2116 2117static int ethtool_get_dump_flag(struct net_device *dev, 2118 void __user *useraddr) 2119{ 2120 int ret; 2121 struct ethtool_dump dump; 2122 const struct ethtool_ops *ops = dev->ethtool_ops; 2123 2124 if (!ops->get_dump_flag) 2125 return -EOPNOTSUPP; 2126 2127 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2128 return -EFAULT; 2129 2130 ret = ops->get_dump_flag(dev, &dump); 2131 if (ret) 2132 return ret; 2133 2134 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2135 return -EFAULT; 2136 return 0; 2137} 2138 2139static int ethtool_get_dump_data(struct net_device *dev, 2140 void __user *useraddr) 2141{ 2142 int ret; 2143 __u32 len; 2144 struct ethtool_dump dump, tmp; 2145 const struct ethtool_ops *ops = dev->ethtool_ops; 2146 void *data = NULL; 2147 2148 if (!ops->get_dump_data || !ops->get_dump_flag) 2149 return -EOPNOTSUPP; 2150 2151 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2152 return -EFAULT; 2153 2154 memset(&tmp, 0, sizeof(tmp)); 2155 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2156 ret = ops->get_dump_flag(dev, &tmp); 2157 if (ret) 2158 return ret; 2159 2160 len = min(tmp.len, dump.len); 2161 if (!len) 2162 return -EFAULT; 2163 2164 /* Don't ever let the driver think there's more space available 2165 * than it requested with .get_dump_flag(). 2166 */ 2167 dump.len = len; 2168 2169 /* Always allocate enough space to hold the whole thing so that the 2170 * driver does not need to check the length and bother with partial 2171 * dumping. 2172 */ 2173 data = vzalloc(tmp.len); 2174 if (!data) 2175 return -ENOMEM; 2176 ret = ops->get_dump_data(dev, &dump, data); 2177 if (ret) 2178 goto out; 2179 2180 /* There are two sane possibilities: 2181 * 1. The driver's .get_dump_data() does not touch dump.len. 2182 * 2. Or it may set dump.len to how much it really writes, which 2183 * should be tmp.len (or len if it can do a partial dump). 2184 * In any case respond to userspace with the actual length of data 2185 * it's receiving. 2186 */ 2187 WARN_ON(dump.len != len && dump.len != tmp.len); 2188 dump.len = len; 2189 2190 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2191 ret = -EFAULT; 2192 goto out; 2193 } 2194 useraddr += offsetof(struct ethtool_dump, data); 2195 if (copy_to_user(useraddr, data, len)) 2196 ret = -EFAULT; 2197out: 2198 vfree(data); 2199 return ret; 2200} 2201 2202static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2203{ 2204 int err = 0; 2205 struct ethtool_ts_info info; 2206 const struct ethtool_ops *ops = dev->ethtool_ops; 2207 struct phy_device *phydev = dev->phydev; 2208 2209 memset(&info, 0, sizeof(info)); 2210 info.cmd = ETHTOOL_GET_TS_INFO; 2211 2212 if (phydev && phydev->drv && phydev->drv->ts_info) { 2213 err = phydev->drv->ts_info(phydev, &info); 2214 } else if (ops->get_ts_info) { 2215 err = ops->get_ts_info(dev, &info); 2216 } else { 2217 info.so_timestamping = 2218 SOF_TIMESTAMPING_RX_SOFTWARE | 2219 SOF_TIMESTAMPING_SOFTWARE; 2220 info.phc_index = -1; 2221 } 2222 2223 if (err) 2224 return err; 2225 2226 if (copy_to_user(useraddr, &info, sizeof(info))) 2227 err = -EFAULT; 2228 2229 return err; 2230} 2231 2232static int __ethtool_get_module_info(struct net_device *dev, 2233 struct ethtool_modinfo *modinfo) 2234{ 2235 const struct ethtool_ops *ops = dev->ethtool_ops; 2236 struct phy_device *phydev = dev->phydev; 2237 2238 if (dev->sfp_bus) 2239 return sfp_get_module_info(dev->sfp_bus, modinfo); 2240 2241 if (phydev && phydev->drv && phydev->drv->module_info) 2242 return phydev->drv->module_info(phydev, modinfo); 2243 2244 if (ops->get_module_info) 2245 return ops->get_module_info(dev, modinfo); 2246 2247 return -EOPNOTSUPP; 2248} 2249 2250static int ethtool_get_module_info(struct net_device *dev, 2251 void __user *useraddr) 2252{ 2253 int ret; 2254 struct ethtool_modinfo modinfo; 2255 2256 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2257 return -EFAULT; 2258 2259 ret = __ethtool_get_module_info(dev, &modinfo); 2260 if (ret) 2261 return ret; 2262 2263 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2264 return -EFAULT; 2265 2266 return 0; 2267} 2268 2269static int __ethtool_get_module_eeprom(struct net_device *dev, 2270 struct ethtool_eeprom *ee, u8 *data) 2271{ 2272 const struct ethtool_ops *ops = dev->ethtool_ops; 2273 struct phy_device *phydev = dev->phydev; 2274 2275 if (dev->sfp_bus) 2276 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2277 2278 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2279 return phydev->drv->module_eeprom(phydev, ee, data); 2280 2281 if (ops->get_module_eeprom) 2282 return ops->get_module_eeprom(dev, ee, data); 2283 2284 return -EOPNOTSUPP; 2285} 2286 2287static int ethtool_get_module_eeprom(struct net_device *dev, 2288 void __user *useraddr) 2289{ 2290 int ret; 2291 struct ethtool_modinfo modinfo; 2292 2293 ret = __ethtool_get_module_info(dev, &modinfo); 2294 if (ret) 2295 return ret; 2296 2297 return ethtool_get_any_eeprom(dev, useraddr, 2298 __ethtool_get_module_eeprom, 2299 modinfo.eeprom_len); 2300} 2301 2302static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2303{ 2304 switch (tuna->id) { 2305 case ETHTOOL_RX_COPYBREAK: 2306 case ETHTOOL_TX_COPYBREAK: 2307 if (tuna->len != sizeof(u32) || 2308 tuna->type_id != ETHTOOL_TUNABLE_U32) 2309 return -EINVAL; 2310 break; 2311 case ETHTOOL_PFC_PREVENTION_TOUT: 2312 if (tuna->len != sizeof(u16) || 2313 tuna->type_id != ETHTOOL_TUNABLE_U16) 2314 return -EINVAL; 2315 break; 2316 default: 2317 return -EINVAL; 2318 } 2319 2320 return 0; 2321} 2322 2323static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2324{ 2325 int ret; 2326 struct ethtool_tunable tuna; 2327 const struct ethtool_ops *ops = dev->ethtool_ops; 2328 void *data; 2329 2330 if (!ops->get_tunable) 2331 return -EOPNOTSUPP; 2332 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2333 return -EFAULT; 2334 ret = ethtool_tunable_valid(&tuna); 2335 if (ret) 2336 return ret; 2337 data = kmalloc(tuna.len, GFP_USER); 2338 if (!data) 2339 return -ENOMEM; 2340 ret = ops->get_tunable(dev, &tuna, data); 2341 if (ret) 2342 goto out; 2343 useraddr += sizeof(tuna); 2344 ret = -EFAULT; 2345 if (copy_to_user(useraddr, data, tuna.len)) 2346 goto out; 2347 ret = 0; 2348 2349out: 2350 kfree(data); 2351 return ret; 2352} 2353 2354static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2355{ 2356 int ret; 2357 struct ethtool_tunable tuna; 2358 const struct ethtool_ops *ops = dev->ethtool_ops; 2359 void *data; 2360 2361 if (!ops->set_tunable) 2362 return -EOPNOTSUPP; 2363 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2364 return -EFAULT; 2365 ret = ethtool_tunable_valid(&tuna); 2366 if (ret) 2367 return ret; 2368 useraddr += sizeof(tuna); 2369 data = memdup_user(useraddr, tuna.len); 2370 if (IS_ERR(data)) 2371 return PTR_ERR(data); 2372 ret = ops->set_tunable(dev, &tuna, data); 2373 2374 kfree(data); 2375 return ret; 2376} 2377 2378static int ethtool_get_per_queue_coalesce(struct net_device *dev, 2379 void __user *useraddr, 2380 struct ethtool_per_queue_op *per_queue_opt) 2381{ 2382 u32 bit; 2383 int ret; 2384 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2385 2386 if (!dev->ethtool_ops->get_per_queue_coalesce) 2387 return -EOPNOTSUPP; 2388 2389 useraddr += sizeof(*per_queue_opt); 2390 2391 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 2392 MAX_NUM_QUEUE); 2393 2394 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2395 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2396 2397 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 2398 if (ret != 0) 2399 return ret; 2400 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2401 return -EFAULT; 2402 useraddr += sizeof(coalesce); 2403 } 2404 2405 return 0; 2406} 2407 2408static int ethtool_set_per_queue_coalesce(struct net_device *dev, 2409 void __user *useraddr, 2410 struct ethtool_per_queue_op *per_queue_opt) 2411{ 2412 u32 bit; 2413 int i, ret = 0; 2414 int n_queue; 2415 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 2416 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2417 2418 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 2419 (!dev->ethtool_ops->get_per_queue_coalesce)) 2420 return -EOPNOTSUPP; 2421 2422 useraddr += sizeof(*per_queue_opt); 2423 2424 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 2425 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 2426 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); 2427 if (!backup) 2428 return -ENOMEM; 2429 2430 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2431 struct ethtool_coalesce coalesce; 2432 2433 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 2434 if (ret != 0) 2435 goto roll_back; 2436 2437 tmp++; 2438 2439 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 2440 ret = -EFAULT; 2441 goto roll_back; 2442 } 2443 2444 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 2445 if (ret != 0) 2446 goto roll_back; 2447 2448 useraddr += sizeof(coalesce); 2449 } 2450 2451roll_back: 2452 if (ret != 0) { 2453 tmp = backup; 2454 for_each_set_bit(i, queue_mask, bit) { 2455 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 2456 tmp++; 2457 } 2458 } 2459 kfree(backup); 2460 2461 return ret; 2462} 2463 2464static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr) 2465{ 2466 struct ethtool_per_queue_op per_queue_opt; 2467 2468 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 2469 return -EFAULT; 2470 2471 switch (per_queue_opt.sub_command) { 2472 case ETHTOOL_GCOALESCE: 2473 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2474 case ETHTOOL_SCOALESCE: 2475 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2476 default: 2477 return -EOPNOTSUPP; 2478 }; 2479} 2480 2481static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 2482{ 2483 switch (tuna->id) { 2484 case ETHTOOL_PHY_DOWNSHIFT: 2485 if (tuna->len != sizeof(u8) || 2486 tuna->type_id != ETHTOOL_TUNABLE_U8) 2487 return -EINVAL; 2488 break; 2489 default: 2490 return -EINVAL; 2491 } 2492 2493 return 0; 2494} 2495 2496static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 2497{ 2498 int ret; 2499 struct ethtool_tunable tuna; 2500 struct phy_device *phydev = dev->phydev; 2501 void *data; 2502 2503 if (!(phydev && phydev->drv && phydev->drv->get_tunable)) 2504 return -EOPNOTSUPP; 2505 2506 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2507 return -EFAULT; 2508 ret = ethtool_phy_tunable_valid(&tuna); 2509 if (ret) 2510 return ret; 2511 data = kmalloc(tuna.len, GFP_USER); 2512 if (!data) 2513 return -ENOMEM; 2514 mutex_lock(&phydev->lock); 2515 ret = phydev->drv->get_tunable(phydev, &tuna, data); 2516 mutex_unlock(&phydev->lock); 2517 if (ret) 2518 goto out; 2519 useraddr += sizeof(tuna); 2520 ret = -EFAULT; 2521 if (copy_to_user(useraddr, data, tuna.len)) 2522 goto out; 2523 ret = 0; 2524 2525out: 2526 kfree(data); 2527 return ret; 2528} 2529 2530static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 2531{ 2532 int ret; 2533 struct ethtool_tunable tuna; 2534 struct phy_device *phydev = dev->phydev; 2535 void *data; 2536 2537 if (!(phydev && phydev->drv && phydev->drv->set_tunable)) 2538 return -EOPNOTSUPP; 2539 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2540 return -EFAULT; 2541 ret = ethtool_phy_tunable_valid(&tuna); 2542 if (ret) 2543 return ret; 2544 useraddr += sizeof(tuna); 2545 data = memdup_user(useraddr, tuna.len); 2546 if (IS_ERR(data)) 2547 return PTR_ERR(data); 2548 mutex_lock(&phydev->lock); 2549 ret = phydev->drv->set_tunable(phydev, &tuna, data); 2550 mutex_unlock(&phydev->lock); 2551 2552 kfree(data); 2553 return ret; 2554} 2555 2556static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 2557{ 2558 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM }; 2559 int rc; 2560 2561 if (!dev->ethtool_ops->get_fecparam) 2562 return -EOPNOTSUPP; 2563 2564 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 2565 if (rc) 2566 return rc; 2567 2568 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 2569 return -EFAULT; 2570 return 0; 2571} 2572 2573static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 2574{ 2575 struct ethtool_fecparam fecparam; 2576 2577 if (!dev->ethtool_ops->set_fecparam) 2578 return -EOPNOTSUPP; 2579 2580 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 2581 return -EFAULT; 2582 2583 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 2584} 2585 2586/* The main entry point in this file. Called from net/core/dev_ioctl.c */ 2587 2588int dev_ethtool(struct net *net, struct ifreq *ifr) 2589{ 2590 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); 2591 void __user *useraddr = ifr->ifr_data; 2592 u32 ethcmd, sub_cmd; 2593 int rc; 2594 netdev_features_t old_features; 2595 2596 if (!dev || !netif_device_present(dev)) 2597 return -ENODEV; 2598 2599 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd))) 2600 return -EFAULT; 2601 2602 if (ethcmd == ETHTOOL_PERQUEUE) { 2603 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 2604 return -EFAULT; 2605 } else { 2606 sub_cmd = ethcmd; 2607 } 2608 /* Allow some commands to be done by anyone */ 2609 switch (sub_cmd) { 2610 case ETHTOOL_GSET: 2611 case ETHTOOL_GDRVINFO: 2612 case ETHTOOL_GMSGLVL: 2613 case ETHTOOL_GLINK: 2614 case ETHTOOL_GCOALESCE: 2615 case ETHTOOL_GRINGPARAM: 2616 case ETHTOOL_GPAUSEPARAM: 2617 case ETHTOOL_GRXCSUM: 2618 case ETHTOOL_GTXCSUM: 2619 case ETHTOOL_GSG: 2620 case ETHTOOL_GSSET_INFO: 2621 case ETHTOOL_GSTRINGS: 2622 case ETHTOOL_GSTATS: 2623 case ETHTOOL_GPHYSTATS: 2624 case ETHTOOL_GTSO: 2625 case ETHTOOL_GPERMADDR: 2626 case ETHTOOL_GGSO: 2627 case ETHTOOL_GGRO: 2628 case ETHTOOL_GFLAGS: 2629 case ETHTOOL_GPFLAGS: 2630 case ETHTOOL_GRXFH: 2631 case ETHTOOL_GRXRINGS: 2632 case ETHTOOL_GRXCLSRLCNT: 2633 case ETHTOOL_GRXCLSRULE: 2634 case ETHTOOL_GRXCLSRLALL: 2635 case ETHTOOL_GRXFHINDIR: 2636 case ETHTOOL_GRSSH: 2637 case ETHTOOL_GFEATURES: 2638 case ETHTOOL_GCHANNELS: 2639 case ETHTOOL_GET_TS_INFO: 2640 case ETHTOOL_GEEE: 2641 case ETHTOOL_GTUNABLE: 2642 case ETHTOOL_PHY_GTUNABLE: 2643 case ETHTOOL_GLINKSETTINGS: 2644 case ETHTOOL_GFECPARAM: 2645 break; 2646 default: 2647 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2648 return -EPERM; 2649 } 2650 2651 if (dev->ethtool_ops->begin) { 2652 rc = dev->ethtool_ops->begin(dev); 2653 if (rc < 0) 2654 return rc; 2655 } 2656 old_features = dev->features; 2657 2658 switch (ethcmd) { 2659 case ETHTOOL_GSET: 2660 rc = ethtool_get_settings(dev, useraddr); 2661 break; 2662 case ETHTOOL_SSET: 2663 rc = ethtool_set_settings(dev, useraddr); 2664 break; 2665 case ETHTOOL_GDRVINFO: 2666 rc = ethtool_get_drvinfo(dev, useraddr); 2667 break; 2668 case ETHTOOL_GREGS: 2669 rc = ethtool_get_regs(dev, useraddr); 2670 break; 2671 case ETHTOOL_GWOL: 2672 rc = ethtool_get_wol(dev, useraddr); 2673 break; 2674 case ETHTOOL_SWOL: 2675 rc = ethtool_set_wol(dev, useraddr); 2676 break; 2677 case ETHTOOL_GMSGLVL: 2678 rc = ethtool_get_value(dev, useraddr, ethcmd, 2679 dev->ethtool_ops->get_msglevel); 2680 break; 2681 case ETHTOOL_SMSGLVL: 2682 rc = ethtool_set_value_void(dev, useraddr, 2683 dev->ethtool_ops->set_msglevel); 2684 break; 2685 case ETHTOOL_GEEE: 2686 rc = ethtool_get_eee(dev, useraddr); 2687 break; 2688 case ETHTOOL_SEEE: 2689 rc = ethtool_set_eee(dev, useraddr); 2690 break; 2691 case ETHTOOL_NWAY_RST: 2692 rc = ethtool_nway_reset(dev); 2693 break; 2694 case ETHTOOL_GLINK: 2695 rc = ethtool_get_link(dev, useraddr); 2696 break; 2697 case ETHTOOL_GEEPROM: 2698 rc = ethtool_get_eeprom(dev, useraddr); 2699 break; 2700 case ETHTOOL_SEEPROM: 2701 rc = ethtool_set_eeprom(dev, useraddr); 2702 break; 2703 case ETHTOOL_GCOALESCE: 2704 rc = ethtool_get_coalesce(dev, useraddr); 2705 break; 2706 case ETHTOOL_SCOALESCE: 2707 rc = ethtool_set_coalesce(dev, useraddr); 2708 break; 2709 case ETHTOOL_GRINGPARAM: 2710 rc = ethtool_get_ringparam(dev, useraddr); 2711 break; 2712 case ETHTOOL_SRINGPARAM: 2713 rc = ethtool_set_ringparam(dev, useraddr); 2714 break; 2715 case ETHTOOL_GPAUSEPARAM: 2716 rc = ethtool_get_pauseparam(dev, useraddr); 2717 break; 2718 case ETHTOOL_SPAUSEPARAM: 2719 rc = ethtool_set_pauseparam(dev, useraddr); 2720 break; 2721 case ETHTOOL_TEST: 2722 rc = ethtool_self_test(dev, useraddr); 2723 break; 2724 case ETHTOOL_GSTRINGS: 2725 rc = ethtool_get_strings(dev, useraddr); 2726 break; 2727 case ETHTOOL_PHYS_ID: 2728 rc = ethtool_phys_id(dev, useraddr); 2729 break; 2730 case ETHTOOL_GSTATS: 2731 rc = ethtool_get_stats(dev, useraddr); 2732 break; 2733 case ETHTOOL_GPERMADDR: 2734 rc = ethtool_get_perm_addr(dev, useraddr); 2735 break; 2736 case ETHTOOL_GFLAGS: 2737 rc = ethtool_get_value(dev, useraddr, ethcmd, 2738 __ethtool_get_flags); 2739 break; 2740 case ETHTOOL_SFLAGS: 2741 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 2742 break; 2743 case ETHTOOL_GPFLAGS: 2744 rc = ethtool_get_value(dev, useraddr, ethcmd, 2745 dev->ethtool_ops->get_priv_flags); 2746 break; 2747 case ETHTOOL_SPFLAGS: 2748 rc = ethtool_set_value(dev, useraddr, 2749 dev->ethtool_ops->set_priv_flags); 2750 break; 2751 case ETHTOOL_GRXFH: 2752 case ETHTOOL_GRXRINGS: 2753 case ETHTOOL_GRXCLSRLCNT: 2754 case ETHTOOL_GRXCLSRULE: 2755 case ETHTOOL_GRXCLSRLALL: 2756 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 2757 break; 2758 case ETHTOOL_SRXFH: 2759 case ETHTOOL_SRXCLSRLDEL: 2760 case ETHTOOL_SRXCLSRLINS: 2761 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 2762 break; 2763 case ETHTOOL_FLASHDEV: 2764 rc = ethtool_flash_device(dev, useraddr); 2765 break; 2766 case ETHTOOL_RESET: 2767 rc = ethtool_reset(dev, useraddr); 2768 break; 2769 case ETHTOOL_GSSET_INFO: 2770 rc = ethtool_get_sset_info(dev, useraddr); 2771 break; 2772 case ETHTOOL_GRXFHINDIR: 2773 rc = ethtool_get_rxfh_indir(dev, useraddr); 2774 break; 2775 case ETHTOOL_SRXFHINDIR: 2776 rc = ethtool_set_rxfh_indir(dev, useraddr); 2777 break; 2778 case ETHTOOL_GRSSH: 2779 rc = ethtool_get_rxfh(dev, useraddr); 2780 break; 2781 case ETHTOOL_SRSSH: 2782 rc = ethtool_set_rxfh(dev, useraddr); 2783 break; 2784 case ETHTOOL_GFEATURES: 2785 rc = ethtool_get_features(dev, useraddr); 2786 break; 2787 case ETHTOOL_SFEATURES: 2788 rc = ethtool_set_features(dev, useraddr); 2789 break; 2790 case ETHTOOL_GTXCSUM: 2791 case ETHTOOL_GRXCSUM: 2792 case ETHTOOL_GSG: 2793 case ETHTOOL_GTSO: 2794 case ETHTOOL_GGSO: 2795 case ETHTOOL_GGRO: 2796 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 2797 break; 2798 case ETHTOOL_STXCSUM: 2799 case ETHTOOL_SRXCSUM: 2800 case ETHTOOL_SSG: 2801 case ETHTOOL_STSO: 2802 case ETHTOOL_SGSO: 2803 case ETHTOOL_SGRO: 2804 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 2805 break; 2806 case ETHTOOL_GCHANNELS: 2807 rc = ethtool_get_channels(dev, useraddr); 2808 break; 2809 case ETHTOOL_SCHANNELS: 2810 rc = ethtool_set_channels(dev, useraddr); 2811 break; 2812 case ETHTOOL_SET_DUMP: 2813 rc = ethtool_set_dump(dev, useraddr); 2814 break; 2815 case ETHTOOL_GET_DUMP_FLAG: 2816 rc = ethtool_get_dump_flag(dev, useraddr); 2817 break; 2818 case ETHTOOL_GET_DUMP_DATA: 2819 rc = ethtool_get_dump_data(dev, useraddr); 2820 break; 2821 case ETHTOOL_GET_TS_INFO: 2822 rc = ethtool_get_ts_info(dev, useraddr); 2823 break; 2824 case ETHTOOL_GMODULEINFO: 2825 rc = ethtool_get_module_info(dev, useraddr); 2826 break; 2827 case ETHTOOL_GMODULEEEPROM: 2828 rc = ethtool_get_module_eeprom(dev, useraddr); 2829 break; 2830 case ETHTOOL_GTUNABLE: 2831 rc = ethtool_get_tunable(dev, useraddr); 2832 break; 2833 case ETHTOOL_STUNABLE: 2834 rc = ethtool_set_tunable(dev, useraddr); 2835 break; 2836 case ETHTOOL_GPHYSTATS: 2837 rc = ethtool_get_phy_stats(dev, useraddr); 2838 break; 2839 case ETHTOOL_PERQUEUE: 2840 rc = ethtool_set_per_queue(dev, useraddr); 2841 break; 2842 case ETHTOOL_GLINKSETTINGS: 2843 rc = ethtool_get_link_ksettings(dev, useraddr); 2844 break; 2845 case ETHTOOL_SLINKSETTINGS: 2846 rc = ethtool_set_link_ksettings(dev, useraddr); 2847 break; 2848 case ETHTOOL_PHY_GTUNABLE: 2849 rc = get_phy_tunable(dev, useraddr); 2850 break; 2851 case ETHTOOL_PHY_STUNABLE: 2852 rc = set_phy_tunable(dev, useraddr); 2853 break; 2854 case ETHTOOL_GFECPARAM: 2855 rc = ethtool_get_fecparam(dev, useraddr); 2856 break; 2857 case ETHTOOL_SFECPARAM: 2858 rc = ethtool_set_fecparam(dev, useraddr); 2859 break; 2860 default: 2861 rc = -EOPNOTSUPP; 2862 } 2863 2864 if (dev->ethtool_ops->complete) 2865 dev->ethtool_ops->complete(dev); 2866 2867 if (old_features != dev->features) 2868 netdev_features_change(dev); 2869 2870 return rc; 2871}