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