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