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