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 ASSERT_RTNL(); 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 ASSERT_RTNL(); 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 ASSERT_RTNL(); 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 ASSERT_RTNL(); 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 { 2457 struct ethtool_value id; 2458 static bool busy; 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 (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 RTNL lock while waiting, but prevent reentry or 2477 * removal of the device. 2478 */ 2479 busy = true; 2480 netdev_hold(dev, &dev_tracker, GFP_KERNEL); 2481 netdev_unlock_ops(dev); 2482 rtnl_unlock(); 2483 2484 if (rc == 0) { 2485 /* Driver will handle this itself */ 2486 schedule_timeout_interruptible( 2487 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 2488 } else { 2489 /* Driver expects to be called at twice the frequency in rc */ 2490 int n = rc * 2, interval = HZ / n; 2491 u64 count = mul_u32_u32(n, id.data); 2492 u64 i = 0; 2493 2494 do { 2495 rtnl_lock(); 2496 netdev_lock_ops(dev); 2497 rc = ops->set_phys_id(dev, 2498 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 2499 netdev_unlock_ops(dev); 2500 rtnl_unlock(); 2501 if (rc) 2502 break; 2503 schedule_timeout_interruptible(interval); 2504 } while (!signal_pending(current) && (!id.data || i < count)); 2505 } 2506 2507 rtnl_lock(); 2508 netdev_lock_ops(dev); 2509 netdev_put(dev, &dev_tracker); 2510 busy = false; 2511 2512 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 2513 return rc; 2514 } 2515 2516 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 2517 { 2518 struct ethtool_stats stats; 2519 const struct ethtool_ops *ops = dev->ethtool_ops; 2520 u64 *data; 2521 int ret, n_stats; 2522 2523 if (!ops->get_ethtool_stats || !ops->get_sset_count) 2524 return -EOPNOTSUPP; 2525 2526 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 2527 if (n_stats < 0) 2528 return n_stats; 2529 if (n_stats > S32_MAX / sizeof(u64)) 2530 return -ENOMEM; 2531 WARN_ON_ONCE(!n_stats); 2532 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2533 return -EFAULT; 2534 2535 if (stats.n_stats && stats.n_stats != n_stats) 2536 stats.n_stats = 0; 2537 else 2538 stats.n_stats = n_stats; 2539 2540 if (stats.n_stats) { 2541 data = vzalloc(array_size(stats.n_stats, sizeof(u64))); 2542 if (!data) 2543 return -ENOMEM; 2544 ops->get_ethtool_stats(dev, &stats, data); 2545 } else { 2546 data = NULL; 2547 } 2548 2549 ret = -EFAULT; 2550 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2551 goto out; 2552 useraddr += sizeof(stats); 2553 if (stats.n_stats && 2554 copy_to_user(useraddr, data, 2555 array_size(stats.n_stats, sizeof(u64)))) 2556 goto out; 2557 ret = 0; 2558 2559 out: 2560 vfree(data); 2561 return ret; 2562 } 2563 2564 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data) 2565 { 2566 if (n_stats < 0) 2567 return n_stats; 2568 if (n_stats > S32_MAX / sizeof(u64)) 2569 return -ENOMEM; 2570 if (WARN_ON_ONCE(!n_stats)) 2571 return -EOPNOTSUPP; 2572 2573 *data = vzalloc(array_size(n_stats, sizeof(u64))); 2574 if (!*data) 2575 return -ENOMEM; 2576 2577 return 0; 2578 } 2579 2580 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev, 2581 struct ethtool_stats *stats, 2582 u64 **data) 2583 { 2584 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 2585 int n_stats, ret; 2586 2587 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats) 2588 return -EOPNOTSUPP; 2589 2590 n_stats = phy_ops->get_sset_count(phydev); 2591 if (stats->n_stats && stats->n_stats != n_stats) { 2592 stats->n_stats = 0; 2593 return 0; 2594 } 2595 2596 ret = ethtool_vzalloc_stats_array(n_stats, data); 2597 if (ret) 2598 return ret; 2599 2600 stats->n_stats = n_stats; 2601 return phy_ops->get_stats(phydev, stats, *data); 2602 } 2603 2604 static int ethtool_get_phy_stats_ethtool(struct net_device *dev, 2605 struct ethtool_stats *stats, 2606 u64 **data) 2607 { 2608 const struct ethtool_ops *ops = dev->ethtool_ops; 2609 int n_stats, ret; 2610 2611 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats) 2612 return -EOPNOTSUPP; 2613 2614 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 2615 if (stats->n_stats && stats->n_stats != n_stats) { 2616 stats->n_stats = 0; 2617 return 0; 2618 } 2619 2620 ret = ethtool_vzalloc_stats_array(n_stats, data); 2621 if (ret) 2622 return ret; 2623 2624 stats->n_stats = n_stats; 2625 ops->get_ethtool_phy_stats(dev, stats, *data); 2626 2627 return 0; 2628 } 2629 2630 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 2631 { 2632 struct phy_device *phydev = dev->phydev; 2633 struct ethtool_stats stats; 2634 u64 *data = NULL; 2635 int ret = -EOPNOTSUPP; 2636 2637 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2638 return -EFAULT; 2639 2640 if (phydev) 2641 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data); 2642 2643 if (ret == -EOPNOTSUPP) 2644 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data); 2645 2646 if (ret) 2647 goto out; 2648 2649 if (copy_to_user(useraddr, &stats, sizeof(stats))) { 2650 ret = -EFAULT; 2651 goto out; 2652 } 2653 2654 useraddr += sizeof(stats); 2655 if (stats.n_stats && 2656 copy_to_user(useraddr, data, 2657 array_size(stats.n_stats, sizeof(u64)))) 2658 ret = -EFAULT; 2659 2660 out: 2661 vfree(data); 2662 return ret; 2663 } 2664 2665 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2666 { 2667 struct ethtool_perm_addr epaddr; 2668 2669 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2670 return -EFAULT; 2671 2672 if (epaddr.size < dev->addr_len) 2673 return -ETOOSMALL; 2674 epaddr.size = dev->addr_len; 2675 2676 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2677 return -EFAULT; 2678 useraddr += sizeof(epaddr); 2679 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2680 return -EFAULT; 2681 return 0; 2682 } 2683 2684 static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2685 u32 cmd, u32 (*actor)(struct net_device *)) 2686 { 2687 struct ethtool_value edata = { .cmd = cmd }; 2688 2689 if (!actor) 2690 return -EOPNOTSUPP; 2691 2692 edata.data = actor(dev); 2693 2694 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2695 return -EFAULT; 2696 return 0; 2697 } 2698 2699 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2700 void (*actor)(struct net_device *, u32)) 2701 { 2702 struct ethtool_value edata; 2703 2704 if (!actor) 2705 return -EOPNOTSUPP; 2706 2707 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2708 return -EFAULT; 2709 2710 actor(dev, edata.data); 2711 return 0; 2712 } 2713 2714 static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2715 int (*actor)(struct net_device *, u32)) 2716 { 2717 struct ethtool_value edata; 2718 2719 if (!actor) 2720 return -EOPNOTSUPP; 2721 2722 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2723 return -EFAULT; 2724 2725 return actor(dev, edata.data); 2726 } 2727 2728 static int 2729 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req) 2730 { 2731 if (!dev->ethtool_ops->flash_device) { 2732 req->devlink = netdev_to_devlink_get(dev); 2733 return 0; 2734 } 2735 2736 return dev->ethtool_ops->flash_device(dev, &req->efl); 2737 } 2738 2739 static int ethtool_set_dump(struct net_device *dev, 2740 void __user *useraddr) 2741 { 2742 struct ethtool_dump dump; 2743 2744 if (!dev->ethtool_ops->set_dump) 2745 return -EOPNOTSUPP; 2746 2747 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2748 return -EFAULT; 2749 2750 return dev->ethtool_ops->set_dump(dev, &dump); 2751 } 2752 2753 static int ethtool_get_dump_flag(struct net_device *dev, 2754 void __user *useraddr) 2755 { 2756 int ret; 2757 struct ethtool_dump dump; 2758 const struct ethtool_ops *ops = dev->ethtool_ops; 2759 2760 if (!ops->get_dump_flag) 2761 return -EOPNOTSUPP; 2762 2763 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2764 return -EFAULT; 2765 2766 ret = ops->get_dump_flag(dev, &dump); 2767 if (ret) 2768 return ret; 2769 2770 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2771 return -EFAULT; 2772 return 0; 2773 } 2774 2775 static int ethtool_get_dump_data(struct net_device *dev, 2776 void __user *useraddr) 2777 { 2778 int ret; 2779 __u32 len; 2780 struct ethtool_dump dump, tmp; 2781 const struct ethtool_ops *ops = dev->ethtool_ops; 2782 void *data = NULL; 2783 2784 if (!ops->get_dump_data || !ops->get_dump_flag) 2785 return -EOPNOTSUPP; 2786 2787 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2788 return -EFAULT; 2789 2790 memset(&tmp, 0, sizeof(tmp)); 2791 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2792 ret = ops->get_dump_flag(dev, &tmp); 2793 if (ret) 2794 return ret; 2795 2796 len = min(tmp.len, dump.len); 2797 if (!len) 2798 return -EFAULT; 2799 2800 /* Don't ever let the driver think there's more space available 2801 * than it requested with .get_dump_flag(). 2802 */ 2803 dump.len = len; 2804 2805 /* Always allocate enough space to hold the whole thing so that the 2806 * driver does not need to check the length and bother with partial 2807 * dumping. 2808 */ 2809 data = vzalloc(tmp.len); 2810 if (!data) 2811 return -ENOMEM; 2812 ret = ops->get_dump_data(dev, &dump, data); 2813 if (ret) 2814 goto out; 2815 2816 /* There are two sane possibilities: 2817 * 1. The driver's .get_dump_data() does not touch dump.len. 2818 * 2. Or it may set dump.len to how much it really writes, which 2819 * should be tmp.len (or len if it can do a partial dump). 2820 * In any case respond to userspace with the actual length of data 2821 * it's receiving. 2822 */ 2823 WARN_ON(dump.len != len && dump.len != tmp.len); 2824 dump.len = len; 2825 2826 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2827 ret = -EFAULT; 2828 goto out; 2829 } 2830 useraddr += offsetof(struct ethtool_dump, data); 2831 if (copy_to_user(useraddr, data, len)) 2832 ret = -EFAULT; 2833 out: 2834 vfree(data); 2835 return ret; 2836 } 2837 2838 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2839 { 2840 struct kernel_ethtool_ts_info kernel_info; 2841 struct ethtool_ts_info info = {}; 2842 int err; 2843 2844 err = __ethtool_get_ts_info(dev, &kernel_info); 2845 if (err) 2846 return err; 2847 2848 info.cmd = kernel_info.cmd; 2849 info.so_timestamping = kernel_info.so_timestamping; 2850 info.phc_index = kernel_info.phc_index; 2851 info.tx_types = kernel_info.tx_types; 2852 info.rx_filters = kernel_info.rx_filters; 2853 2854 if (copy_to_user(useraddr, &info, sizeof(info))) 2855 return -EFAULT; 2856 2857 return 0; 2858 } 2859 2860 int ethtool_get_module_info_call(struct net_device *dev, 2861 struct ethtool_modinfo *modinfo) 2862 { 2863 const struct ethtool_ops *ops = dev->ethtool_ops; 2864 struct phy_device *phydev = dev->phydev; 2865 2866 if (dev->ethtool->module_fw_flash_in_progress) 2867 return -EBUSY; 2868 2869 if (dev->sfp_bus) 2870 return sfp_get_module_info(dev->sfp_bus, modinfo); 2871 2872 if (phydev && phydev->drv && phydev->drv->module_info) 2873 return phydev->drv->module_info(phydev, modinfo); 2874 2875 if (ops->get_module_info) 2876 return ops->get_module_info(dev, modinfo); 2877 2878 return -EOPNOTSUPP; 2879 } 2880 2881 static int ethtool_get_module_info(struct net_device *dev, 2882 void __user *useraddr) 2883 { 2884 int ret; 2885 struct ethtool_modinfo modinfo; 2886 2887 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2888 return -EFAULT; 2889 2890 ret = ethtool_get_module_info_call(dev, &modinfo); 2891 if (ret) 2892 return ret; 2893 2894 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2895 return -EFAULT; 2896 2897 return 0; 2898 } 2899 2900 int ethtool_get_module_eeprom_call(struct net_device *dev, 2901 struct ethtool_eeprom *ee, u8 *data) 2902 { 2903 const struct ethtool_ops *ops = dev->ethtool_ops; 2904 struct phy_device *phydev = dev->phydev; 2905 2906 if (dev->ethtool->module_fw_flash_in_progress) 2907 return -EBUSY; 2908 2909 if (dev->sfp_bus) 2910 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2911 2912 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2913 return phydev->drv->module_eeprom(phydev, ee, data); 2914 2915 if (ops->get_module_eeprom) 2916 return ops->get_module_eeprom(dev, ee, data); 2917 2918 return -EOPNOTSUPP; 2919 } 2920 2921 static int ethtool_get_module_eeprom(struct net_device *dev, 2922 void __user *useraddr) 2923 { 2924 int ret; 2925 struct ethtool_modinfo modinfo; 2926 2927 ret = ethtool_get_module_info_call(dev, &modinfo); 2928 if (ret) 2929 return ret; 2930 2931 return ethtool_get_any_eeprom(dev, useraddr, 2932 ethtool_get_module_eeprom_call, 2933 modinfo.eeprom_len); 2934 } 2935 2936 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2937 { 2938 switch (tuna->id) { 2939 case ETHTOOL_RX_COPYBREAK: 2940 case ETHTOOL_TX_COPYBREAK: 2941 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 2942 if (tuna->len != sizeof(u32) || 2943 tuna->type_id != ETHTOOL_TUNABLE_U32) 2944 return -EINVAL; 2945 break; 2946 case ETHTOOL_PFC_PREVENTION_TOUT: 2947 if (tuna->len != sizeof(u16) || 2948 tuna->type_id != ETHTOOL_TUNABLE_U16) 2949 return -EINVAL; 2950 break; 2951 default: 2952 return -EINVAL; 2953 } 2954 2955 return 0; 2956 } 2957 2958 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2959 { 2960 int ret; 2961 struct ethtool_tunable tuna; 2962 const struct ethtool_ops *ops = dev->ethtool_ops; 2963 void *data; 2964 2965 if (!ops->get_tunable) 2966 return -EOPNOTSUPP; 2967 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2968 return -EFAULT; 2969 ret = ethtool_tunable_valid(&tuna); 2970 if (ret) 2971 return ret; 2972 data = kzalloc(tuna.len, GFP_USER); 2973 if (!data) 2974 return -ENOMEM; 2975 ret = ops->get_tunable(dev, &tuna, data); 2976 if (ret) 2977 goto out; 2978 useraddr += sizeof(tuna); 2979 ret = -EFAULT; 2980 if (copy_to_user(useraddr, data, tuna.len)) 2981 goto out; 2982 ret = 0; 2983 2984 out: 2985 kfree(data); 2986 return ret; 2987 } 2988 2989 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2990 { 2991 int ret; 2992 struct ethtool_tunable tuna; 2993 const struct ethtool_ops *ops = dev->ethtool_ops; 2994 void *data; 2995 2996 if (!ops->set_tunable) 2997 return -EOPNOTSUPP; 2998 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2999 return -EFAULT; 3000 ret = ethtool_tunable_valid(&tuna); 3001 if (ret) 3002 return ret; 3003 useraddr += sizeof(tuna); 3004 data = memdup_user(useraddr, tuna.len); 3005 if (IS_ERR(data)) 3006 return PTR_ERR(data); 3007 ret = ops->set_tunable(dev, &tuna, data); 3008 3009 kfree(data); 3010 return ret; 3011 } 3012 3013 static noinline_for_stack int 3014 ethtool_get_per_queue_coalesce(struct net_device *dev, 3015 void __user *useraddr, 3016 struct ethtool_per_queue_op *per_queue_opt) 3017 { 3018 u32 bit; 3019 int ret; 3020 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 3021 3022 if (!dev->ethtool_ops->get_per_queue_coalesce) 3023 return -EOPNOTSUPP; 3024 3025 useraddr += sizeof(*per_queue_opt); 3026 3027 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 3028 MAX_NUM_QUEUE); 3029 3030 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 3031 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 3032 3033 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 3034 if (ret != 0) 3035 return ret; 3036 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 3037 return -EFAULT; 3038 useraddr += sizeof(coalesce); 3039 } 3040 3041 return 0; 3042 } 3043 3044 static noinline_for_stack int 3045 ethtool_set_per_queue_coalesce(struct net_device *dev, 3046 void __user *useraddr, 3047 struct ethtool_per_queue_op *per_queue_opt) 3048 { 3049 u32 bit; 3050 int i, ret = 0; 3051 int n_queue; 3052 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 3053 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 3054 3055 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 3056 (!dev->ethtool_ops->get_per_queue_coalesce)) 3057 return -EOPNOTSUPP; 3058 3059 useraddr += sizeof(*per_queue_opt); 3060 3061 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 3062 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 3063 tmp = backup = kmalloc_objs(*backup, n_queue); 3064 if (!backup) 3065 return -ENOMEM; 3066 3067 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 3068 struct ethtool_coalesce coalesce; 3069 3070 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 3071 if (ret != 0) 3072 goto roll_back; 3073 3074 tmp++; 3075 3076 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 3077 ret = -EFAULT; 3078 goto roll_back; 3079 } 3080 3081 if (!ethtool_set_coalesce_supported(dev, &coalesce)) { 3082 ret = -EOPNOTSUPP; 3083 goto roll_back; 3084 } 3085 3086 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 3087 if (ret != 0) 3088 goto roll_back; 3089 3090 useraddr += sizeof(coalesce); 3091 } 3092 3093 roll_back: 3094 if (ret != 0) { 3095 tmp = backup; 3096 for_each_set_bit(i, queue_mask, bit) { 3097 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 3098 tmp++; 3099 } 3100 } 3101 kfree(backup); 3102 3103 return ret; 3104 } 3105 3106 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, 3107 void __user *useraddr, u32 sub_cmd) 3108 { 3109 struct ethtool_per_queue_op per_queue_opt; 3110 3111 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 3112 return -EFAULT; 3113 3114 if (per_queue_opt.sub_command != sub_cmd) 3115 return -EINVAL; 3116 3117 switch (per_queue_opt.sub_command) { 3118 case ETHTOOL_GCOALESCE: 3119 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 3120 case ETHTOOL_SCOALESCE: 3121 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 3122 default: 3123 return -EOPNOTSUPP; 3124 } 3125 } 3126 3127 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 3128 { 3129 switch (tuna->id) { 3130 case ETHTOOL_PHY_DOWNSHIFT: 3131 case ETHTOOL_PHY_FAST_LINK_DOWN: 3132 if (tuna->len != sizeof(u8) || 3133 tuna->type_id != ETHTOOL_TUNABLE_U8) 3134 return -EINVAL; 3135 break; 3136 case ETHTOOL_PHY_EDPD: 3137 if (tuna->len != sizeof(u16) || 3138 tuna->type_id != ETHTOOL_TUNABLE_U16) 3139 return -EINVAL; 3140 break; 3141 default: 3142 return -EINVAL; 3143 } 3144 3145 return 0; 3146 } 3147 3148 static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 3149 { 3150 struct phy_device *phydev = dev->phydev; 3151 struct ethtool_tunable tuna; 3152 bool phy_drv_tunable; 3153 void *data; 3154 int ret; 3155 3156 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 3157 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) 3158 return -EOPNOTSUPP; 3159 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 3160 return -EFAULT; 3161 ret = ethtool_phy_tunable_valid(&tuna); 3162 if (ret) 3163 return ret; 3164 data = kzalloc(tuna.len, GFP_USER); 3165 if (!data) 3166 return -ENOMEM; 3167 if (phy_drv_tunable) { 3168 mutex_lock(&phydev->lock); 3169 ret = phydev->drv->get_tunable(phydev, &tuna, data); 3170 mutex_unlock(&phydev->lock); 3171 } else { 3172 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); 3173 } 3174 if (ret) 3175 goto out; 3176 useraddr += sizeof(tuna); 3177 ret = -EFAULT; 3178 if (copy_to_user(useraddr, data, tuna.len)) 3179 goto out; 3180 ret = 0; 3181 3182 out: 3183 kfree(data); 3184 return ret; 3185 } 3186 3187 static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 3188 { 3189 struct phy_device *phydev = dev->phydev; 3190 struct ethtool_tunable tuna; 3191 bool phy_drv_tunable; 3192 void *data; 3193 int ret; 3194 3195 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 3196 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) 3197 return -EOPNOTSUPP; 3198 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 3199 return -EFAULT; 3200 ret = ethtool_phy_tunable_valid(&tuna); 3201 if (ret) 3202 return ret; 3203 useraddr += sizeof(tuna); 3204 data = memdup_user(useraddr, tuna.len); 3205 if (IS_ERR(data)) 3206 return PTR_ERR(data); 3207 if (phy_drv_tunable) { 3208 mutex_lock(&phydev->lock); 3209 ret = phydev->drv->set_tunable(phydev, &tuna, data); 3210 mutex_unlock(&phydev->lock); 3211 } else { 3212 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); 3213 } 3214 3215 kfree(data); 3216 return ret; 3217 } 3218 3219 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 3220 { 3221 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; 3222 int rc; 3223 3224 if (!dev->ethtool_ops->get_fecparam) 3225 return -EOPNOTSUPP; 3226 3227 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 3228 if (rc) 3229 return rc; 3230 3231 if (WARN_ON_ONCE(fecparam.reserved)) 3232 fecparam.reserved = 0; 3233 3234 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 3235 return -EFAULT; 3236 return 0; 3237 } 3238 3239 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 3240 { 3241 struct ethtool_fecparam fecparam; 3242 3243 if (!dev->ethtool_ops->set_fecparam) 3244 return -EOPNOTSUPP; 3245 3246 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 3247 return -EFAULT; 3248 3249 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) 3250 return -EINVAL; 3251 3252 fecparam.active_fec = 0; 3253 fecparam.reserved = 0; 3254 3255 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 3256 } 3257 3258 /* The main entry point in this file. Called from net/core/dev_ioctl.c */ 3259 3260 static int 3261 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr, 3262 u32 ethcmd, struct ethtool_devlink_compat *devlink_state) 3263 { 3264 struct net_device *dev; 3265 u32 sub_cmd; 3266 int rc; 3267 netdev_features_t old_features; 3268 3269 dev = __dev_get_by_name(net, ifr->ifr_name); 3270 if (!dev) 3271 return -ENODEV; 3272 3273 if (ethcmd == ETHTOOL_PERQUEUE) { 3274 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 3275 return -EFAULT; 3276 } else { 3277 sub_cmd = ethcmd; 3278 } 3279 /* Allow some commands to be done by anyone */ 3280 switch (sub_cmd) { 3281 case ETHTOOL_GSET: 3282 case ETHTOOL_GDRVINFO: 3283 case ETHTOOL_GMSGLVL: 3284 case ETHTOOL_GLINK: 3285 case ETHTOOL_GCOALESCE: 3286 case ETHTOOL_GRINGPARAM: 3287 case ETHTOOL_GPAUSEPARAM: 3288 case ETHTOOL_GRXCSUM: 3289 case ETHTOOL_GTXCSUM: 3290 case ETHTOOL_GSG: 3291 case ETHTOOL_GSSET_INFO: 3292 case ETHTOOL_GSTRINGS: 3293 case ETHTOOL_GSTATS: 3294 case ETHTOOL_GPHYSTATS: 3295 case ETHTOOL_GTSO: 3296 case ETHTOOL_GPERMADDR: 3297 case ETHTOOL_GUFO: 3298 case ETHTOOL_GGSO: 3299 case ETHTOOL_GGRO: 3300 case ETHTOOL_GFLAGS: 3301 case ETHTOOL_GPFLAGS: 3302 case ETHTOOL_GRXFH: 3303 case ETHTOOL_GRXRINGS: 3304 case ETHTOOL_GRXCLSRLCNT: 3305 case ETHTOOL_GRXCLSRULE: 3306 case ETHTOOL_GRXCLSRLALL: 3307 case ETHTOOL_GRXFHINDIR: 3308 case ETHTOOL_GRSSH: 3309 case ETHTOOL_GFEATURES: 3310 case ETHTOOL_GCHANNELS: 3311 case ETHTOOL_GET_TS_INFO: 3312 case ETHTOOL_GEEE: 3313 case ETHTOOL_GTUNABLE: 3314 case ETHTOOL_PHY_GTUNABLE: 3315 case ETHTOOL_GLINKSETTINGS: 3316 case ETHTOOL_GFECPARAM: 3317 break; 3318 default: 3319 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3320 return -EPERM; 3321 } 3322 3323 netdev_lock_ops(dev); 3324 if (dev->dev.parent) 3325 pm_runtime_get_sync(dev->dev.parent); 3326 3327 if (!netif_device_present(dev)) { 3328 rc = -ENODEV; 3329 goto out; 3330 } 3331 3332 if (dev->ethtool_ops->begin) { 3333 rc = dev->ethtool_ops->begin(dev); 3334 if (rc < 0) 3335 goto out; 3336 } 3337 old_features = dev->features; 3338 3339 switch (ethcmd) { 3340 case ETHTOOL_GSET: 3341 rc = ethtool_get_settings(dev, useraddr); 3342 break; 3343 case ETHTOOL_SSET: 3344 rc = ethtool_set_settings(dev, useraddr); 3345 break; 3346 case ETHTOOL_GDRVINFO: 3347 rc = ethtool_get_drvinfo(dev, devlink_state); 3348 break; 3349 case ETHTOOL_GREGS: 3350 rc = ethtool_get_regs(dev, useraddr); 3351 break; 3352 case ETHTOOL_GWOL: 3353 rc = ethtool_get_wol(dev, useraddr); 3354 break; 3355 case ETHTOOL_SWOL: 3356 rc = ethtool_set_wol(dev, useraddr); 3357 break; 3358 case ETHTOOL_GMSGLVL: 3359 rc = ethtool_get_value(dev, useraddr, ethcmd, 3360 dev->ethtool_ops->get_msglevel); 3361 break; 3362 case ETHTOOL_SMSGLVL: 3363 rc = ethtool_set_value_void(dev, useraddr, 3364 dev->ethtool_ops->set_msglevel); 3365 if (!rc) 3366 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF); 3367 break; 3368 case ETHTOOL_GEEE: 3369 rc = ethtool_get_eee(dev, useraddr); 3370 break; 3371 case ETHTOOL_SEEE: 3372 rc = ethtool_set_eee(dev, useraddr); 3373 break; 3374 case ETHTOOL_NWAY_RST: 3375 rc = ethtool_nway_reset(dev); 3376 break; 3377 case ETHTOOL_GLINK: 3378 rc = ethtool_get_link(dev, useraddr); 3379 break; 3380 case ETHTOOL_GEEPROM: 3381 rc = ethtool_get_eeprom(dev, useraddr); 3382 break; 3383 case ETHTOOL_SEEPROM: 3384 rc = ethtool_set_eeprom(dev, useraddr); 3385 break; 3386 case ETHTOOL_GCOALESCE: 3387 rc = ethtool_get_coalesce(dev, useraddr); 3388 break; 3389 case ETHTOOL_SCOALESCE: 3390 rc = ethtool_set_coalesce(dev, useraddr); 3391 break; 3392 case ETHTOOL_GRINGPARAM: 3393 rc = ethtool_get_ringparam(dev, useraddr); 3394 break; 3395 case ETHTOOL_SRINGPARAM: 3396 rc = ethtool_set_ringparam(dev, useraddr); 3397 break; 3398 case ETHTOOL_GPAUSEPARAM: 3399 rc = ethtool_get_pauseparam(dev, useraddr); 3400 break; 3401 case ETHTOOL_SPAUSEPARAM: 3402 rc = ethtool_set_pauseparam(dev, useraddr); 3403 break; 3404 case ETHTOOL_TEST: 3405 rc = ethtool_self_test(dev, useraddr); 3406 break; 3407 case ETHTOOL_GSTRINGS: 3408 rc = ethtool_get_strings(dev, useraddr); 3409 break; 3410 case ETHTOOL_PHYS_ID: 3411 rc = ethtool_phys_id(dev, useraddr); 3412 break; 3413 case ETHTOOL_GSTATS: 3414 rc = ethtool_get_stats(dev, useraddr); 3415 break; 3416 case ETHTOOL_GPERMADDR: 3417 rc = ethtool_get_perm_addr(dev, useraddr); 3418 break; 3419 case ETHTOOL_GFLAGS: 3420 rc = ethtool_get_value(dev, useraddr, ethcmd, 3421 __ethtool_get_flags); 3422 break; 3423 case ETHTOOL_SFLAGS: 3424 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 3425 break; 3426 case ETHTOOL_GPFLAGS: 3427 rc = ethtool_get_value(dev, useraddr, ethcmd, 3428 dev->ethtool_ops->get_priv_flags); 3429 if (!rc) 3430 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF); 3431 break; 3432 case ETHTOOL_SPFLAGS: 3433 rc = ethtool_set_value(dev, useraddr, 3434 dev->ethtool_ops->set_priv_flags); 3435 break; 3436 case ETHTOOL_GRXFH: 3437 rc = ethtool_get_rxfh_fields(dev, ethcmd, useraddr); 3438 break; 3439 case ETHTOOL_SRXFH: 3440 rc = ethtool_set_rxfh_fields(dev, ethcmd, useraddr); 3441 break; 3442 case ETHTOOL_GRXRINGS: 3443 rc = ethtool_get_rxrings(dev, ethcmd, useraddr); 3444 break; 3445 case ETHTOOL_GRXCLSRLCNT: 3446 case ETHTOOL_GRXCLSRULE: 3447 case ETHTOOL_GRXCLSRLALL: 3448 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 3449 break; 3450 case ETHTOOL_SRXCLSRLDEL: 3451 case ETHTOOL_SRXCLSRLINS: 3452 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 3453 break; 3454 case ETHTOOL_FLASHDEV: 3455 rc = ethtool_flash_device(dev, devlink_state); 3456 break; 3457 case ETHTOOL_RESET: 3458 rc = ethtool_reset(dev, useraddr); 3459 break; 3460 case ETHTOOL_GSSET_INFO: 3461 rc = ethtool_get_sset_info(dev, useraddr); 3462 break; 3463 case ETHTOOL_GRXFHINDIR: 3464 rc = ethtool_get_rxfh_indir(dev, useraddr); 3465 break; 3466 case ETHTOOL_SRXFHINDIR: 3467 rc = ethtool_set_rxfh_indir(dev, useraddr); 3468 break; 3469 case ETHTOOL_GRSSH: 3470 rc = ethtool_get_rxfh(dev, useraddr); 3471 break; 3472 case ETHTOOL_SRSSH: 3473 rc = ethtool_set_rxfh(dev, useraddr); 3474 break; 3475 case ETHTOOL_GFEATURES: 3476 rc = ethtool_get_features(dev, useraddr); 3477 break; 3478 case ETHTOOL_SFEATURES: 3479 rc = ethtool_set_features(dev, useraddr); 3480 break; 3481 case ETHTOOL_GTXCSUM: 3482 case ETHTOOL_GRXCSUM: 3483 case ETHTOOL_GSG: 3484 case ETHTOOL_GTSO: 3485 case ETHTOOL_GGSO: 3486 case ETHTOOL_GGRO: 3487 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 3488 break; 3489 case ETHTOOL_STXCSUM: 3490 case ETHTOOL_SRXCSUM: 3491 case ETHTOOL_SSG: 3492 case ETHTOOL_STSO: 3493 case ETHTOOL_SGSO: 3494 case ETHTOOL_SGRO: 3495 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 3496 break; 3497 case ETHTOOL_GCHANNELS: 3498 rc = ethtool_get_channels(dev, useraddr); 3499 break; 3500 case ETHTOOL_SCHANNELS: 3501 rc = ethtool_set_channels(dev, useraddr); 3502 break; 3503 case ETHTOOL_SET_DUMP: 3504 rc = ethtool_set_dump(dev, useraddr); 3505 break; 3506 case ETHTOOL_GET_DUMP_FLAG: 3507 rc = ethtool_get_dump_flag(dev, useraddr); 3508 break; 3509 case ETHTOOL_GET_DUMP_DATA: 3510 rc = ethtool_get_dump_data(dev, useraddr); 3511 break; 3512 case ETHTOOL_GET_TS_INFO: 3513 rc = ethtool_get_ts_info(dev, useraddr); 3514 break; 3515 case ETHTOOL_GMODULEINFO: 3516 rc = ethtool_get_module_info(dev, useraddr); 3517 break; 3518 case ETHTOOL_GMODULEEEPROM: 3519 rc = ethtool_get_module_eeprom(dev, useraddr); 3520 break; 3521 case ETHTOOL_GTUNABLE: 3522 rc = ethtool_get_tunable(dev, useraddr); 3523 break; 3524 case ETHTOOL_STUNABLE: 3525 rc = ethtool_set_tunable(dev, useraddr); 3526 break; 3527 case ETHTOOL_GPHYSTATS: 3528 rc = ethtool_get_phy_stats(dev, useraddr); 3529 break; 3530 case ETHTOOL_PERQUEUE: 3531 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); 3532 break; 3533 case ETHTOOL_GLINKSETTINGS: 3534 rc = ethtool_get_link_ksettings(dev, useraddr); 3535 break; 3536 case ETHTOOL_SLINKSETTINGS: 3537 rc = ethtool_set_link_ksettings(dev, useraddr); 3538 break; 3539 case ETHTOOL_PHY_GTUNABLE: 3540 rc = get_phy_tunable(dev, useraddr); 3541 break; 3542 case ETHTOOL_PHY_STUNABLE: 3543 rc = set_phy_tunable(dev, useraddr); 3544 break; 3545 case ETHTOOL_GFECPARAM: 3546 rc = ethtool_get_fecparam(dev, useraddr); 3547 break; 3548 case ETHTOOL_SFECPARAM: 3549 rc = ethtool_set_fecparam(dev, useraddr); 3550 break; 3551 default: 3552 rc = -EOPNOTSUPP; 3553 } 3554 3555 if (dev->ethtool_ops->complete) 3556 dev->ethtool_ops->complete(dev); 3557 3558 if (old_features != dev->features) 3559 netdev_features_change(dev); 3560 out: 3561 if (dev->dev.parent) 3562 pm_runtime_put(dev->dev.parent); 3563 netdev_unlock_ops(dev); 3564 3565 return rc; 3566 } 3567 3568 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) 3569 { 3570 struct ethtool_devlink_compat *state; 3571 u32 ethcmd; 3572 int rc; 3573 3574 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 3575 return -EFAULT; 3576 3577 state = kzalloc_obj(*state); 3578 if (!state) 3579 return -ENOMEM; 3580 3581 switch (ethcmd) { 3582 case ETHTOOL_FLASHDEV: 3583 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) { 3584 rc = -EFAULT; 3585 goto exit_free; 3586 } 3587 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 3588 break; 3589 } 3590 3591 rtnl_lock(); 3592 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state); 3593 rtnl_unlock(); 3594 if (rc) 3595 goto exit_free; 3596 3597 switch (ethcmd) { 3598 case ETHTOOL_FLASHDEV: 3599 if (state->devlink) 3600 rc = devlink_compat_flash_update(state->devlink, 3601 state->efl.data); 3602 break; 3603 case ETHTOOL_GDRVINFO: 3604 if (state->devlink) 3605 devlink_compat_running_version(state->devlink, 3606 state->info.fw_version, 3607 sizeof(state->info.fw_version)); 3608 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) { 3609 rc = -EFAULT; 3610 goto exit_free; 3611 } 3612 break; 3613 } 3614 3615 exit_free: 3616 if (state->devlink) 3617 devlink_put(state->devlink); 3618 kfree(state); 3619 return rc; 3620 } 3621 3622 struct ethtool_rx_flow_key { 3623 struct flow_dissector_key_basic basic; 3624 union { 3625 struct flow_dissector_key_ipv4_addrs ipv4; 3626 struct flow_dissector_key_ipv6_addrs ipv6; 3627 }; 3628 struct flow_dissector_key_ports tp; 3629 struct flow_dissector_key_ip ip; 3630 struct flow_dissector_key_vlan vlan; 3631 struct flow_dissector_key_eth_addrs eth_addrs; 3632 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 3633 3634 struct ethtool_rx_flow_match { 3635 struct flow_dissector dissector; 3636 struct ethtool_rx_flow_key key; 3637 struct ethtool_rx_flow_key mask; 3638 }; 3639 3640 struct ethtool_rx_flow_rule * 3641 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) 3642 { 3643 const struct ethtool_rx_flow_spec *fs = input->fs; 3644 struct ethtool_rx_flow_match *match; 3645 struct ethtool_rx_flow_rule *flow; 3646 struct flow_action_entry *act; 3647 3648 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + 3649 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); 3650 if (!flow) 3651 return ERR_PTR(-ENOMEM); 3652 3653 /* ethtool_rx supports only one single action per rule. */ 3654 flow->rule = flow_rule_alloc(1); 3655 if (!flow->rule) { 3656 kfree(flow); 3657 return ERR_PTR(-ENOMEM); 3658 } 3659 3660 match = (struct ethtool_rx_flow_match *)flow->priv; 3661 flow->rule->match.dissector = &match->dissector; 3662 flow->rule->match.mask = &match->mask; 3663 flow->rule->match.key = &match->key; 3664 3665 match->mask.basic.n_proto = htons(0xffff); 3666 3667 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3668 case ETHER_FLOW: { 3669 const struct ethhdr *ether_spec, *ether_m_spec; 3670 3671 ether_spec = &fs->h_u.ether_spec; 3672 ether_m_spec = &fs->m_u.ether_spec; 3673 3674 if (!is_zero_ether_addr(ether_m_spec->h_source)) { 3675 ether_addr_copy(match->key.eth_addrs.src, 3676 ether_spec->h_source); 3677 ether_addr_copy(match->mask.eth_addrs.src, 3678 ether_m_spec->h_source); 3679 } 3680 if (!is_zero_ether_addr(ether_m_spec->h_dest)) { 3681 ether_addr_copy(match->key.eth_addrs.dst, 3682 ether_spec->h_dest); 3683 ether_addr_copy(match->mask.eth_addrs.dst, 3684 ether_m_spec->h_dest); 3685 } 3686 if (ether_m_spec->h_proto) { 3687 match->key.basic.n_proto = ether_spec->h_proto; 3688 match->mask.basic.n_proto = ether_m_spec->h_proto; 3689 } 3690 } 3691 break; 3692 case TCP_V4_FLOW: 3693 case UDP_V4_FLOW: { 3694 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; 3695 3696 match->key.basic.n_proto = htons(ETH_P_IP); 3697 3698 v4_spec = &fs->h_u.tcp_ip4_spec; 3699 v4_m_spec = &fs->m_u.tcp_ip4_spec; 3700 3701 if (v4_m_spec->ip4src) { 3702 match->key.ipv4.src = v4_spec->ip4src; 3703 match->mask.ipv4.src = v4_m_spec->ip4src; 3704 } 3705 if (v4_m_spec->ip4dst) { 3706 match->key.ipv4.dst = v4_spec->ip4dst; 3707 match->mask.ipv4.dst = v4_m_spec->ip4dst; 3708 } 3709 if (v4_m_spec->ip4src || 3710 v4_m_spec->ip4dst) { 3711 match->dissector.used_keys |= 3712 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS); 3713 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 3714 offsetof(struct ethtool_rx_flow_key, ipv4); 3715 } 3716 if (v4_m_spec->psrc) { 3717 match->key.tp.src = v4_spec->psrc; 3718 match->mask.tp.src = v4_m_spec->psrc; 3719 } 3720 if (v4_m_spec->pdst) { 3721 match->key.tp.dst = v4_spec->pdst; 3722 match->mask.tp.dst = v4_m_spec->pdst; 3723 } 3724 if (v4_m_spec->psrc || 3725 v4_m_spec->pdst) { 3726 match->dissector.used_keys |= 3727 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3728 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3729 offsetof(struct ethtool_rx_flow_key, tp); 3730 } 3731 if (v4_m_spec->tos) { 3732 match->key.ip.tos = v4_spec->tos; 3733 match->mask.ip.tos = v4_m_spec->tos; 3734 match->dissector.used_keys |= 3735 BIT(FLOW_DISSECTOR_KEY_IP); 3736 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3737 offsetof(struct ethtool_rx_flow_key, ip); 3738 } 3739 } 3740 break; 3741 case TCP_V6_FLOW: 3742 case UDP_V6_FLOW: { 3743 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; 3744 3745 match->key.basic.n_proto = htons(ETH_P_IPV6); 3746 3747 v6_spec = &fs->h_u.tcp_ip6_spec; 3748 v6_m_spec = &fs->m_u.tcp_ip6_spec; 3749 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) { 3750 memcpy(&match->key.ipv6.src, v6_spec->ip6src, 3751 sizeof(match->key.ipv6.src)); 3752 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, 3753 sizeof(match->mask.ipv6.src)); 3754 } 3755 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3756 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, 3757 sizeof(match->key.ipv6.dst)); 3758 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, 3759 sizeof(match->mask.ipv6.dst)); 3760 } 3761 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) || 3762 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3763 match->dissector.used_keys |= 3764 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS); 3765 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = 3766 offsetof(struct ethtool_rx_flow_key, ipv6); 3767 } 3768 if (v6_m_spec->psrc) { 3769 match->key.tp.src = v6_spec->psrc; 3770 match->mask.tp.src = v6_m_spec->psrc; 3771 } 3772 if (v6_m_spec->pdst) { 3773 match->key.tp.dst = v6_spec->pdst; 3774 match->mask.tp.dst = v6_m_spec->pdst; 3775 } 3776 if (v6_m_spec->psrc || 3777 v6_m_spec->pdst) { 3778 match->dissector.used_keys |= 3779 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3780 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3781 offsetof(struct ethtool_rx_flow_key, tp); 3782 } 3783 if (v6_m_spec->tclass) { 3784 match->key.ip.tos = v6_spec->tclass; 3785 match->mask.ip.tos = v6_m_spec->tclass; 3786 match->dissector.used_keys |= 3787 BIT_ULL(FLOW_DISSECTOR_KEY_IP); 3788 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3789 offsetof(struct ethtool_rx_flow_key, ip); 3790 } 3791 } 3792 break; 3793 default: 3794 ethtool_rx_flow_rule_destroy(flow); 3795 return ERR_PTR(-EINVAL); 3796 } 3797 3798 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3799 case TCP_V4_FLOW: 3800 case TCP_V6_FLOW: 3801 match->key.basic.ip_proto = IPPROTO_TCP; 3802 match->mask.basic.ip_proto = 0xff; 3803 break; 3804 case UDP_V4_FLOW: 3805 case UDP_V6_FLOW: 3806 match->key.basic.ip_proto = IPPROTO_UDP; 3807 match->mask.basic.ip_proto = 0xff; 3808 break; 3809 } 3810 3811 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC); 3812 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = 3813 offsetof(struct ethtool_rx_flow_key, basic); 3814 3815 if (fs->flow_type & FLOW_EXT) { 3816 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3817 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3818 3819 if (ext_m_spec->vlan_etype) { 3820 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; 3821 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; 3822 } 3823 3824 if (ext_m_spec->vlan_tci) { 3825 match->key.vlan.vlan_id = 3826 ntohs(ext_h_spec->vlan_tci) & 0x0fff; 3827 match->mask.vlan.vlan_id = 3828 ntohs(ext_m_spec->vlan_tci) & 0x0fff; 3829 3830 match->key.vlan.vlan_dei = 3831 !!(ext_h_spec->vlan_tci & htons(0x1000)); 3832 match->mask.vlan.vlan_dei = 3833 !!(ext_m_spec->vlan_tci & htons(0x1000)); 3834 3835 match->key.vlan.vlan_priority = 3836 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; 3837 match->mask.vlan.vlan_priority = 3838 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; 3839 } 3840 3841 if (ext_m_spec->vlan_etype || 3842 ext_m_spec->vlan_tci) { 3843 match->dissector.used_keys |= 3844 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN); 3845 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = 3846 offsetof(struct ethtool_rx_flow_key, vlan); 3847 } 3848 } 3849 if (fs->flow_type & FLOW_MAC_EXT) { 3850 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3851 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3852 3853 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, 3854 ETH_ALEN); 3855 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, 3856 ETH_ALEN); 3857 3858 match->dissector.used_keys |= 3859 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS); 3860 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = 3861 offsetof(struct ethtool_rx_flow_key, eth_addrs); 3862 } 3863 3864 act = &flow->rule->action.entries[0]; 3865 switch (fs->ring_cookie) { 3866 case RX_CLS_FLOW_DISC: 3867 act->id = FLOW_ACTION_DROP; 3868 break; 3869 case RX_CLS_FLOW_WAKE: 3870 act->id = FLOW_ACTION_WAKE; 3871 break; 3872 default: 3873 act->id = FLOW_ACTION_QUEUE; 3874 if (fs->flow_type & FLOW_RSS) 3875 act->queue.ctx = input->rss_ctx; 3876 3877 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); 3878 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); 3879 break; 3880 } 3881 3882 return flow; 3883 } 3884 EXPORT_SYMBOL(ethtool_rx_flow_rule_create); 3885 3886 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) 3887 { 3888 kfree(flow->rule); 3889 kfree(flow); 3890 } 3891 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); 3892