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 either indir, hash key or function is valid, proceed further. 1373 * Must request at least one change: indir size, hash key, function 1374 * or input transformation. 1375 */ 1376 if ((rxfh.indir_size && 1377 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && 1378 rxfh.indir_size != dev_indir_size) || 1379 (rxfh.key_size && (rxfh.key_size != dev_key_size)) || 1380 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && 1381 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE && 1382 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE)) 1383 return -EINVAL; 1384 1385 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]); 1386 1387 rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER); 1388 if (!rss_config) 1389 return -ENOMEM; 1390 1391 rx_rings.cmd = ETHTOOL_GRXRINGS; 1392 ret = ops->get_rxnfc(dev, &rx_rings, NULL); 1393 if (ret) 1394 goto out; 1395 1396 /* rxfh.indir_size == 0 means reset the indir table to default (master 1397 * context) or delete the context (other RSS contexts). 1398 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. 1399 */ 1400 if (rxfh.indir_size && 1401 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { 1402 user_indir_len = indir_bytes; 1403 rxfh_dev.indir = (u32 *)rss_config; 1404 rxfh_dev.indir_size = dev_indir_size; 1405 ret = ethtool_copy_validate_indir(rxfh_dev.indir, 1406 useraddr + rss_cfg_offset, 1407 &rx_rings, 1408 rxfh.indir_size); 1409 if (ret) 1410 goto out; 1411 } else if (rxfh.indir_size == 0) { 1412 if (rxfh.rss_context == 0) { 1413 u32 *indir; 1414 1415 rxfh_dev.indir = (u32 *)rss_config; 1416 rxfh_dev.indir_size = dev_indir_size; 1417 indir = rxfh_dev.indir; 1418 for (i = 0; i < dev_indir_size; i++) 1419 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); 1420 } else { 1421 rxfh_dev.rss_delete = true; 1422 } 1423 } 1424 1425 if (rxfh.key_size) { 1426 rxfh_dev.key_size = dev_key_size; 1427 rxfh_dev.key = rss_config + indir_bytes; 1428 if (copy_from_user(rxfh_dev.key, 1429 useraddr + rss_cfg_offset + user_indir_len, 1430 rxfh.key_size)) { 1431 ret = -EFAULT; 1432 goto out; 1433 } 1434 } 1435 1436 if (rxfh.rss_context) { 1437 mutex_lock(&dev->ethtool->rss_lock); 1438 locked = true; 1439 } 1440 if (create) { 1441 if (rxfh_dev.rss_delete) { 1442 ret = -EINVAL; 1443 goto out; 1444 } 1445 ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size); 1446 if (!ctx) { 1447 ret = -ENOMEM; 1448 goto out; 1449 } 1450 1451 if (ops->create_rxfh_context) { 1452 u32 limit = ops->rxfh_max_context_id ?: U32_MAX; 1453 u32 ctx_id; 1454 1455 /* driver uses new API, core allocates ID */ 1456 ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx, 1457 XA_LIMIT(1, limit), GFP_KERNEL_ACCOUNT); 1458 if (ret < 0) { 1459 kfree(ctx); 1460 goto out; 1461 } 1462 WARN_ON(!ctx_id); /* can't happen */ 1463 rxfh.rss_context = ctx_id; 1464 } 1465 } else if (rxfh.rss_context) { 1466 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context); 1467 if (!ctx) { 1468 ret = -ENOENT; 1469 goto out; 1470 } 1471 } 1472 rxfh_dev.hfunc = rxfh.hfunc; 1473 rxfh_dev.rss_context = rxfh.rss_context; 1474 rxfh_dev.input_xfrm = rxfh.input_xfrm; 1475 1476 if (rxfh.rss_context && ops->create_rxfh_context) { 1477 if (create) { 1478 ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev, 1479 extack); 1480 /* Make sure driver populates defaults */ 1481 WARN_ON_ONCE(!ret && !rxfh_dev.key && 1482 !memchr_inv(ethtool_rxfh_context_key(ctx), 1483 0, ctx->key_size)); 1484 } else if (rxfh_dev.rss_delete) { 1485 ret = ops->remove_rxfh_context(dev, ctx, 1486 rxfh.rss_context, 1487 extack); 1488 } else { 1489 ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev, 1490 extack); 1491 } 1492 } else { 1493 ret = ops->set_rxfh(dev, &rxfh_dev, extack); 1494 } 1495 if (ret) { 1496 if (create) { 1497 /* failed to create, free our new tracking entry */ 1498 if (ops->create_rxfh_context) 1499 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context); 1500 kfree(ctx); 1501 } 1502 goto out; 1503 } 1504 1505 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context), 1506 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context))) 1507 ret = -EFAULT; 1508 1509 if (!rxfh_dev.rss_context) { 1510 /* indicate whether rxfh was set to default */ 1511 if (rxfh.indir_size == 0) 1512 dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 1513 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) 1514 dev->priv_flags |= IFF_RXFH_CONFIGURED; 1515 } 1516 /* Update rss_ctx tracking */ 1517 if (create && !ops->create_rxfh_context) { 1518 /* driver uses old API, it chose context ID */ 1519 if (WARN_ON(xa_load(&dev->ethtool->rss_ctx, rxfh_dev.rss_context))) { 1520 /* context ID reused, our tracking is screwed */ 1521 kfree(ctx); 1522 goto out; 1523 } 1524 /* Allocate the exact ID the driver gave us */ 1525 if (xa_is_err(xa_store(&dev->ethtool->rss_ctx, rxfh_dev.rss_context, 1526 ctx, GFP_KERNEL))) { 1527 kfree(ctx); 1528 goto out; 1529 } 1530 1531 /* Fetch the defaults for the old API, in the new API drivers 1532 * should write defaults into ctx themselves. 1533 */ 1534 rxfh_dev.indir = (u32 *)rss_config; 1535 rxfh_dev.indir_size = dev_indir_size; 1536 1537 rxfh_dev.key = rss_config + indir_bytes; 1538 rxfh_dev.key_size = dev_key_size; 1539 1540 ret = ops->get_rxfh(dev, &rxfh_dev); 1541 if (WARN_ON(ret)) { 1542 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context); 1543 kfree(ctx); 1544 goto out; 1545 } 1546 } 1547 if (rxfh_dev.rss_delete) { 1548 WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx); 1549 kfree(ctx); 1550 } else if (ctx) { 1551 if (rxfh_dev.indir) { 1552 for (i = 0; i < dev_indir_size; i++) 1553 ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i]; 1554 ctx->indir_configured = 1555 rxfh.indir_size && 1556 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE; 1557 } 1558 if (rxfh_dev.key) { 1559 memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key, 1560 dev_key_size); 1561 ctx->key_configured = !!rxfh.key_size; 1562 } 1563 if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE) 1564 ctx->hfunc = rxfh_dev.hfunc; 1565 if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE) 1566 ctx->input_xfrm = rxfh_dev.input_xfrm; 1567 } 1568 1569 out: 1570 if (locked) 1571 mutex_unlock(&dev->ethtool->rss_lock); 1572 kfree(rss_config); 1573 return ret; 1574 } 1575 1576 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) 1577 { 1578 struct ethtool_regs regs; 1579 const struct ethtool_ops *ops = dev->ethtool_ops; 1580 void *regbuf; 1581 int reglen, ret; 1582 1583 if (!ops->get_regs || !ops->get_regs_len) 1584 return -EOPNOTSUPP; 1585 1586 if (copy_from_user(®s, useraddr, sizeof(regs))) 1587 return -EFAULT; 1588 1589 reglen = ops->get_regs_len(dev); 1590 if (reglen <= 0) 1591 return reglen; 1592 1593 if (regs.len > reglen) 1594 regs.len = reglen; 1595 1596 regbuf = vzalloc(reglen); 1597 if (!regbuf) 1598 return -ENOMEM; 1599 1600 if (regs.len < reglen) 1601 reglen = regs.len; 1602 1603 ops->get_regs(dev, ®s, regbuf); 1604 1605 ret = -EFAULT; 1606 if (copy_to_user(useraddr, ®s, sizeof(regs))) 1607 goto out; 1608 useraddr += offsetof(struct ethtool_regs, data); 1609 if (copy_to_user(useraddr, regbuf, reglen)) 1610 goto out; 1611 ret = 0; 1612 1613 out: 1614 vfree(regbuf); 1615 return ret; 1616 } 1617 1618 static int ethtool_reset(struct net_device *dev, char __user *useraddr) 1619 { 1620 struct ethtool_value reset; 1621 int ret; 1622 1623 if (!dev->ethtool_ops->reset) 1624 return -EOPNOTSUPP; 1625 1626 if (dev->ethtool->module_fw_flash_in_progress) 1627 return -EBUSY; 1628 1629 if (copy_from_user(&reset, useraddr, sizeof(reset))) 1630 return -EFAULT; 1631 1632 ret = dev->ethtool_ops->reset(dev, &reset.data); 1633 if (ret) 1634 return ret; 1635 1636 if (copy_to_user(useraddr, &reset, sizeof(reset))) 1637 return -EFAULT; 1638 return 0; 1639 } 1640 1641 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) 1642 { 1643 struct ethtool_wolinfo wol; 1644 1645 if (!dev->ethtool_ops->get_wol) 1646 return -EOPNOTSUPP; 1647 1648 memset(&wol, 0, sizeof(struct ethtool_wolinfo)); 1649 wol.cmd = ETHTOOL_GWOL; 1650 dev->ethtool_ops->get_wol(dev, &wol); 1651 1652 if (copy_to_user(useraddr, &wol, sizeof(wol))) 1653 return -EFAULT; 1654 return 0; 1655 } 1656 1657 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) 1658 { 1659 struct ethtool_wolinfo wol, cur_wol; 1660 int ret; 1661 1662 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol) 1663 return -EOPNOTSUPP; 1664 1665 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo)); 1666 cur_wol.cmd = ETHTOOL_GWOL; 1667 dev->ethtool_ops->get_wol(dev, &cur_wol); 1668 1669 if (copy_from_user(&wol, useraddr, sizeof(wol))) 1670 return -EFAULT; 1671 1672 if (wol.wolopts & ~cur_wol.supported) 1673 return -EINVAL; 1674 1675 if (wol.wolopts == cur_wol.wolopts && 1676 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass))) 1677 return 0; 1678 1679 ret = dev->ethtool_ops->set_wol(dev, &wol); 1680 if (ret) 1681 return ret; 1682 1683 dev->ethtool->wol_enabled = !!wol.wolopts; 1684 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL); 1685 1686 return 0; 1687 } 1688 1689 static void eee_to_keee(struct ethtool_keee *keee, 1690 const struct ethtool_eee *eee) 1691 { 1692 memset(keee, 0, sizeof(*keee)); 1693 1694 keee->eee_enabled = eee->eee_enabled; 1695 keee->tx_lpi_enabled = eee->tx_lpi_enabled; 1696 keee->tx_lpi_timer = eee->tx_lpi_timer; 1697 1698 ethtool_convert_legacy_u32_to_link_mode(keee->advertised, 1699 eee->advertised); 1700 } 1701 1702 static void keee_to_eee(struct ethtool_eee *eee, 1703 const struct ethtool_keee *keee) 1704 { 1705 bool overflow; 1706 1707 memset(eee, 0, sizeof(*eee)); 1708 1709 eee->eee_active = keee->eee_active; 1710 eee->eee_enabled = keee->eee_enabled; 1711 eee->tx_lpi_enabled = keee->tx_lpi_enabled; 1712 eee->tx_lpi_timer = keee->tx_lpi_timer; 1713 1714 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported, 1715 keee->supported); 1716 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised, 1717 keee->advertised); 1718 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised, 1719 keee->lp_advertised); 1720 if (overflow) 1721 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n"); 1722 } 1723 1724 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) 1725 { 1726 struct ethtool_keee keee; 1727 struct ethtool_eee eee; 1728 int rc; 1729 1730 if (!dev->ethtool_ops->get_eee) 1731 return -EOPNOTSUPP; 1732 1733 memset(&keee, 0, sizeof(keee)); 1734 rc = dev->ethtool_ops->get_eee(dev, &keee); 1735 if (rc) 1736 return rc; 1737 1738 keee_to_eee(&eee, &keee); 1739 if (copy_to_user(useraddr, &eee, sizeof(eee))) 1740 return -EFAULT; 1741 1742 return 0; 1743 } 1744 1745 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) 1746 { 1747 struct ethtool_keee keee; 1748 struct ethtool_eee eee; 1749 int ret; 1750 1751 if (!dev->ethtool_ops->set_eee) 1752 return -EOPNOTSUPP; 1753 1754 if (copy_from_user(&eee, useraddr, sizeof(eee))) 1755 return -EFAULT; 1756 1757 eee_to_keee(&keee, &eee); 1758 ret = dev->ethtool_ops->set_eee(dev, &keee); 1759 if (!ret) 1760 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL); 1761 return ret; 1762 } 1763 1764 static int ethtool_nway_reset(struct net_device *dev) 1765 { 1766 if (!dev->ethtool_ops->nway_reset) 1767 return -EOPNOTSUPP; 1768 1769 return dev->ethtool_ops->nway_reset(dev); 1770 } 1771 1772 static int ethtool_get_link(struct net_device *dev, char __user *useraddr) 1773 { 1774 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; 1775 int link = __ethtool_get_link(dev); 1776 1777 if (link < 0) 1778 return link; 1779 1780 edata.data = link; 1781 if (copy_to_user(useraddr, &edata, sizeof(edata))) 1782 return -EFAULT; 1783 return 0; 1784 } 1785 1786 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, 1787 int (*getter)(struct net_device *, 1788 struct ethtool_eeprom *, u8 *), 1789 u32 total_len) 1790 { 1791 struct ethtool_eeprom eeprom; 1792 void __user *userbuf = useraddr + sizeof(eeprom); 1793 u32 bytes_remaining; 1794 u8 *data; 1795 int ret = 0; 1796 1797 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1798 return -EFAULT; 1799 1800 /* Check for wrap and zero */ 1801 if (eeprom.offset + eeprom.len <= eeprom.offset) 1802 return -EINVAL; 1803 1804 /* Check for exceeding total eeprom len */ 1805 if (eeprom.offset + eeprom.len > total_len) 1806 return -EINVAL; 1807 1808 data = kzalloc(PAGE_SIZE, GFP_USER); 1809 if (!data) 1810 return -ENOMEM; 1811 1812 bytes_remaining = eeprom.len; 1813 while (bytes_remaining > 0) { 1814 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1815 1816 ret = getter(dev, &eeprom, data); 1817 if (ret) 1818 break; 1819 if (!eeprom.len) { 1820 ret = -EIO; 1821 break; 1822 } 1823 if (copy_to_user(userbuf, data, eeprom.len)) { 1824 ret = -EFAULT; 1825 break; 1826 } 1827 userbuf += eeprom.len; 1828 eeprom.offset += eeprom.len; 1829 bytes_remaining -= eeprom.len; 1830 } 1831 1832 eeprom.len = userbuf - (useraddr + sizeof(eeprom)); 1833 eeprom.offset -= eeprom.len; 1834 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) 1835 ret = -EFAULT; 1836 1837 kfree(data); 1838 return ret; 1839 } 1840 1841 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) 1842 { 1843 const struct ethtool_ops *ops = dev->ethtool_ops; 1844 1845 if (!ops->get_eeprom || !ops->get_eeprom_len || 1846 !ops->get_eeprom_len(dev)) 1847 return -EOPNOTSUPP; 1848 1849 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, 1850 ops->get_eeprom_len(dev)); 1851 } 1852 1853 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) 1854 { 1855 struct ethtool_eeprom eeprom; 1856 const struct ethtool_ops *ops = dev->ethtool_ops; 1857 void __user *userbuf = useraddr + sizeof(eeprom); 1858 u32 bytes_remaining; 1859 u8 *data; 1860 int ret = 0; 1861 1862 if (!ops->set_eeprom || !ops->get_eeprom_len || 1863 !ops->get_eeprom_len(dev)) 1864 return -EOPNOTSUPP; 1865 1866 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1867 return -EFAULT; 1868 1869 /* Check for wrap and zero */ 1870 if (eeprom.offset + eeprom.len <= eeprom.offset) 1871 return -EINVAL; 1872 1873 /* Check for exceeding total eeprom len */ 1874 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) 1875 return -EINVAL; 1876 1877 data = kzalloc(PAGE_SIZE, GFP_USER); 1878 if (!data) 1879 return -ENOMEM; 1880 1881 bytes_remaining = eeprom.len; 1882 while (bytes_remaining > 0) { 1883 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1884 1885 if (copy_from_user(data, userbuf, eeprom.len)) { 1886 ret = -EFAULT; 1887 break; 1888 } 1889 ret = ops->set_eeprom(dev, &eeprom, data); 1890 if (ret) 1891 break; 1892 userbuf += eeprom.len; 1893 eeprom.offset += eeprom.len; 1894 bytes_remaining -= eeprom.len; 1895 } 1896 1897 kfree(data); 1898 return ret; 1899 } 1900 1901 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, 1902 void __user *useraddr) 1903 { 1904 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 1905 struct kernel_ethtool_coalesce kernel_coalesce = {}; 1906 int ret; 1907 1908 if (!dev->ethtool_ops->get_coalesce) 1909 return -EOPNOTSUPP; 1910 1911 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, 1912 NULL); 1913 if (ret) 1914 return ret; 1915 1916 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 1917 return -EFAULT; 1918 return 0; 1919 } 1920 1921 static bool 1922 ethtool_set_coalesce_supported(struct net_device *dev, 1923 struct ethtool_coalesce *coalesce) 1924 { 1925 u32 supported_params = dev->ethtool_ops->supported_coalesce_params; 1926 u32 nonzero_params = 0; 1927 1928 if (coalesce->rx_coalesce_usecs) 1929 nonzero_params |= ETHTOOL_COALESCE_RX_USECS; 1930 if (coalesce->rx_max_coalesced_frames) 1931 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES; 1932 if (coalesce->rx_coalesce_usecs_irq) 1933 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ; 1934 if (coalesce->rx_max_coalesced_frames_irq) 1935 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ; 1936 if (coalesce->tx_coalesce_usecs) 1937 nonzero_params |= ETHTOOL_COALESCE_TX_USECS; 1938 if (coalesce->tx_max_coalesced_frames) 1939 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES; 1940 if (coalesce->tx_coalesce_usecs_irq) 1941 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ; 1942 if (coalesce->tx_max_coalesced_frames_irq) 1943 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ; 1944 if (coalesce->stats_block_coalesce_usecs) 1945 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS; 1946 if (coalesce->use_adaptive_rx_coalesce) 1947 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX; 1948 if (coalesce->use_adaptive_tx_coalesce) 1949 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX; 1950 if (coalesce->pkt_rate_low) 1951 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW; 1952 if (coalesce->rx_coalesce_usecs_low) 1953 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW; 1954 if (coalesce->rx_max_coalesced_frames_low) 1955 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW; 1956 if (coalesce->tx_coalesce_usecs_low) 1957 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW; 1958 if (coalesce->tx_max_coalesced_frames_low) 1959 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW; 1960 if (coalesce->pkt_rate_high) 1961 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH; 1962 if (coalesce->rx_coalesce_usecs_high) 1963 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH; 1964 if (coalesce->rx_max_coalesced_frames_high) 1965 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH; 1966 if (coalesce->tx_coalesce_usecs_high) 1967 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH; 1968 if (coalesce->tx_max_coalesced_frames_high) 1969 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH; 1970 if (coalesce->rate_sample_interval) 1971 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL; 1972 1973 return (supported_params & nonzero_params) == nonzero_params; 1974 } 1975 1976 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, 1977 void __user *useraddr) 1978 { 1979 struct kernel_ethtool_coalesce kernel_coalesce = {}; 1980 struct ethtool_coalesce coalesce; 1981 int ret; 1982 1983 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce) 1984 return -EOPNOTSUPP; 1985 1986 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, 1987 NULL); 1988 if (ret) 1989 return ret; 1990 1991 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) 1992 return -EFAULT; 1993 1994 if (!ethtool_set_coalesce_supported(dev, &coalesce)) 1995 return -EOPNOTSUPP; 1996 1997 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce, 1998 NULL); 1999 if (!ret) 2000 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL); 2001 return ret; 2002 } 2003 2004 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) 2005 { 2006 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; 2007 struct kernel_ethtool_ringparam kernel_ringparam = {}; 2008 2009 if (!dev->ethtool_ops->get_ringparam) 2010 return -EOPNOTSUPP; 2011 2012 dev->ethtool_ops->get_ringparam(dev, &ringparam, 2013 &kernel_ringparam, NULL); 2014 2015 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) 2016 return -EFAULT; 2017 return 0; 2018 } 2019 2020 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) 2021 { 2022 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM }; 2023 struct kernel_ethtool_ringparam kernel_ringparam; 2024 int ret; 2025 2026 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) 2027 return -EOPNOTSUPP; 2028 2029 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) 2030 return -EFAULT; 2031 2032 dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL); 2033 2034 /* ensure new ring parameters are within the maximums */ 2035 if (ringparam.rx_pending > max.rx_max_pending || 2036 ringparam.rx_mini_pending > max.rx_mini_max_pending || 2037 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || 2038 ringparam.tx_pending > max.tx_max_pending) 2039 return -EINVAL; 2040 2041 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam, 2042 &kernel_ringparam, NULL); 2043 if (!ret) 2044 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL); 2045 return ret; 2046 } 2047 2048 static noinline_for_stack int ethtool_get_channels(struct net_device *dev, 2049 void __user *useraddr) 2050 { 2051 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; 2052 2053 if (!dev->ethtool_ops->get_channels) 2054 return -EOPNOTSUPP; 2055 2056 dev->ethtool_ops->get_channels(dev, &channels); 2057 2058 if (copy_to_user(useraddr, &channels, sizeof(channels))) 2059 return -EFAULT; 2060 return 0; 2061 } 2062 2063 static noinline_for_stack int ethtool_set_channels(struct net_device *dev, 2064 void __user *useraddr) 2065 { 2066 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS }; 2067 u16 from_channel, to_channel; 2068 u64 max_rxnfc_in_use; 2069 u32 max_rxfh_in_use; 2070 unsigned int i; 2071 int ret; 2072 2073 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) 2074 return -EOPNOTSUPP; 2075 2076 if (copy_from_user(&channels, useraddr, sizeof(channels))) 2077 return -EFAULT; 2078 2079 dev->ethtool_ops->get_channels(dev, &curr); 2080 2081 if (channels.rx_count == curr.rx_count && 2082 channels.tx_count == curr.tx_count && 2083 channels.combined_count == curr.combined_count && 2084 channels.other_count == curr.other_count) 2085 return 0; 2086 2087 /* ensure new counts are within the maximums */ 2088 if (channels.rx_count > curr.max_rx || 2089 channels.tx_count > curr.max_tx || 2090 channels.combined_count > curr.max_combined || 2091 channels.other_count > curr.max_other) 2092 return -EINVAL; 2093 2094 /* ensure there is at least one RX and one TX channel */ 2095 if (!channels.combined_count && 2096 (!channels.rx_count || !channels.tx_count)) 2097 return -EINVAL; 2098 2099 /* ensure the new Rx count fits within the configured Rx flow 2100 * indirection table/rxnfc settings */ 2101 if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use)) 2102 max_rxnfc_in_use = 0; 2103 max_rxfh_in_use = ethtool_get_max_rxfh_channel(dev); 2104 if (channels.combined_count + channels.rx_count <= 2105 max_t(u64, max_rxnfc_in_use, max_rxfh_in_use)) 2106 return -EINVAL; 2107 2108 /* Disabling channels, query zero-copy AF_XDP sockets */ 2109 from_channel = channels.combined_count + 2110 min(channels.rx_count, channels.tx_count); 2111 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count); 2112 for (i = from_channel; i < to_channel; i++) 2113 if (xsk_get_pool_from_qid(dev, i)) 2114 return -EINVAL; 2115 2116 ret = dev->ethtool_ops->set_channels(dev, &channels); 2117 if (!ret) 2118 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL); 2119 return ret; 2120 } 2121 2122 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) 2123 { 2124 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM }; 2125 2126 if (!dev->ethtool_ops->get_pauseparam) 2127 return -EOPNOTSUPP; 2128 2129 dev->ethtool_ops->get_pauseparam(dev, &pauseparam); 2130 2131 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) 2132 return -EFAULT; 2133 return 0; 2134 } 2135 2136 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) 2137 { 2138 struct ethtool_pauseparam pauseparam; 2139 int ret; 2140 2141 if (!dev->ethtool_ops->set_pauseparam) 2142 return -EOPNOTSUPP; 2143 2144 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) 2145 return -EFAULT; 2146 2147 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam); 2148 if (!ret) 2149 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 2150 return ret; 2151 } 2152 2153 static int ethtool_self_test(struct net_device *dev, char __user *useraddr) 2154 { 2155 struct ethtool_test test; 2156 const struct ethtool_ops *ops = dev->ethtool_ops; 2157 u64 *data; 2158 int ret, test_len; 2159 2160 if (!ops->self_test || !ops->get_sset_count) 2161 return -EOPNOTSUPP; 2162 2163 test_len = ops->get_sset_count(dev, ETH_SS_TEST); 2164 if (test_len < 0) 2165 return test_len; 2166 WARN_ON(test_len == 0); 2167 2168 if (copy_from_user(&test, useraddr, sizeof(test))) 2169 return -EFAULT; 2170 2171 test.len = test_len; 2172 data = kcalloc(test_len, sizeof(u64), GFP_USER); 2173 if (!data) 2174 return -ENOMEM; 2175 2176 netif_testing_on(dev); 2177 ops->self_test(dev, &test, data); 2178 netif_testing_off(dev); 2179 2180 ret = -EFAULT; 2181 if (copy_to_user(useraddr, &test, sizeof(test))) 2182 goto out; 2183 useraddr += sizeof(test); 2184 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64)))) 2185 goto out; 2186 ret = 0; 2187 2188 out: 2189 kfree(data); 2190 return ret; 2191 } 2192 2193 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) 2194 { 2195 struct ethtool_gstrings gstrings; 2196 u8 *data; 2197 int ret; 2198 2199 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) 2200 return -EFAULT; 2201 2202 ret = __ethtool_get_sset_count(dev, gstrings.string_set); 2203 if (ret < 0) 2204 return ret; 2205 if (ret > S32_MAX / ETH_GSTRING_LEN) 2206 return -ENOMEM; 2207 WARN_ON_ONCE(!ret); 2208 2209 gstrings.len = ret; 2210 2211 if (gstrings.len) { 2212 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); 2213 if (!data) 2214 return -ENOMEM; 2215 2216 __ethtool_get_strings(dev, gstrings.string_set, data); 2217 } else { 2218 data = NULL; 2219 } 2220 2221 ret = -EFAULT; 2222 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) 2223 goto out; 2224 useraddr += sizeof(gstrings); 2225 if (gstrings.len && 2226 copy_to_user(useraddr, data, 2227 array_size(gstrings.len, ETH_GSTRING_LEN))) 2228 goto out; 2229 ret = 0; 2230 2231 out: 2232 vfree(data); 2233 return ret; 2234 } 2235 2236 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...) 2237 { 2238 va_list args; 2239 2240 va_start(args, fmt); 2241 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args); 2242 va_end(args); 2243 2244 *data += ETH_GSTRING_LEN; 2245 } 2246 EXPORT_SYMBOL(ethtool_sprintf); 2247 2248 void ethtool_puts(u8 **data, const char *str) 2249 { 2250 strscpy(*data, str, ETH_GSTRING_LEN); 2251 *data += ETH_GSTRING_LEN; 2252 } 2253 EXPORT_SYMBOL(ethtool_puts); 2254 2255 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) 2256 { 2257 struct ethtool_value id; 2258 static bool busy; 2259 const struct ethtool_ops *ops = dev->ethtool_ops; 2260 netdevice_tracker dev_tracker; 2261 int rc; 2262 2263 if (!ops->set_phys_id) 2264 return -EOPNOTSUPP; 2265 2266 if (busy) 2267 return -EBUSY; 2268 2269 if (copy_from_user(&id, useraddr, sizeof(id))) 2270 return -EFAULT; 2271 2272 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); 2273 if (rc < 0) 2274 return rc; 2275 2276 /* Drop the RTNL lock while waiting, but prevent reentry or 2277 * removal of the device. 2278 */ 2279 busy = true; 2280 netdev_hold(dev, &dev_tracker, GFP_KERNEL); 2281 rtnl_unlock(); 2282 2283 if (rc == 0) { 2284 /* Driver will handle this itself */ 2285 schedule_timeout_interruptible( 2286 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 2287 } else { 2288 /* Driver expects to be called at twice the frequency in rc */ 2289 int n = rc * 2, interval = HZ / n; 2290 u64 count = mul_u32_u32(n, id.data); 2291 u64 i = 0; 2292 2293 do { 2294 rtnl_lock(); 2295 rc = ops->set_phys_id(dev, 2296 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 2297 rtnl_unlock(); 2298 if (rc) 2299 break; 2300 schedule_timeout_interruptible(interval); 2301 } while (!signal_pending(current) && (!id.data || i < count)); 2302 } 2303 2304 rtnl_lock(); 2305 netdev_put(dev, &dev_tracker); 2306 busy = false; 2307 2308 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 2309 return rc; 2310 } 2311 2312 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 2313 { 2314 struct ethtool_stats stats; 2315 const struct ethtool_ops *ops = dev->ethtool_ops; 2316 u64 *data; 2317 int ret, n_stats; 2318 2319 if (!ops->get_ethtool_stats || !ops->get_sset_count) 2320 return -EOPNOTSUPP; 2321 2322 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 2323 if (n_stats < 0) 2324 return n_stats; 2325 if (n_stats > S32_MAX / sizeof(u64)) 2326 return -ENOMEM; 2327 WARN_ON_ONCE(!n_stats); 2328 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2329 return -EFAULT; 2330 2331 stats.n_stats = n_stats; 2332 2333 if (n_stats) { 2334 data = vzalloc(array_size(n_stats, sizeof(u64))); 2335 if (!data) 2336 return -ENOMEM; 2337 ops->get_ethtool_stats(dev, &stats, data); 2338 } else { 2339 data = NULL; 2340 } 2341 2342 ret = -EFAULT; 2343 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2344 goto out; 2345 useraddr += sizeof(stats); 2346 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2347 goto out; 2348 ret = 0; 2349 2350 out: 2351 vfree(data); 2352 return ret; 2353 } 2354 2355 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data) 2356 { 2357 if (n_stats < 0) 2358 return n_stats; 2359 if (n_stats > S32_MAX / sizeof(u64)) 2360 return -ENOMEM; 2361 if (WARN_ON_ONCE(!n_stats)) 2362 return -EOPNOTSUPP; 2363 2364 *data = vzalloc(array_size(n_stats, sizeof(u64))); 2365 if (!*data) 2366 return -ENOMEM; 2367 2368 return 0; 2369 } 2370 2371 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev, 2372 struct ethtool_stats *stats, 2373 u64 **data) 2374 { 2375 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 2376 int n_stats, ret; 2377 2378 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats) 2379 return -EOPNOTSUPP; 2380 2381 n_stats = phy_ops->get_sset_count(phydev); 2382 2383 ret = ethtool_vzalloc_stats_array(n_stats, data); 2384 if (ret) 2385 return ret; 2386 2387 stats->n_stats = n_stats; 2388 return phy_ops->get_stats(phydev, stats, *data); 2389 } 2390 2391 static int ethtool_get_phy_stats_ethtool(struct net_device *dev, 2392 struct ethtool_stats *stats, 2393 u64 **data) 2394 { 2395 const struct ethtool_ops *ops = dev->ethtool_ops; 2396 int n_stats, ret; 2397 2398 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats) 2399 return -EOPNOTSUPP; 2400 2401 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 2402 2403 ret = ethtool_vzalloc_stats_array(n_stats, data); 2404 if (ret) 2405 return ret; 2406 2407 stats->n_stats = n_stats; 2408 ops->get_ethtool_phy_stats(dev, stats, *data); 2409 2410 return 0; 2411 } 2412 2413 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 2414 { 2415 struct phy_device *phydev = dev->phydev; 2416 struct ethtool_stats stats; 2417 u64 *data = NULL; 2418 int ret = -EOPNOTSUPP; 2419 2420 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2421 return -EFAULT; 2422 2423 if (phydev) 2424 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data); 2425 2426 if (ret == -EOPNOTSUPP) 2427 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data); 2428 2429 if (ret) 2430 goto out; 2431 2432 if (copy_to_user(useraddr, &stats, sizeof(stats))) { 2433 ret = -EFAULT; 2434 goto out; 2435 } 2436 2437 useraddr += sizeof(stats); 2438 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64)))) 2439 ret = -EFAULT; 2440 2441 out: 2442 vfree(data); 2443 return ret; 2444 } 2445 2446 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2447 { 2448 struct ethtool_perm_addr epaddr; 2449 2450 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2451 return -EFAULT; 2452 2453 if (epaddr.size < dev->addr_len) 2454 return -ETOOSMALL; 2455 epaddr.size = dev->addr_len; 2456 2457 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2458 return -EFAULT; 2459 useraddr += sizeof(epaddr); 2460 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2461 return -EFAULT; 2462 return 0; 2463 } 2464 2465 static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2466 u32 cmd, u32 (*actor)(struct net_device *)) 2467 { 2468 struct ethtool_value edata = { .cmd = cmd }; 2469 2470 if (!actor) 2471 return -EOPNOTSUPP; 2472 2473 edata.data = actor(dev); 2474 2475 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2476 return -EFAULT; 2477 return 0; 2478 } 2479 2480 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2481 void (*actor)(struct net_device *, u32)) 2482 { 2483 struct ethtool_value edata; 2484 2485 if (!actor) 2486 return -EOPNOTSUPP; 2487 2488 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2489 return -EFAULT; 2490 2491 actor(dev, edata.data); 2492 return 0; 2493 } 2494 2495 static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2496 int (*actor)(struct net_device *, u32)) 2497 { 2498 struct ethtool_value edata; 2499 2500 if (!actor) 2501 return -EOPNOTSUPP; 2502 2503 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2504 return -EFAULT; 2505 2506 return actor(dev, edata.data); 2507 } 2508 2509 static int 2510 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req) 2511 { 2512 if (!dev->ethtool_ops->flash_device) { 2513 req->devlink = netdev_to_devlink_get(dev); 2514 return 0; 2515 } 2516 2517 return dev->ethtool_ops->flash_device(dev, &req->efl); 2518 } 2519 2520 static int ethtool_set_dump(struct net_device *dev, 2521 void __user *useraddr) 2522 { 2523 struct ethtool_dump dump; 2524 2525 if (!dev->ethtool_ops->set_dump) 2526 return -EOPNOTSUPP; 2527 2528 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2529 return -EFAULT; 2530 2531 return dev->ethtool_ops->set_dump(dev, &dump); 2532 } 2533 2534 static int ethtool_get_dump_flag(struct net_device *dev, 2535 void __user *useraddr) 2536 { 2537 int ret; 2538 struct ethtool_dump dump; 2539 const struct ethtool_ops *ops = dev->ethtool_ops; 2540 2541 if (!ops->get_dump_flag) 2542 return -EOPNOTSUPP; 2543 2544 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2545 return -EFAULT; 2546 2547 ret = ops->get_dump_flag(dev, &dump); 2548 if (ret) 2549 return ret; 2550 2551 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2552 return -EFAULT; 2553 return 0; 2554 } 2555 2556 static int ethtool_get_dump_data(struct net_device *dev, 2557 void __user *useraddr) 2558 { 2559 int ret; 2560 __u32 len; 2561 struct ethtool_dump dump, tmp; 2562 const struct ethtool_ops *ops = dev->ethtool_ops; 2563 void *data = NULL; 2564 2565 if (!ops->get_dump_data || !ops->get_dump_flag) 2566 return -EOPNOTSUPP; 2567 2568 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2569 return -EFAULT; 2570 2571 memset(&tmp, 0, sizeof(tmp)); 2572 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2573 ret = ops->get_dump_flag(dev, &tmp); 2574 if (ret) 2575 return ret; 2576 2577 len = min(tmp.len, dump.len); 2578 if (!len) 2579 return -EFAULT; 2580 2581 /* Don't ever let the driver think there's more space available 2582 * than it requested with .get_dump_flag(). 2583 */ 2584 dump.len = len; 2585 2586 /* Always allocate enough space to hold the whole thing so that the 2587 * driver does not need to check the length and bother with partial 2588 * dumping. 2589 */ 2590 data = vzalloc(tmp.len); 2591 if (!data) 2592 return -ENOMEM; 2593 ret = ops->get_dump_data(dev, &dump, data); 2594 if (ret) 2595 goto out; 2596 2597 /* There are two sane possibilities: 2598 * 1. The driver's .get_dump_data() does not touch dump.len. 2599 * 2. Or it may set dump.len to how much it really writes, which 2600 * should be tmp.len (or len if it can do a partial dump). 2601 * In any case respond to userspace with the actual length of data 2602 * it's receiving. 2603 */ 2604 WARN_ON(dump.len != len && dump.len != tmp.len); 2605 dump.len = len; 2606 2607 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2608 ret = -EFAULT; 2609 goto out; 2610 } 2611 useraddr += offsetof(struct ethtool_dump, data); 2612 if (copy_to_user(useraddr, data, len)) 2613 ret = -EFAULT; 2614 out: 2615 vfree(data); 2616 return ret; 2617 } 2618 2619 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2620 { 2621 struct kernel_ethtool_ts_info kernel_info; 2622 struct ethtool_ts_info info = {}; 2623 int err; 2624 2625 err = __ethtool_get_ts_info(dev, &kernel_info); 2626 if (err) 2627 return err; 2628 2629 info.cmd = kernel_info.cmd; 2630 info.so_timestamping = kernel_info.so_timestamping; 2631 info.phc_index = kernel_info.phc_index; 2632 info.tx_types = kernel_info.tx_types; 2633 info.rx_filters = kernel_info.rx_filters; 2634 2635 if (copy_to_user(useraddr, &info, sizeof(info))) 2636 return -EFAULT; 2637 2638 return 0; 2639 } 2640 2641 int ethtool_get_module_info_call(struct net_device *dev, 2642 struct ethtool_modinfo *modinfo) 2643 { 2644 const struct ethtool_ops *ops = dev->ethtool_ops; 2645 struct phy_device *phydev = dev->phydev; 2646 2647 if (dev->ethtool->module_fw_flash_in_progress) 2648 return -EBUSY; 2649 2650 if (dev->sfp_bus) 2651 return sfp_get_module_info(dev->sfp_bus, modinfo); 2652 2653 if (phydev && phydev->drv && phydev->drv->module_info) 2654 return phydev->drv->module_info(phydev, modinfo); 2655 2656 if (ops->get_module_info) 2657 return ops->get_module_info(dev, modinfo); 2658 2659 return -EOPNOTSUPP; 2660 } 2661 2662 static int ethtool_get_module_info(struct net_device *dev, 2663 void __user *useraddr) 2664 { 2665 int ret; 2666 struct ethtool_modinfo modinfo; 2667 2668 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2669 return -EFAULT; 2670 2671 ret = ethtool_get_module_info_call(dev, &modinfo); 2672 if (ret) 2673 return ret; 2674 2675 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2676 return -EFAULT; 2677 2678 return 0; 2679 } 2680 2681 int ethtool_get_module_eeprom_call(struct net_device *dev, 2682 struct ethtool_eeprom *ee, u8 *data) 2683 { 2684 const struct ethtool_ops *ops = dev->ethtool_ops; 2685 struct phy_device *phydev = dev->phydev; 2686 2687 if (dev->ethtool->module_fw_flash_in_progress) 2688 return -EBUSY; 2689 2690 if (dev->sfp_bus) 2691 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2692 2693 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2694 return phydev->drv->module_eeprom(phydev, ee, data); 2695 2696 if (ops->get_module_eeprom) 2697 return ops->get_module_eeprom(dev, ee, data); 2698 2699 return -EOPNOTSUPP; 2700 } 2701 2702 static int ethtool_get_module_eeprom(struct net_device *dev, 2703 void __user *useraddr) 2704 { 2705 int ret; 2706 struct ethtool_modinfo modinfo; 2707 2708 ret = ethtool_get_module_info_call(dev, &modinfo); 2709 if (ret) 2710 return ret; 2711 2712 return ethtool_get_any_eeprom(dev, useraddr, 2713 ethtool_get_module_eeprom_call, 2714 modinfo.eeprom_len); 2715 } 2716 2717 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2718 { 2719 switch (tuna->id) { 2720 case ETHTOOL_RX_COPYBREAK: 2721 case ETHTOOL_TX_COPYBREAK: 2722 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 2723 if (tuna->len != sizeof(u32) || 2724 tuna->type_id != ETHTOOL_TUNABLE_U32) 2725 return -EINVAL; 2726 break; 2727 case ETHTOOL_PFC_PREVENTION_TOUT: 2728 if (tuna->len != sizeof(u16) || 2729 tuna->type_id != ETHTOOL_TUNABLE_U16) 2730 return -EINVAL; 2731 break; 2732 default: 2733 return -EINVAL; 2734 } 2735 2736 return 0; 2737 } 2738 2739 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2740 { 2741 int ret; 2742 struct ethtool_tunable tuna; 2743 const struct ethtool_ops *ops = dev->ethtool_ops; 2744 void *data; 2745 2746 if (!ops->get_tunable) 2747 return -EOPNOTSUPP; 2748 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2749 return -EFAULT; 2750 ret = ethtool_tunable_valid(&tuna); 2751 if (ret) 2752 return ret; 2753 data = kzalloc(tuna.len, GFP_USER); 2754 if (!data) 2755 return -ENOMEM; 2756 ret = ops->get_tunable(dev, &tuna, data); 2757 if (ret) 2758 goto out; 2759 useraddr += sizeof(tuna); 2760 ret = -EFAULT; 2761 if (copy_to_user(useraddr, data, tuna.len)) 2762 goto out; 2763 ret = 0; 2764 2765 out: 2766 kfree(data); 2767 return ret; 2768 } 2769 2770 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2771 { 2772 int ret; 2773 struct ethtool_tunable tuna; 2774 const struct ethtool_ops *ops = dev->ethtool_ops; 2775 void *data; 2776 2777 if (!ops->set_tunable) 2778 return -EOPNOTSUPP; 2779 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2780 return -EFAULT; 2781 ret = ethtool_tunable_valid(&tuna); 2782 if (ret) 2783 return ret; 2784 useraddr += sizeof(tuna); 2785 data = memdup_user(useraddr, tuna.len); 2786 if (IS_ERR(data)) 2787 return PTR_ERR(data); 2788 ret = ops->set_tunable(dev, &tuna, data); 2789 2790 kfree(data); 2791 return ret; 2792 } 2793 2794 static noinline_for_stack int 2795 ethtool_get_per_queue_coalesce(struct net_device *dev, 2796 void __user *useraddr, 2797 struct ethtool_per_queue_op *per_queue_opt) 2798 { 2799 u32 bit; 2800 int ret; 2801 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2802 2803 if (!dev->ethtool_ops->get_per_queue_coalesce) 2804 return -EOPNOTSUPP; 2805 2806 useraddr += sizeof(*per_queue_opt); 2807 2808 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 2809 MAX_NUM_QUEUE); 2810 2811 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2812 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2813 2814 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 2815 if (ret != 0) 2816 return ret; 2817 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2818 return -EFAULT; 2819 useraddr += sizeof(coalesce); 2820 } 2821 2822 return 0; 2823 } 2824 2825 static noinline_for_stack int 2826 ethtool_set_per_queue_coalesce(struct net_device *dev, 2827 void __user *useraddr, 2828 struct ethtool_per_queue_op *per_queue_opt) 2829 { 2830 u32 bit; 2831 int i, ret = 0; 2832 int n_queue; 2833 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 2834 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2835 2836 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 2837 (!dev->ethtool_ops->get_per_queue_coalesce)) 2838 return -EOPNOTSUPP; 2839 2840 useraddr += sizeof(*per_queue_opt); 2841 2842 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 2843 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 2844 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); 2845 if (!backup) 2846 return -ENOMEM; 2847 2848 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2849 struct ethtool_coalesce coalesce; 2850 2851 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 2852 if (ret != 0) 2853 goto roll_back; 2854 2855 tmp++; 2856 2857 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 2858 ret = -EFAULT; 2859 goto roll_back; 2860 } 2861 2862 if (!ethtool_set_coalesce_supported(dev, &coalesce)) { 2863 ret = -EOPNOTSUPP; 2864 goto roll_back; 2865 } 2866 2867 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 2868 if (ret != 0) 2869 goto roll_back; 2870 2871 useraddr += sizeof(coalesce); 2872 } 2873 2874 roll_back: 2875 if (ret != 0) { 2876 tmp = backup; 2877 for_each_set_bit(i, queue_mask, bit) { 2878 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 2879 tmp++; 2880 } 2881 } 2882 kfree(backup); 2883 2884 return ret; 2885 } 2886 2887 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, 2888 void __user *useraddr, u32 sub_cmd) 2889 { 2890 struct ethtool_per_queue_op per_queue_opt; 2891 2892 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 2893 return -EFAULT; 2894 2895 if (per_queue_opt.sub_command != sub_cmd) 2896 return -EINVAL; 2897 2898 switch (per_queue_opt.sub_command) { 2899 case ETHTOOL_GCOALESCE: 2900 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2901 case ETHTOOL_SCOALESCE: 2902 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2903 default: 2904 return -EOPNOTSUPP; 2905 } 2906 } 2907 2908 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 2909 { 2910 switch (tuna->id) { 2911 case ETHTOOL_PHY_DOWNSHIFT: 2912 case ETHTOOL_PHY_FAST_LINK_DOWN: 2913 if (tuna->len != sizeof(u8) || 2914 tuna->type_id != ETHTOOL_TUNABLE_U8) 2915 return -EINVAL; 2916 break; 2917 case ETHTOOL_PHY_EDPD: 2918 if (tuna->len != sizeof(u16) || 2919 tuna->type_id != ETHTOOL_TUNABLE_U16) 2920 return -EINVAL; 2921 break; 2922 default: 2923 return -EINVAL; 2924 } 2925 2926 return 0; 2927 } 2928 2929 static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 2930 { 2931 struct phy_device *phydev = dev->phydev; 2932 struct ethtool_tunable tuna; 2933 bool phy_drv_tunable; 2934 void *data; 2935 int ret; 2936 2937 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2938 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) 2939 return -EOPNOTSUPP; 2940 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2941 return -EFAULT; 2942 ret = ethtool_phy_tunable_valid(&tuna); 2943 if (ret) 2944 return ret; 2945 data = kzalloc(tuna.len, GFP_USER); 2946 if (!data) 2947 return -ENOMEM; 2948 if (phy_drv_tunable) { 2949 mutex_lock(&phydev->lock); 2950 ret = phydev->drv->get_tunable(phydev, &tuna, data); 2951 mutex_unlock(&phydev->lock); 2952 } else { 2953 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); 2954 } 2955 if (ret) 2956 goto out; 2957 useraddr += sizeof(tuna); 2958 ret = -EFAULT; 2959 if (copy_to_user(useraddr, data, tuna.len)) 2960 goto out; 2961 ret = 0; 2962 2963 out: 2964 kfree(data); 2965 return ret; 2966 } 2967 2968 static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 2969 { 2970 struct phy_device *phydev = dev->phydev; 2971 struct ethtool_tunable tuna; 2972 bool phy_drv_tunable; 2973 void *data; 2974 int ret; 2975 2976 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2977 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) 2978 return -EOPNOTSUPP; 2979 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2980 return -EFAULT; 2981 ret = ethtool_phy_tunable_valid(&tuna); 2982 if (ret) 2983 return ret; 2984 useraddr += sizeof(tuna); 2985 data = memdup_user(useraddr, tuna.len); 2986 if (IS_ERR(data)) 2987 return PTR_ERR(data); 2988 if (phy_drv_tunable) { 2989 mutex_lock(&phydev->lock); 2990 ret = phydev->drv->set_tunable(phydev, &tuna, data); 2991 mutex_unlock(&phydev->lock); 2992 } else { 2993 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); 2994 } 2995 2996 kfree(data); 2997 return ret; 2998 } 2999 3000 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 3001 { 3002 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; 3003 int rc; 3004 3005 if (!dev->ethtool_ops->get_fecparam) 3006 return -EOPNOTSUPP; 3007 3008 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 3009 if (rc) 3010 return rc; 3011 3012 if (WARN_ON_ONCE(fecparam.reserved)) 3013 fecparam.reserved = 0; 3014 3015 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 3016 return -EFAULT; 3017 return 0; 3018 } 3019 3020 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 3021 { 3022 struct ethtool_fecparam fecparam; 3023 3024 if (!dev->ethtool_ops->set_fecparam) 3025 return -EOPNOTSUPP; 3026 3027 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 3028 return -EFAULT; 3029 3030 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) 3031 return -EINVAL; 3032 3033 fecparam.active_fec = 0; 3034 fecparam.reserved = 0; 3035 3036 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 3037 } 3038 3039 /* The main entry point in this file. Called from net/core/dev_ioctl.c */ 3040 3041 static int 3042 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr, 3043 u32 ethcmd, struct ethtool_devlink_compat *devlink_state) 3044 { 3045 struct net_device *dev; 3046 u32 sub_cmd; 3047 int rc; 3048 netdev_features_t old_features; 3049 3050 dev = __dev_get_by_name(net, ifr->ifr_name); 3051 if (!dev) 3052 return -ENODEV; 3053 3054 if (ethcmd == ETHTOOL_PERQUEUE) { 3055 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 3056 return -EFAULT; 3057 } else { 3058 sub_cmd = ethcmd; 3059 } 3060 /* Allow some commands to be done by anyone */ 3061 switch (sub_cmd) { 3062 case ETHTOOL_GSET: 3063 case ETHTOOL_GDRVINFO: 3064 case ETHTOOL_GMSGLVL: 3065 case ETHTOOL_GLINK: 3066 case ETHTOOL_GCOALESCE: 3067 case ETHTOOL_GRINGPARAM: 3068 case ETHTOOL_GPAUSEPARAM: 3069 case ETHTOOL_GRXCSUM: 3070 case ETHTOOL_GTXCSUM: 3071 case ETHTOOL_GSG: 3072 case ETHTOOL_GSSET_INFO: 3073 case ETHTOOL_GSTRINGS: 3074 case ETHTOOL_GSTATS: 3075 case ETHTOOL_GPHYSTATS: 3076 case ETHTOOL_GTSO: 3077 case ETHTOOL_GPERMADDR: 3078 case ETHTOOL_GUFO: 3079 case ETHTOOL_GGSO: 3080 case ETHTOOL_GGRO: 3081 case ETHTOOL_GFLAGS: 3082 case ETHTOOL_GPFLAGS: 3083 case ETHTOOL_GRXFH: 3084 case ETHTOOL_GRXRINGS: 3085 case ETHTOOL_GRXCLSRLCNT: 3086 case ETHTOOL_GRXCLSRULE: 3087 case ETHTOOL_GRXCLSRLALL: 3088 case ETHTOOL_GRXFHINDIR: 3089 case ETHTOOL_GRSSH: 3090 case ETHTOOL_GFEATURES: 3091 case ETHTOOL_GCHANNELS: 3092 case ETHTOOL_GET_TS_INFO: 3093 case ETHTOOL_GEEE: 3094 case ETHTOOL_GTUNABLE: 3095 case ETHTOOL_PHY_GTUNABLE: 3096 case ETHTOOL_GLINKSETTINGS: 3097 case ETHTOOL_GFECPARAM: 3098 break; 3099 default: 3100 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3101 return -EPERM; 3102 } 3103 3104 if (dev->dev.parent) 3105 pm_runtime_get_sync(dev->dev.parent); 3106 3107 if (!netif_device_present(dev)) { 3108 rc = -ENODEV; 3109 goto out; 3110 } 3111 3112 if (dev->ethtool_ops->begin) { 3113 rc = dev->ethtool_ops->begin(dev); 3114 if (rc < 0) 3115 goto out; 3116 } 3117 old_features = dev->features; 3118 3119 switch (ethcmd) { 3120 case ETHTOOL_GSET: 3121 rc = ethtool_get_settings(dev, useraddr); 3122 break; 3123 case ETHTOOL_SSET: 3124 rc = ethtool_set_settings(dev, useraddr); 3125 break; 3126 case ETHTOOL_GDRVINFO: 3127 rc = ethtool_get_drvinfo(dev, devlink_state); 3128 break; 3129 case ETHTOOL_GREGS: 3130 rc = ethtool_get_regs(dev, useraddr); 3131 break; 3132 case ETHTOOL_GWOL: 3133 rc = ethtool_get_wol(dev, useraddr); 3134 break; 3135 case ETHTOOL_SWOL: 3136 rc = ethtool_set_wol(dev, useraddr); 3137 break; 3138 case ETHTOOL_GMSGLVL: 3139 rc = ethtool_get_value(dev, useraddr, ethcmd, 3140 dev->ethtool_ops->get_msglevel); 3141 break; 3142 case ETHTOOL_SMSGLVL: 3143 rc = ethtool_set_value_void(dev, useraddr, 3144 dev->ethtool_ops->set_msglevel); 3145 if (!rc) 3146 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL); 3147 break; 3148 case ETHTOOL_GEEE: 3149 rc = ethtool_get_eee(dev, useraddr); 3150 break; 3151 case ETHTOOL_SEEE: 3152 rc = ethtool_set_eee(dev, useraddr); 3153 break; 3154 case ETHTOOL_NWAY_RST: 3155 rc = ethtool_nway_reset(dev); 3156 break; 3157 case ETHTOOL_GLINK: 3158 rc = ethtool_get_link(dev, useraddr); 3159 break; 3160 case ETHTOOL_GEEPROM: 3161 rc = ethtool_get_eeprom(dev, useraddr); 3162 break; 3163 case ETHTOOL_SEEPROM: 3164 rc = ethtool_set_eeprom(dev, useraddr); 3165 break; 3166 case ETHTOOL_GCOALESCE: 3167 rc = ethtool_get_coalesce(dev, useraddr); 3168 break; 3169 case ETHTOOL_SCOALESCE: 3170 rc = ethtool_set_coalesce(dev, useraddr); 3171 break; 3172 case ETHTOOL_GRINGPARAM: 3173 rc = ethtool_get_ringparam(dev, useraddr); 3174 break; 3175 case ETHTOOL_SRINGPARAM: 3176 rc = ethtool_set_ringparam(dev, useraddr); 3177 break; 3178 case ETHTOOL_GPAUSEPARAM: 3179 rc = ethtool_get_pauseparam(dev, useraddr); 3180 break; 3181 case ETHTOOL_SPAUSEPARAM: 3182 rc = ethtool_set_pauseparam(dev, useraddr); 3183 break; 3184 case ETHTOOL_TEST: 3185 rc = ethtool_self_test(dev, useraddr); 3186 break; 3187 case ETHTOOL_GSTRINGS: 3188 rc = ethtool_get_strings(dev, useraddr); 3189 break; 3190 case ETHTOOL_PHYS_ID: 3191 rc = ethtool_phys_id(dev, useraddr); 3192 break; 3193 case ETHTOOL_GSTATS: 3194 rc = ethtool_get_stats(dev, useraddr); 3195 break; 3196 case ETHTOOL_GPERMADDR: 3197 rc = ethtool_get_perm_addr(dev, useraddr); 3198 break; 3199 case ETHTOOL_GFLAGS: 3200 rc = ethtool_get_value(dev, useraddr, ethcmd, 3201 __ethtool_get_flags); 3202 break; 3203 case ETHTOOL_SFLAGS: 3204 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 3205 break; 3206 case ETHTOOL_GPFLAGS: 3207 rc = ethtool_get_value(dev, useraddr, ethcmd, 3208 dev->ethtool_ops->get_priv_flags); 3209 if (!rc) 3210 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL); 3211 break; 3212 case ETHTOOL_SPFLAGS: 3213 rc = ethtool_set_value(dev, useraddr, 3214 dev->ethtool_ops->set_priv_flags); 3215 break; 3216 case ETHTOOL_GRXFH: 3217 case ETHTOOL_GRXRINGS: 3218 case ETHTOOL_GRXCLSRLCNT: 3219 case ETHTOOL_GRXCLSRULE: 3220 case ETHTOOL_GRXCLSRLALL: 3221 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 3222 break; 3223 case ETHTOOL_SRXFH: 3224 case ETHTOOL_SRXCLSRLDEL: 3225 case ETHTOOL_SRXCLSRLINS: 3226 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 3227 break; 3228 case ETHTOOL_FLASHDEV: 3229 rc = ethtool_flash_device(dev, devlink_state); 3230 break; 3231 case ETHTOOL_RESET: 3232 rc = ethtool_reset(dev, useraddr); 3233 break; 3234 case ETHTOOL_GSSET_INFO: 3235 rc = ethtool_get_sset_info(dev, useraddr); 3236 break; 3237 case ETHTOOL_GRXFHINDIR: 3238 rc = ethtool_get_rxfh_indir(dev, useraddr); 3239 break; 3240 case ETHTOOL_SRXFHINDIR: 3241 rc = ethtool_set_rxfh_indir(dev, useraddr); 3242 break; 3243 case ETHTOOL_GRSSH: 3244 rc = ethtool_get_rxfh(dev, useraddr); 3245 break; 3246 case ETHTOOL_SRSSH: 3247 rc = ethtool_set_rxfh(dev, useraddr); 3248 break; 3249 case ETHTOOL_GFEATURES: 3250 rc = ethtool_get_features(dev, useraddr); 3251 break; 3252 case ETHTOOL_SFEATURES: 3253 rc = ethtool_set_features(dev, useraddr); 3254 break; 3255 case ETHTOOL_GTXCSUM: 3256 case ETHTOOL_GRXCSUM: 3257 case ETHTOOL_GSG: 3258 case ETHTOOL_GTSO: 3259 case ETHTOOL_GGSO: 3260 case ETHTOOL_GGRO: 3261 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 3262 break; 3263 case ETHTOOL_STXCSUM: 3264 case ETHTOOL_SRXCSUM: 3265 case ETHTOOL_SSG: 3266 case ETHTOOL_STSO: 3267 case ETHTOOL_SGSO: 3268 case ETHTOOL_SGRO: 3269 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 3270 break; 3271 case ETHTOOL_GCHANNELS: 3272 rc = ethtool_get_channels(dev, useraddr); 3273 break; 3274 case ETHTOOL_SCHANNELS: 3275 rc = ethtool_set_channels(dev, useraddr); 3276 break; 3277 case ETHTOOL_SET_DUMP: 3278 rc = ethtool_set_dump(dev, useraddr); 3279 break; 3280 case ETHTOOL_GET_DUMP_FLAG: 3281 rc = ethtool_get_dump_flag(dev, useraddr); 3282 break; 3283 case ETHTOOL_GET_DUMP_DATA: 3284 rc = ethtool_get_dump_data(dev, useraddr); 3285 break; 3286 case ETHTOOL_GET_TS_INFO: 3287 rc = ethtool_get_ts_info(dev, useraddr); 3288 break; 3289 case ETHTOOL_GMODULEINFO: 3290 rc = ethtool_get_module_info(dev, useraddr); 3291 break; 3292 case ETHTOOL_GMODULEEEPROM: 3293 rc = ethtool_get_module_eeprom(dev, useraddr); 3294 break; 3295 case ETHTOOL_GTUNABLE: 3296 rc = ethtool_get_tunable(dev, useraddr); 3297 break; 3298 case ETHTOOL_STUNABLE: 3299 rc = ethtool_set_tunable(dev, useraddr); 3300 break; 3301 case ETHTOOL_GPHYSTATS: 3302 rc = ethtool_get_phy_stats(dev, useraddr); 3303 break; 3304 case ETHTOOL_PERQUEUE: 3305 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); 3306 break; 3307 case ETHTOOL_GLINKSETTINGS: 3308 rc = ethtool_get_link_ksettings(dev, useraddr); 3309 break; 3310 case ETHTOOL_SLINKSETTINGS: 3311 rc = ethtool_set_link_ksettings(dev, useraddr); 3312 break; 3313 case ETHTOOL_PHY_GTUNABLE: 3314 rc = get_phy_tunable(dev, useraddr); 3315 break; 3316 case ETHTOOL_PHY_STUNABLE: 3317 rc = set_phy_tunable(dev, useraddr); 3318 break; 3319 case ETHTOOL_GFECPARAM: 3320 rc = ethtool_get_fecparam(dev, useraddr); 3321 break; 3322 case ETHTOOL_SFECPARAM: 3323 rc = ethtool_set_fecparam(dev, useraddr); 3324 break; 3325 default: 3326 rc = -EOPNOTSUPP; 3327 } 3328 3329 if (dev->ethtool_ops->complete) 3330 dev->ethtool_ops->complete(dev); 3331 3332 if (old_features != dev->features) 3333 netdev_features_change(dev); 3334 out: 3335 if (dev->dev.parent) 3336 pm_runtime_put(dev->dev.parent); 3337 3338 return rc; 3339 } 3340 3341 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) 3342 { 3343 struct ethtool_devlink_compat *state; 3344 u32 ethcmd; 3345 int rc; 3346 3347 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 3348 return -EFAULT; 3349 3350 state = kzalloc(sizeof(*state), GFP_KERNEL); 3351 if (!state) 3352 return -ENOMEM; 3353 3354 switch (ethcmd) { 3355 case ETHTOOL_FLASHDEV: 3356 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) { 3357 rc = -EFAULT; 3358 goto exit_free; 3359 } 3360 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 3361 break; 3362 } 3363 3364 rtnl_lock(); 3365 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state); 3366 rtnl_unlock(); 3367 if (rc) 3368 goto exit_free; 3369 3370 switch (ethcmd) { 3371 case ETHTOOL_FLASHDEV: 3372 if (state->devlink) 3373 rc = devlink_compat_flash_update(state->devlink, 3374 state->efl.data); 3375 break; 3376 case ETHTOOL_GDRVINFO: 3377 if (state->devlink) 3378 devlink_compat_running_version(state->devlink, 3379 state->info.fw_version, 3380 sizeof(state->info.fw_version)); 3381 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) { 3382 rc = -EFAULT; 3383 goto exit_free; 3384 } 3385 break; 3386 } 3387 3388 exit_free: 3389 if (state->devlink) 3390 devlink_put(state->devlink); 3391 kfree(state); 3392 return rc; 3393 } 3394 3395 struct ethtool_rx_flow_key { 3396 struct flow_dissector_key_basic basic; 3397 union { 3398 struct flow_dissector_key_ipv4_addrs ipv4; 3399 struct flow_dissector_key_ipv6_addrs ipv6; 3400 }; 3401 struct flow_dissector_key_ports tp; 3402 struct flow_dissector_key_ip ip; 3403 struct flow_dissector_key_vlan vlan; 3404 struct flow_dissector_key_eth_addrs eth_addrs; 3405 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 3406 3407 struct ethtool_rx_flow_match { 3408 struct flow_dissector dissector; 3409 struct ethtool_rx_flow_key key; 3410 struct ethtool_rx_flow_key mask; 3411 }; 3412 3413 struct ethtool_rx_flow_rule * 3414 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) 3415 { 3416 const struct ethtool_rx_flow_spec *fs = input->fs; 3417 struct ethtool_rx_flow_match *match; 3418 struct ethtool_rx_flow_rule *flow; 3419 struct flow_action_entry *act; 3420 3421 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + 3422 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); 3423 if (!flow) 3424 return ERR_PTR(-ENOMEM); 3425 3426 /* ethtool_rx supports only one single action per rule. */ 3427 flow->rule = flow_rule_alloc(1); 3428 if (!flow->rule) { 3429 kfree(flow); 3430 return ERR_PTR(-ENOMEM); 3431 } 3432 3433 match = (struct ethtool_rx_flow_match *)flow->priv; 3434 flow->rule->match.dissector = &match->dissector; 3435 flow->rule->match.mask = &match->mask; 3436 flow->rule->match.key = &match->key; 3437 3438 match->mask.basic.n_proto = htons(0xffff); 3439 3440 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3441 case ETHER_FLOW: { 3442 const struct ethhdr *ether_spec, *ether_m_spec; 3443 3444 ether_spec = &fs->h_u.ether_spec; 3445 ether_m_spec = &fs->m_u.ether_spec; 3446 3447 if (!is_zero_ether_addr(ether_m_spec->h_source)) { 3448 ether_addr_copy(match->key.eth_addrs.src, 3449 ether_spec->h_source); 3450 ether_addr_copy(match->mask.eth_addrs.src, 3451 ether_m_spec->h_source); 3452 } 3453 if (!is_zero_ether_addr(ether_m_spec->h_dest)) { 3454 ether_addr_copy(match->key.eth_addrs.dst, 3455 ether_spec->h_dest); 3456 ether_addr_copy(match->mask.eth_addrs.dst, 3457 ether_m_spec->h_dest); 3458 } 3459 if (ether_m_spec->h_proto) { 3460 match->key.basic.n_proto = ether_spec->h_proto; 3461 match->mask.basic.n_proto = ether_m_spec->h_proto; 3462 } 3463 } 3464 break; 3465 case TCP_V4_FLOW: 3466 case UDP_V4_FLOW: { 3467 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; 3468 3469 match->key.basic.n_proto = htons(ETH_P_IP); 3470 3471 v4_spec = &fs->h_u.tcp_ip4_spec; 3472 v4_m_spec = &fs->m_u.tcp_ip4_spec; 3473 3474 if (v4_m_spec->ip4src) { 3475 match->key.ipv4.src = v4_spec->ip4src; 3476 match->mask.ipv4.src = v4_m_spec->ip4src; 3477 } 3478 if (v4_m_spec->ip4dst) { 3479 match->key.ipv4.dst = v4_spec->ip4dst; 3480 match->mask.ipv4.dst = v4_m_spec->ip4dst; 3481 } 3482 if (v4_m_spec->ip4src || 3483 v4_m_spec->ip4dst) { 3484 match->dissector.used_keys |= 3485 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS); 3486 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 3487 offsetof(struct ethtool_rx_flow_key, ipv4); 3488 } 3489 if (v4_m_spec->psrc) { 3490 match->key.tp.src = v4_spec->psrc; 3491 match->mask.tp.src = v4_m_spec->psrc; 3492 } 3493 if (v4_m_spec->pdst) { 3494 match->key.tp.dst = v4_spec->pdst; 3495 match->mask.tp.dst = v4_m_spec->pdst; 3496 } 3497 if (v4_m_spec->psrc || 3498 v4_m_spec->pdst) { 3499 match->dissector.used_keys |= 3500 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3501 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3502 offsetof(struct ethtool_rx_flow_key, tp); 3503 } 3504 if (v4_m_spec->tos) { 3505 match->key.ip.tos = v4_spec->tos; 3506 match->mask.ip.tos = v4_m_spec->tos; 3507 match->dissector.used_keys |= 3508 BIT(FLOW_DISSECTOR_KEY_IP); 3509 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3510 offsetof(struct ethtool_rx_flow_key, ip); 3511 } 3512 } 3513 break; 3514 case TCP_V6_FLOW: 3515 case UDP_V6_FLOW: { 3516 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; 3517 3518 match->key.basic.n_proto = htons(ETH_P_IPV6); 3519 3520 v6_spec = &fs->h_u.tcp_ip6_spec; 3521 v6_m_spec = &fs->m_u.tcp_ip6_spec; 3522 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) { 3523 memcpy(&match->key.ipv6.src, v6_spec->ip6src, 3524 sizeof(match->key.ipv6.src)); 3525 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, 3526 sizeof(match->mask.ipv6.src)); 3527 } 3528 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3529 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, 3530 sizeof(match->key.ipv6.dst)); 3531 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, 3532 sizeof(match->mask.ipv6.dst)); 3533 } 3534 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) || 3535 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3536 match->dissector.used_keys |= 3537 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS); 3538 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = 3539 offsetof(struct ethtool_rx_flow_key, ipv6); 3540 } 3541 if (v6_m_spec->psrc) { 3542 match->key.tp.src = v6_spec->psrc; 3543 match->mask.tp.src = v6_m_spec->psrc; 3544 } 3545 if (v6_m_spec->pdst) { 3546 match->key.tp.dst = v6_spec->pdst; 3547 match->mask.tp.dst = v6_m_spec->pdst; 3548 } 3549 if (v6_m_spec->psrc || 3550 v6_m_spec->pdst) { 3551 match->dissector.used_keys |= 3552 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3553 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3554 offsetof(struct ethtool_rx_flow_key, tp); 3555 } 3556 if (v6_m_spec->tclass) { 3557 match->key.ip.tos = v6_spec->tclass; 3558 match->mask.ip.tos = v6_m_spec->tclass; 3559 match->dissector.used_keys |= 3560 BIT_ULL(FLOW_DISSECTOR_KEY_IP); 3561 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3562 offsetof(struct ethtool_rx_flow_key, ip); 3563 } 3564 } 3565 break; 3566 default: 3567 ethtool_rx_flow_rule_destroy(flow); 3568 return ERR_PTR(-EINVAL); 3569 } 3570 3571 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3572 case TCP_V4_FLOW: 3573 case TCP_V6_FLOW: 3574 match->key.basic.ip_proto = IPPROTO_TCP; 3575 match->mask.basic.ip_proto = 0xff; 3576 break; 3577 case UDP_V4_FLOW: 3578 case UDP_V6_FLOW: 3579 match->key.basic.ip_proto = IPPROTO_UDP; 3580 match->mask.basic.ip_proto = 0xff; 3581 break; 3582 } 3583 3584 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC); 3585 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = 3586 offsetof(struct ethtool_rx_flow_key, basic); 3587 3588 if (fs->flow_type & FLOW_EXT) { 3589 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3590 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3591 3592 if (ext_m_spec->vlan_etype) { 3593 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; 3594 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; 3595 } 3596 3597 if (ext_m_spec->vlan_tci) { 3598 match->key.vlan.vlan_id = 3599 ntohs(ext_h_spec->vlan_tci) & 0x0fff; 3600 match->mask.vlan.vlan_id = 3601 ntohs(ext_m_spec->vlan_tci) & 0x0fff; 3602 3603 match->key.vlan.vlan_dei = 3604 !!(ext_h_spec->vlan_tci & htons(0x1000)); 3605 match->mask.vlan.vlan_dei = 3606 !!(ext_m_spec->vlan_tci & htons(0x1000)); 3607 3608 match->key.vlan.vlan_priority = 3609 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; 3610 match->mask.vlan.vlan_priority = 3611 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; 3612 } 3613 3614 if (ext_m_spec->vlan_etype || 3615 ext_m_spec->vlan_tci) { 3616 match->dissector.used_keys |= 3617 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN); 3618 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = 3619 offsetof(struct ethtool_rx_flow_key, vlan); 3620 } 3621 } 3622 if (fs->flow_type & FLOW_MAC_EXT) { 3623 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3624 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3625 3626 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, 3627 ETH_ALEN); 3628 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, 3629 ETH_ALEN); 3630 3631 match->dissector.used_keys |= 3632 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS); 3633 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = 3634 offsetof(struct ethtool_rx_flow_key, eth_addrs); 3635 } 3636 3637 act = &flow->rule->action.entries[0]; 3638 switch (fs->ring_cookie) { 3639 case RX_CLS_FLOW_DISC: 3640 act->id = FLOW_ACTION_DROP; 3641 break; 3642 case RX_CLS_FLOW_WAKE: 3643 act->id = FLOW_ACTION_WAKE; 3644 break; 3645 default: 3646 act->id = FLOW_ACTION_QUEUE; 3647 if (fs->flow_type & FLOW_RSS) 3648 act->queue.ctx = input->rss_ctx; 3649 3650 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); 3651 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); 3652 break; 3653 } 3654 3655 return flow; 3656 } 3657 EXPORT_SYMBOL(ethtool_rx_flow_rule_create); 3658 3659 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) 3660 { 3661 kfree(flow->rule); 3662 kfree(flow); 3663 } 3664 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); 3665