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