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