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