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