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