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 ETHER_FLOW: 985 case TCP_V4_FLOW: 986 case UDP_V4_FLOW: 987 case SCTP_V4_FLOW: 988 case AH_ESP_V4_FLOW: 989 case TCP_V6_FLOW: 990 case UDP_V6_FLOW: 991 case SCTP_V6_FLOW: 992 case AH_ESP_V6_FLOW: 993 case AH_V4_FLOW: 994 case ESP_V4_FLOW: 995 case AH_V6_FLOW: 996 case ESP_V6_FLOW: 997 case IPV4_FLOW: 998 case IPV6_FLOW: 999 case GTPU_V4_FLOW: 1000 case GTPU_V6_FLOW: 1001 case GTPC_V4_FLOW: 1002 case GTPC_V6_FLOW: 1003 case GTPC_TEID_V4_FLOW: 1004 case GTPC_TEID_V6_FLOW: 1005 case GTPU_EH_V4_FLOW: 1006 case GTPU_EH_V6_FLOW: 1007 case GTPU_UL_V4_FLOW: 1008 case GTPU_UL_V6_FLOW: 1009 case GTPU_DL_V4_FLOW: 1010 case GTPU_DL_V6_FLOW: 1011 return true; 1012 } 1013 1014 return false; 1015 } 1016 1017 static bool flow_type_v6(u32 flow_type) 1018 { 1019 switch (flow_type) { 1020 case TCP_V6_FLOW: 1021 case UDP_V6_FLOW: 1022 case SCTP_V6_FLOW: 1023 case AH_ESP_V6_FLOW: 1024 case AH_V6_FLOW: 1025 case ESP_V6_FLOW: 1026 case IPV6_FLOW: 1027 case GTPU_V6_FLOW: 1028 case GTPC_V6_FLOW: 1029 case GTPC_TEID_V6_FLOW: 1030 case GTPU_EH_V6_FLOW: 1031 case GTPU_UL_V6_FLOW: 1032 case GTPU_DL_V6_FLOW: 1033 return true; 1034 } 1035 1036 return false; 1037 } 1038 1039 /* When adding a new type, update the assert and, if it's hashable, add it to 1040 * the flow_type_hashable switch case. 1041 */ 1042 static_assert(GTPU_DL_V6_FLOW + 1 == __FLOW_TYPE_COUNT); 1043 1044 static int ethtool_check_xfrm_rxfh(u32 input_xfrm, u64 rxfh) 1045 { 1046 /* Sanity check: if symmetric-xor/symmetric-or-xor is set, then: 1047 * 1 - no other fields besides IP src/dst and/or L4 src/dst are set 1048 * 2 - If src is set, dst must also be set 1049 */ 1050 if ((input_xfrm != RXH_XFRM_NO_CHANGE && 1051 input_xfrm & (RXH_XFRM_SYM_XOR | RXH_XFRM_SYM_OR_XOR)) && 1052 !ethtool_rxfh_config_is_sym(rxfh)) 1053 return -EINVAL; 1054 1055 return 0; 1056 } 1057 1058 static int ethtool_check_flow_types(struct net_device *dev, u32 input_xfrm) 1059 { 1060 const struct ethtool_ops *ops = dev->ethtool_ops; 1061 int err; 1062 u32 i; 1063 1064 if (!input_xfrm || input_xfrm == RXH_XFRM_NO_CHANGE) 1065 return 0; 1066 1067 for (i = 0; i < __FLOW_TYPE_COUNT; i++) { 1068 struct ethtool_rxfh_fields fields = { 1069 .flow_type = i, 1070 }; 1071 1072 if (!flow_type_hashable(i)) 1073 continue; 1074 1075 if (ops->get_rxfh_fields(dev, &fields)) 1076 continue; 1077 1078 err = ethtool_check_xfrm_rxfh(input_xfrm, fields.data); 1079 if (err) 1080 return err; 1081 } 1082 1083 return 0; 1084 } 1085 1086 static noinline_for_stack int 1087 ethtool_set_rxfh_fields(struct net_device *dev, u32 cmd, void __user *useraddr) 1088 { 1089 const struct ethtool_ops *ops = dev->ethtool_ops; 1090 struct ethtool_rxfh_fields fields = {}; 1091 struct ethtool_rxnfc info; 1092 size_t info_size = sizeof(info); 1093 int rc; 1094 1095 if (!ops->set_rxfh_fields) 1096 return -EOPNOTSUPP; 1097 1098 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr); 1099 if (rc) 1100 return rc; 1101 1102 if (info.data & RXH_IP6_FL && !flow_type_v6(info.flow_type)) 1103 return -EINVAL; 1104 1105 if (info.flow_type & FLOW_RSS && info.rss_context && 1106 !ops->rxfh_per_ctx_fields) 1107 return -EINVAL; 1108 1109 mutex_lock(&dev->ethtool->rss_lock); 1110 if (ops->get_rxfh) { 1111 struct ethtool_rxfh_param rxfh = {}; 1112 1113 rc = ops->get_rxfh(dev, &rxfh); 1114 if (rc) 1115 goto exit_unlock; 1116 1117 rc = ethtool_check_xfrm_rxfh(rxfh.input_xfrm, info.data); 1118 if (rc) 1119 goto exit_unlock; 1120 } 1121 1122 fields.data = info.data; 1123 fields.flow_type = info.flow_type & ~FLOW_RSS; 1124 if (info.flow_type & FLOW_RSS) 1125 fields.rss_context = info.rss_context; 1126 1127 rc = ops->set_rxfh_fields(dev, &fields, NULL); 1128 exit_unlock: 1129 mutex_unlock(&dev->ethtool->rss_lock); 1130 if (rc) 1131 return rc; 1132 1133 ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_NTF, fields.rss_context); 1134 return 0; 1135 } 1136 1137 static noinline_for_stack int 1138 ethtool_get_rxfh_fields(struct net_device *dev, u32 cmd, void __user *useraddr) 1139 { 1140 struct ethtool_rxnfc info; 1141 size_t info_size = sizeof(info); 1142 const struct ethtool_ops *ops = dev->ethtool_ops; 1143 struct ethtool_rxfh_fields fields = {}; 1144 int ret; 1145 1146 if (!ops->get_rxfh_fields) 1147 return -EOPNOTSUPP; 1148 1149 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr); 1150 if (ret) 1151 return ret; 1152 1153 if (info.flow_type & FLOW_RSS && info.rss_context && 1154 !ops->rxfh_per_ctx_fields) 1155 return -EINVAL; 1156 1157 fields.flow_type = info.flow_type & ~FLOW_RSS; 1158 if (info.flow_type & FLOW_RSS) 1159 fields.rss_context = info.rss_context; 1160 1161 mutex_lock(&dev->ethtool->rss_lock); 1162 ret = ops->get_rxfh_fields(dev, &fields); 1163 mutex_unlock(&dev->ethtool->rss_lock); 1164 if (ret < 0) 1165 return ret; 1166 1167 info.data = fields.data; 1168 1169 return ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL); 1170 } 1171 1172 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, 1173 u32 cmd, void __user *useraddr) 1174 { 1175 const struct ethtool_ops *ops = dev->ethtool_ops; 1176 struct ethtool_rxnfc info; 1177 size_t info_size = sizeof(info); 1178 int rc; 1179 1180 if (!ops->set_rxnfc) 1181 return -EOPNOTSUPP; 1182 1183 rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr); 1184 if (rc) 1185 return rc; 1186 1187 if (cmd == ETHTOOL_SRXCLSRLINS && info.fs.flow_type & FLOW_RSS) { 1188 /* Nonzero ring with RSS only makes sense 1189 * if NIC adds them together 1190 */ 1191 if (!ops->cap_rss_rxnfc_adds && 1192 ethtool_get_flow_spec_ring(info.fs.ring_cookie)) 1193 return -EINVAL; 1194 1195 if (info.rss_context && 1196 !xa_load(&dev->ethtool->rss_ctx, info.rss_context)) 1197 return -EINVAL; 1198 } 1199 1200 rc = ops->set_rxnfc(dev, &info); 1201 if (rc) 1202 return rc; 1203 1204 if (cmd == ETHTOOL_SRXCLSRLINS && 1205 ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL)) 1206 return -EFAULT; 1207 1208 return 0; 1209 } 1210 1211 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, 1212 u32 cmd, void __user *useraddr) 1213 { 1214 struct ethtool_rxnfc info; 1215 size_t info_size = sizeof(info); 1216 const struct ethtool_ops *ops = dev->ethtool_ops; 1217 int ret; 1218 void *rule_buf = NULL; 1219 1220 if (!ops->get_rxnfc) 1221 return -EOPNOTSUPP; 1222 1223 ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr); 1224 if (ret) 1225 return ret; 1226 1227 if (info.cmd == ETHTOOL_GRXCLSRLALL) { 1228 if (info.rule_cnt > 0) { 1229 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) 1230 rule_buf = kcalloc(info.rule_cnt, sizeof(u32), 1231 GFP_USER); 1232 if (!rule_buf) 1233 return -ENOMEM; 1234 } 1235 } 1236 1237 ret = ops->get_rxnfc(dev, &info, rule_buf); 1238 if (ret < 0) 1239 goto err_out; 1240 1241 ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf); 1242 err_out: 1243 kfree(rule_buf); 1244 1245 return ret; 1246 } 1247 1248 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, 1249 struct ethtool_rxnfc *rx_rings, 1250 u32 size) 1251 { 1252 int i; 1253 1254 if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0])))) 1255 return -EFAULT; 1256 1257 /* Validate ring indices */ 1258 for (i = 0; i < size; i++) 1259 if (indir[i] >= rx_rings->data) 1260 return -EINVAL; 1261 1262 return 0; 1263 } 1264 1265 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly; 1266 1267 void netdev_rss_key_fill(void *buffer, size_t len) 1268 { 1269 BUG_ON(len > sizeof(netdev_rss_key)); 1270 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key)); 1271 memcpy(buffer, netdev_rss_key, len); 1272 } 1273 EXPORT_SYMBOL(netdev_rss_key_fill); 1274 1275 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, 1276 void __user *useraddr) 1277 { 1278 struct ethtool_rxfh_param rxfh = {}; 1279 u32 user_size; 1280 int ret; 1281 1282 if (!dev->ethtool_ops->get_rxfh_indir_size || 1283 !dev->ethtool_ops->get_rxfh) 1284 return -EOPNOTSUPP; 1285 rxfh.indir_size = dev->ethtool_ops->get_rxfh_indir_size(dev); 1286 if (rxfh.indir_size == 0) 1287 return -EOPNOTSUPP; 1288 1289 if (copy_from_user(&user_size, 1290 useraddr + offsetof(struct ethtool_rxfh_indir, size), 1291 sizeof(user_size))) 1292 return -EFAULT; 1293 1294 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), 1295 &rxfh.indir_size, sizeof(rxfh.indir_size))) 1296 return -EFAULT; 1297 1298 /* If the user buffer size is 0, this is just a query for the 1299 * device table size. Otherwise, if it's smaller than the 1300 * device table size it's an error. 1301 */ 1302 if (user_size < rxfh.indir_size) 1303 return user_size == 0 ? 0 : -EINVAL; 1304 1305 rxfh.indir = kcalloc(rxfh.indir_size, sizeof(rxfh.indir[0]), GFP_USER); 1306 if (!rxfh.indir) 1307 return -ENOMEM; 1308 1309 mutex_lock(&dev->ethtool->rss_lock); 1310 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh); 1311 mutex_unlock(&dev->ethtool->rss_lock); 1312 if (ret) 1313 goto out; 1314 if (copy_to_user(useraddr + 1315 offsetof(struct ethtool_rxfh_indir, ring_index[0]), 1316 rxfh.indir, rxfh.indir_size * sizeof(*rxfh.indir))) 1317 ret = -EFAULT; 1318 1319 out: 1320 kfree(rxfh.indir); 1321 return ret; 1322 } 1323 1324 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, 1325 void __user *useraddr) 1326 { 1327 const struct ethtool_ops *ops = dev->ethtool_ops; 1328 struct ethtool_rxfh_param rxfh_dev = {}; 1329 struct netlink_ext_ack *extack = NULL; 1330 struct ethtool_rxnfc rx_rings; 1331 u32 user_size, i; 1332 int ret; 1333 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); 1334 1335 if (!ops->get_rxfh_indir_size || !ops->set_rxfh || 1336 !ops->get_rxnfc) 1337 return -EOPNOTSUPP; 1338 1339 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev); 1340 if (rxfh_dev.indir_size == 0) 1341 return -EOPNOTSUPP; 1342 1343 if (copy_from_user(&user_size, 1344 useraddr + offsetof(struct ethtool_rxfh_indir, size), 1345 sizeof(user_size))) 1346 return -EFAULT; 1347 1348 if (user_size != 0 && user_size != rxfh_dev.indir_size) 1349 return -EINVAL; 1350 1351 rxfh_dev.indir = kcalloc(rxfh_dev.indir_size, 1352 sizeof(rxfh_dev.indir[0]), GFP_USER); 1353 if (!rxfh_dev.indir) 1354 return -ENOMEM; 1355 1356 rx_rings.cmd = ETHTOOL_GRXRINGS; 1357 ret = ops->get_rxnfc(dev, &rx_rings, NULL); 1358 if (ret) 1359 goto out; 1360 1361 if (user_size == 0) { 1362 u32 *indir = rxfh_dev.indir; 1363 1364 for (i = 0; i < rxfh_dev.indir_size; i++) 1365 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); 1366 } else { 1367 ret = ethtool_copy_validate_indir(rxfh_dev.indir, 1368 useraddr + ringidx_offset, 1369 &rx_rings, 1370 rxfh_dev.indir_size); 1371 if (ret) 1372 goto out; 1373 } 1374 1375 rxfh_dev.hfunc = ETH_RSS_HASH_NO_CHANGE; 1376 1377 mutex_lock(&dev->ethtool->rss_lock); 1378 ret = ops->set_rxfh(dev, &rxfh_dev, extack); 1379 if (ret) 1380 goto out_unlock; 1381 1382 /* indicate whether rxfh was set to default */ 1383 if (user_size == 0) 1384 dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 1385 else 1386 dev->priv_flags |= IFF_RXFH_CONFIGURED; 1387 1388 out_unlock: 1389 mutex_unlock(&dev->ethtool->rss_lock); 1390 out: 1391 kfree(rxfh_dev.indir); 1392 return ret; 1393 } 1394 1395 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, 1396 void __user *useraddr) 1397 { 1398 const struct ethtool_ops *ops = dev->ethtool_ops; 1399 struct ethtool_rxfh_param rxfh_dev = {}; 1400 u32 user_indir_size, user_key_size; 1401 struct ethtool_rxfh_context *ctx; 1402 struct ethtool_rxfh rxfh; 1403 u32 indir_bytes; 1404 u8 *rss_config; 1405 u32 total_size; 1406 int ret; 1407 1408 if (!ops->get_rxfh) 1409 return -EOPNOTSUPP; 1410 1411 if (ops->get_rxfh_indir_size) 1412 rxfh_dev.indir_size = ops->get_rxfh_indir_size(dev); 1413 if (ops->get_rxfh_key_size) 1414 rxfh_dev.key_size = ops->get_rxfh_key_size(dev); 1415 1416 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) 1417 return -EFAULT; 1418 user_indir_size = rxfh.indir_size; 1419 user_key_size = rxfh.key_size; 1420 1421 /* Check that reserved fields are 0 for now */ 1422 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32) 1423 return -EINVAL; 1424 /* Most drivers don't handle rss_context, check it's 0 as well */ 1425 if (rxfh.rss_context && !ops->create_rxfh_context) 1426 return -EOPNOTSUPP; 1427 1428 rxfh.indir_size = rxfh_dev.indir_size; 1429 rxfh.key_size = rxfh_dev.key_size; 1430 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh))) 1431 return -EFAULT; 1432 1433 if ((user_indir_size && user_indir_size != rxfh_dev.indir_size) || 1434 (user_key_size && user_key_size != rxfh_dev.key_size)) 1435 return -EINVAL; 1436 1437 indir_bytes = user_indir_size * sizeof(rxfh_dev.indir[0]); 1438 total_size = indir_bytes + user_key_size; 1439 rss_config = kzalloc(total_size, GFP_USER); 1440 if (!rss_config) 1441 return -ENOMEM; 1442 1443 if (user_indir_size) 1444 rxfh_dev.indir = (u32 *)rss_config; 1445 1446 if (user_key_size) 1447 rxfh_dev.key = rss_config + indir_bytes; 1448 1449 mutex_lock(&dev->ethtool->rss_lock); 1450 if (rxfh.rss_context) { 1451 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context); 1452 if (!ctx) { 1453 ret = -ENOENT; 1454 goto out; 1455 } 1456 if (rxfh_dev.indir) 1457 memcpy(rxfh_dev.indir, ethtool_rxfh_context_indir(ctx), 1458 indir_bytes); 1459 if (!ops->rxfh_per_ctx_key) { 1460 rxfh_dev.key_size = 0; 1461 } else { 1462 if (rxfh_dev.key) 1463 memcpy(rxfh_dev.key, 1464 ethtool_rxfh_context_key(ctx), 1465 user_key_size); 1466 rxfh_dev.hfunc = ctx->hfunc; 1467 } 1468 rxfh_dev.input_xfrm = ctx->input_xfrm; 1469 ret = 0; 1470 } else { 1471 ret = dev->ethtool_ops->get_rxfh(dev, &rxfh_dev); 1472 if (ret) 1473 goto out; 1474 } 1475 1476 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc), 1477 &rxfh_dev.hfunc, sizeof(rxfh.hfunc))) { 1478 ret = -EFAULT; 1479 } else if (copy_to_user(useraddr + 1480 offsetof(struct ethtool_rxfh, input_xfrm), 1481 &rxfh_dev.input_xfrm, 1482 sizeof(rxfh.input_xfrm))) { 1483 ret = -EFAULT; 1484 } else if (copy_to_user(useraddr + 1485 offsetof(struct ethtool_rxfh, key_size), 1486 &rxfh_dev.key_size, 1487 sizeof(rxfh.key_size))) { 1488 ret = -EFAULT; 1489 } else if (copy_to_user(useraddr + 1490 offsetof(struct ethtool_rxfh, rss_config[0]), 1491 rss_config, total_size)) { 1492 ret = -EFAULT; 1493 } 1494 out: 1495 mutex_unlock(&dev->ethtool->rss_lock); 1496 kfree(rss_config); 1497 1498 return ret; 1499 } 1500 1501 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, 1502 void __user *useraddr) 1503 { 1504 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); 1505 const struct ethtool_ops *ops = dev->ethtool_ops; 1506 u32 dev_indir_size = 0, dev_key_size = 0, i; 1507 u32 user_indir_len = 0, indir_bytes = 0; 1508 struct ethtool_rxfh_param rxfh_dev = {}; 1509 struct ethtool_rxfh_context *ctx = NULL; 1510 struct netlink_ext_ack *extack = NULL; 1511 struct ethtool_rxnfc rx_rings; 1512 struct ethtool_rxfh rxfh; 1513 bool create = false; 1514 u8 *rss_config; 1515 int ntf = 0; 1516 int ret; 1517 1518 if (!ops->get_rxnfc || !ops->set_rxfh) 1519 return -EOPNOTSUPP; 1520 1521 if (ops->get_rxfh_indir_size) 1522 dev_indir_size = ops->get_rxfh_indir_size(dev); 1523 if (ops->get_rxfh_key_size) 1524 dev_key_size = ops->get_rxfh_key_size(dev); 1525 1526 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) 1527 return -EFAULT; 1528 1529 /* Check that reserved fields are 0 for now */ 1530 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd32) 1531 return -EINVAL; 1532 /* Most drivers don't handle rss_context, check it's 0 as well */ 1533 if (rxfh.rss_context && !ops->create_rxfh_context) 1534 return -EOPNOTSUPP; 1535 /* Check input data transformation capabilities */ 1536 if (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_SYM_XOR && 1537 rxfh.input_xfrm != RXH_XFRM_SYM_OR_XOR && 1538 rxfh.input_xfrm != RXH_XFRM_NO_CHANGE) 1539 return -EINVAL; 1540 if (rxfh.input_xfrm != RXH_XFRM_NO_CHANGE && 1541 rxfh.input_xfrm & ~ops->supported_input_xfrm) 1542 return -EOPNOTSUPP; 1543 create = rxfh.rss_context == ETH_RXFH_CONTEXT_ALLOC; 1544 1545 if ((rxfh.indir_size && 1546 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && 1547 rxfh.indir_size != dev_indir_size) || 1548 (rxfh.key_size && rxfh.key_size != dev_key_size)) 1549 return -EINVAL; 1550 1551 /* Must request at least one change: indir size, hash key, function 1552 * or input transformation. 1553 * There's no need for any of it in case of context creation. 1554 */ 1555 if (!create && 1556 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && 1557 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE && 1558 rxfh.input_xfrm == RXH_XFRM_NO_CHANGE)) 1559 return -EINVAL; 1560 1561 indir_bytes = dev_indir_size * sizeof(rxfh_dev.indir[0]); 1562 1563 /* Check settings which may be global rather than per RSS-context */ 1564 if (rxfh.rss_context && !ops->rxfh_per_ctx_key) 1565 if (rxfh.key_size || 1566 (rxfh.hfunc && rxfh.hfunc != ETH_RSS_HASH_NO_CHANGE) || 1567 (rxfh.input_xfrm && rxfh.input_xfrm != RXH_XFRM_NO_CHANGE)) 1568 return -EOPNOTSUPP; 1569 1570 rss_config = kzalloc(indir_bytes + dev_key_size, GFP_USER); 1571 if (!rss_config) 1572 return -ENOMEM; 1573 1574 rx_rings.cmd = ETHTOOL_GRXRINGS; 1575 ret = ops->get_rxnfc(dev, &rx_rings, NULL); 1576 if (ret) 1577 goto out_free; 1578 1579 /* rxfh.indir_size == 0 means reset the indir table to default (master 1580 * context) or delete the context (other RSS contexts). 1581 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. 1582 */ 1583 if (rxfh.indir_size && 1584 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { 1585 user_indir_len = indir_bytes; 1586 rxfh_dev.indir = (u32 *)rss_config; 1587 rxfh_dev.indir_size = dev_indir_size; 1588 ret = ethtool_copy_validate_indir(rxfh_dev.indir, 1589 useraddr + rss_cfg_offset, 1590 &rx_rings, 1591 rxfh.indir_size); 1592 if (ret) 1593 goto out_free; 1594 } else if (rxfh.indir_size == 0) { 1595 if (rxfh.rss_context == 0) { 1596 u32 *indir; 1597 1598 rxfh_dev.indir = (u32 *)rss_config; 1599 rxfh_dev.indir_size = dev_indir_size; 1600 indir = rxfh_dev.indir; 1601 for (i = 0; i < dev_indir_size; i++) 1602 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); 1603 } else { 1604 rxfh_dev.rss_delete = true; 1605 } 1606 } 1607 1608 if (rxfh.key_size) { 1609 rxfh_dev.key_size = dev_key_size; 1610 rxfh_dev.key = rss_config + indir_bytes; 1611 if (copy_from_user(rxfh_dev.key, 1612 useraddr + rss_cfg_offset + user_indir_len, 1613 rxfh.key_size)) { 1614 ret = -EFAULT; 1615 goto out_free; 1616 } 1617 } 1618 1619 mutex_lock(&dev->ethtool->rss_lock); 1620 1621 ret = ethtool_check_flow_types(dev, rxfh.input_xfrm); 1622 if (ret) 1623 goto out_unlock; 1624 1625 if (rxfh.rss_context && rxfh_dev.rss_delete) { 1626 ret = ethtool_check_rss_ctx_busy(dev, rxfh.rss_context); 1627 if (ret) 1628 goto out_unlock; 1629 } 1630 1631 if (create) { 1632 u32 limit, ctx_id; 1633 1634 if (rxfh_dev.rss_delete) { 1635 ret = -EINVAL; 1636 goto out_unlock; 1637 } 1638 ctx = ethtool_rxfh_ctx_alloc(ops, dev_indir_size, dev_key_size); 1639 if (!ctx) { 1640 ret = -ENOMEM; 1641 goto out_unlock; 1642 } 1643 1644 limit = ops->rxfh_max_num_contexts ?: U32_MAX; 1645 ret = xa_alloc(&dev->ethtool->rss_ctx, &ctx_id, ctx, 1646 XA_LIMIT(1, limit - 1), GFP_KERNEL_ACCOUNT); 1647 if (ret < 0) { 1648 kfree(ctx); 1649 goto out_unlock; 1650 } 1651 WARN_ON(!ctx_id); /* can't happen */ 1652 rxfh.rss_context = ctx_id; 1653 } else if (rxfh.rss_context) { 1654 ctx = xa_load(&dev->ethtool->rss_ctx, rxfh.rss_context); 1655 if (!ctx) { 1656 ret = -ENOENT; 1657 goto out_unlock; 1658 } 1659 } 1660 rxfh_dev.hfunc = rxfh.hfunc; 1661 rxfh_dev.rss_context = rxfh.rss_context; 1662 rxfh_dev.input_xfrm = rxfh.input_xfrm; 1663 1664 if (!rxfh.rss_context) { 1665 ntf = ETHTOOL_MSG_RSS_NTF; 1666 ret = ops->set_rxfh(dev, &rxfh_dev, extack); 1667 } else if (create) { 1668 ntf = ETHTOOL_MSG_RSS_CREATE_NTF; 1669 ret = ops->create_rxfh_context(dev, ctx, &rxfh_dev, extack); 1670 /* Make sure driver populates defaults */ 1671 WARN_ON_ONCE(!ret && !rxfh_dev.key && ops->rxfh_per_ctx_key && 1672 !memchr_inv(ethtool_rxfh_context_key(ctx), 0, 1673 ctx->key_size)); 1674 } else if (rxfh_dev.rss_delete) { 1675 ntf = ETHTOOL_MSG_RSS_DELETE_NTF; 1676 ret = ops->remove_rxfh_context(dev, ctx, rxfh.rss_context, 1677 extack); 1678 } else { 1679 ntf = ETHTOOL_MSG_RSS_NTF; 1680 ret = ops->modify_rxfh_context(dev, ctx, &rxfh_dev, extack); 1681 } 1682 if (ret) { 1683 ntf = 0; 1684 if (create) { 1685 /* failed to create, free our new tracking entry */ 1686 xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context); 1687 kfree(ctx); 1688 } 1689 goto out_unlock; 1690 } 1691 1692 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context), 1693 &rxfh_dev.rss_context, sizeof(rxfh_dev.rss_context))) 1694 ret = -EFAULT; 1695 1696 if (!rxfh_dev.rss_context) { 1697 /* indicate whether rxfh was set to default */ 1698 if (rxfh.indir_size == 0) 1699 dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 1700 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) 1701 dev->priv_flags |= IFF_RXFH_CONFIGURED; 1702 } 1703 /* Update rss_ctx tracking */ 1704 if (rxfh_dev.rss_delete) { 1705 WARN_ON(xa_erase(&dev->ethtool->rss_ctx, rxfh.rss_context) != ctx); 1706 kfree(ctx); 1707 } else if (ctx) { 1708 if (rxfh_dev.indir) { 1709 for (i = 0; i < dev_indir_size; i++) 1710 ethtool_rxfh_context_indir(ctx)[i] = rxfh_dev.indir[i]; 1711 ctx->indir_configured = 1712 rxfh.indir_size && 1713 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE; 1714 } 1715 if (rxfh_dev.key) { 1716 memcpy(ethtool_rxfh_context_key(ctx), rxfh_dev.key, 1717 dev_key_size); 1718 ctx->key_configured = !!rxfh.key_size; 1719 } 1720 if (rxfh_dev.hfunc != ETH_RSS_HASH_NO_CHANGE) 1721 ctx->hfunc = rxfh_dev.hfunc; 1722 if (rxfh_dev.input_xfrm != RXH_XFRM_NO_CHANGE) 1723 ctx->input_xfrm = rxfh_dev.input_xfrm; 1724 } 1725 1726 out_unlock: 1727 mutex_unlock(&dev->ethtool->rss_lock); 1728 out_free: 1729 kfree(rss_config); 1730 if (ntf) 1731 ethtool_rss_notify(dev, ntf, rxfh.rss_context); 1732 return ret; 1733 } 1734 1735 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) 1736 { 1737 struct ethtool_regs regs; 1738 const struct ethtool_ops *ops = dev->ethtool_ops; 1739 void *regbuf; 1740 int reglen, ret; 1741 1742 if (!ops->get_regs || !ops->get_regs_len) 1743 return -EOPNOTSUPP; 1744 1745 if (copy_from_user(®s, useraddr, sizeof(regs))) 1746 return -EFAULT; 1747 1748 reglen = ops->get_regs_len(dev); 1749 if (reglen <= 0) 1750 return reglen; 1751 1752 if (regs.len > reglen) 1753 regs.len = reglen; 1754 1755 regbuf = vzalloc(reglen); 1756 if (!regbuf) 1757 return -ENOMEM; 1758 1759 if (regs.len < reglen) 1760 reglen = regs.len; 1761 1762 ops->get_regs(dev, ®s, regbuf); 1763 1764 ret = -EFAULT; 1765 if (copy_to_user(useraddr, ®s, sizeof(regs))) 1766 goto out; 1767 useraddr += offsetof(struct ethtool_regs, data); 1768 if (copy_to_user(useraddr, regbuf, reglen)) 1769 goto out; 1770 ret = 0; 1771 1772 out: 1773 vfree(regbuf); 1774 return ret; 1775 } 1776 1777 static int ethtool_reset(struct net_device *dev, char __user *useraddr) 1778 { 1779 struct ethtool_value reset; 1780 int ret; 1781 1782 if (!dev->ethtool_ops->reset) 1783 return -EOPNOTSUPP; 1784 1785 if (dev->ethtool->module_fw_flash_in_progress) 1786 return -EBUSY; 1787 1788 if (copy_from_user(&reset, useraddr, sizeof(reset))) 1789 return -EFAULT; 1790 1791 ret = dev->ethtool_ops->reset(dev, &reset.data); 1792 if (ret) 1793 return ret; 1794 1795 if (copy_to_user(useraddr, &reset, sizeof(reset))) 1796 return -EFAULT; 1797 return 0; 1798 } 1799 1800 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) 1801 { 1802 struct ethtool_wolinfo wol; 1803 1804 if (!dev->ethtool_ops->get_wol) 1805 return -EOPNOTSUPP; 1806 1807 memset(&wol, 0, sizeof(struct ethtool_wolinfo)); 1808 wol.cmd = ETHTOOL_GWOL; 1809 dev->ethtool_ops->get_wol(dev, &wol); 1810 1811 if (copy_to_user(useraddr, &wol, sizeof(wol))) 1812 return -EFAULT; 1813 return 0; 1814 } 1815 1816 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) 1817 { 1818 struct ethtool_wolinfo wol, cur_wol; 1819 int ret; 1820 1821 if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol) 1822 return -EOPNOTSUPP; 1823 1824 memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo)); 1825 cur_wol.cmd = ETHTOOL_GWOL; 1826 dev->ethtool_ops->get_wol(dev, &cur_wol); 1827 1828 if (copy_from_user(&wol, useraddr, sizeof(wol))) 1829 return -EFAULT; 1830 1831 if (wol.wolopts & ~cur_wol.supported) 1832 return -EINVAL; 1833 1834 if (wol.wolopts == cur_wol.wolopts && 1835 !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass))) 1836 return 0; 1837 1838 ret = dev->ethtool_ops->set_wol(dev, &wol); 1839 if (ret) 1840 return ret; 1841 1842 dev->ethtool->wol_enabled = !!wol.wolopts; 1843 ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF); 1844 1845 return 0; 1846 } 1847 1848 static void eee_to_keee(struct ethtool_keee *keee, 1849 const struct ethtool_eee *eee) 1850 { 1851 memset(keee, 0, sizeof(*keee)); 1852 1853 keee->eee_enabled = eee->eee_enabled; 1854 keee->tx_lpi_enabled = eee->tx_lpi_enabled; 1855 keee->tx_lpi_timer = eee->tx_lpi_timer; 1856 1857 ethtool_convert_legacy_u32_to_link_mode(keee->advertised, 1858 eee->advertised); 1859 } 1860 1861 static void keee_to_eee(struct ethtool_eee *eee, 1862 const struct ethtool_keee *keee) 1863 { 1864 bool overflow; 1865 1866 memset(eee, 0, sizeof(*eee)); 1867 1868 eee->eee_active = keee->eee_active; 1869 eee->eee_enabled = keee->eee_enabled; 1870 eee->tx_lpi_enabled = keee->tx_lpi_enabled; 1871 eee->tx_lpi_timer = keee->tx_lpi_timer; 1872 1873 overflow = !ethtool_convert_link_mode_to_legacy_u32(&eee->supported, 1874 keee->supported); 1875 ethtool_convert_link_mode_to_legacy_u32(&eee->advertised, 1876 keee->advertised); 1877 ethtool_convert_link_mode_to_legacy_u32(&eee->lp_advertised, 1878 keee->lp_advertised); 1879 if (overflow) 1880 pr_warn("Ethtool ioctl interface doesn't support passing EEE linkmodes beyond bit 32\n"); 1881 } 1882 1883 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) 1884 { 1885 struct ethtool_keee keee; 1886 struct ethtool_eee eee; 1887 int rc; 1888 1889 if (!dev->ethtool_ops->get_eee) 1890 return -EOPNOTSUPP; 1891 1892 memset(&keee, 0, sizeof(keee)); 1893 rc = dev->ethtool_ops->get_eee(dev, &keee); 1894 if (rc) 1895 return rc; 1896 1897 keee_to_eee(&eee, &keee); 1898 if (copy_to_user(useraddr, &eee, sizeof(eee))) 1899 return -EFAULT; 1900 1901 return 0; 1902 } 1903 1904 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) 1905 { 1906 struct ethtool_keee keee; 1907 struct ethtool_eee eee; 1908 int ret; 1909 1910 if (!dev->ethtool_ops->set_eee) 1911 return -EOPNOTSUPP; 1912 1913 if (copy_from_user(&eee, useraddr, sizeof(eee))) 1914 return -EFAULT; 1915 1916 eee_to_keee(&keee, &eee); 1917 ret = dev->ethtool_ops->set_eee(dev, &keee); 1918 if (!ret) 1919 ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF); 1920 return ret; 1921 } 1922 1923 static int ethtool_nway_reset(struct net_device *dev) 1924 { 1925 if (!dev->ethtool_ops->nway_reset) 1926 return -EOPNOTSUPP; 1927 1928 return dev->ethtool_ops->nway_reset(dev); 1929 } 1930 1931 static int ethtool_get_link(struct net_device *dev, char __user *useraddr) 1932 { 1933 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; 1934 int link = __ethtool_get_link(dev); 1935 1936 if (link < 0) 1937 return link; 1938 1939 edata.data = link; 1940 if (copy_to_user(useraddr, &edata, sizeof(edata))) 1941 return -EFAULT; 1942 return 0; 1943 } 1944 1945 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, 1946 int (*getter)(struct net_device *, 1947 struct ethtool_eeprom *, u8 *), 1948 u32 total_len) 1949 { 1950 struct ethtool_eeprom eeprom; 1951 void __user *userbuf = useraddr + sizeof(eeprom); 1952 u32 bytes_remaining; 1953 u8 *data; 1954 int ret = 0; 1955 1956 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1957 return -EFAULT; 1958 1959 /* Check for wrap and zero */ 1960 if (eeprom.offset + eeprom.len <= eeprom.offset) 1961 return -EINVAL; 1962 1963 /* Check for exceeding total eeprom len */ 1964 if (eeprom.offset + eeprom.len > total_len) 1965 return -EINVAL; 1966 1967 data = kzalloc(PAGE_SIZE, GFP_USER); 1968 if (!data) 1969 return -ENOMEM; 1970 1971 bytes_remaining = eeprom.len; 1972 while (bytes_remaining > 0) { 1973 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1974 1975 ret = getter(dev, &eeprom, data); 1976 if (ret) 1977 break; 1978 if (!eeprom.len) { 1979 ret = -EIO; 1980 break; 1981 } 1982 if (copy_to_user(userbuf, data, eeprom.len)) { 1983 ret = -EFAULT; 1984 break; 1985 } 1986 userbuf += eeprom.len; 1987 eeprom.offset += eeprom.len; 1988 bytes_remaining -= eeprom.len; 1989 } 1990 1991 eeprom.len = userbuf - (useraddr + sizeof(eeprom)); 1992 eeprom.offset -= eeprom.len; 1993 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) 1994 ret = -EFAULT; 1995 1996 kfree(data); 1997 return ret; 1998 } 1999 2000 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) 2001 { 2002 const struct ethtool_ops *ops = dev->ethtool_ops; 2003 2004 if (!ops->get_eeprom || !ops->get_eeprom_len || 2005 !ops->get_eeprom_len(dev)) 2006 return -EOPNOTSUPP; 2007 2008 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, 2009 ops->get_eeprom_len(dev)); 2010 } 2011 2012 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) 2013 { 2014 struct ethtool_eeprom eeprom; 2015 const struct ethtool_ops *ops = dev->ethtool_ops; 2016 void __user *userbuf = useraddr + sizeof(eeprom); 2017 u32 bytes_remaining; 2018 u8 *data; 2019 int ret = 0; 2020 2021 if (!ops->set_eeprom || !ops->get_eeprom_len || 2022 !ops->get_eeprom_len(dev)) 2023 return -EOPNOTSUPP; 2024 2025 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 2026 return -EFAULT; 2027 2028 /* Check for wrap and zero */ 2029 if (eeprom.offset + eeprom.len <= eeprom.offset) 2030 return -EINVAL; 2031 2032 /* Check for exceeding total eeprom len */ 2033 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) 2034 return -EINVAL; 2035 2036 data = kzalloc(PAGE_SIZE, GFP_USER); 2037 if (!data) 2038 return -ENOMEM; 2039 2040 bytes_remaining = eeprom.len; 2041 while (bytes_remaining > 0) { 2042 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 2043 2044 if (copy_from_user(data, userbuf, eeprom.len)) { 2045 ret = -EFAULT; 2046 break; 2047 } 2048 ret = ops->set_eeprom(dev, &eeprom, data); 2049 if (ret) 2050 break; 2051 userbuf += eeprom.len; 2052 eeprom.offset += eeprom.len; 2053 bytes_remaining -= eeprom.len; 2054 } 2055 2056 kfree(data); 2057 return ret; 2058 } 2059 2060 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, 2061 void __user *useraddr) 2062 { 2063 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2064 struct kernel_ethtool_coalesce kernel_coalesce = {}; 2065 int ret; 2066 2067 if (!dev->ethtool_ops->get_coalesce) 2068 return -EOPNOTSUPP; 2069 2070 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, 2071 NULL); 2072 if (ret) 2073 return ret; 2074 2075 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2076 return -EFAULT; 2077 return 0; 2078 } 2079 2080 static bool 2081 ethtool_set_coalesce_supported(struct net_device *dev, 2082 struct ethtool_coalesce *coalesce) 2083 { 2084 u32 supported_params = dev->ethtool_ops->supported_coalesce_params; 2085 u32 nonzero_params = 0; 2086 2087 if (coalesce->rx_coalesce_usecs) 2088 nonzero_params |= ETHTOOL_COALESCE_RX_USECS; 2089 if (coalesce->rx_max_coalesced_frames) 2090 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES; 2091 if (coalesce->rx_coalesce_usecs_irq) 2092 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ; 2093 if (coalesce->rx_max_coalesced_frames_irq) 2094 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ; 2095 if (coalesce->tx_coalesce_usecs) 2096 nonzero_params |= ETHTOOL_COALESCE_TX_USECS; 2097 if (coalesce->tx_max_coalesced_frames) 2098 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES; 2099 if (coalesce->tx_coalesce_usecs_irq) 2100 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ; 2101 if (coalesce->tx_max_coalesced_frames_irq) 2102 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ; 2103 if (coalesce->stats_block_coalesce_usecs) 2104 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS; 2105 if (coalesce->use_adaptive_rx_coalesce) 2106 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX; 2107 if (coalesce->use_adaptive_tx_coalesce) 2108 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX; 2109 if (coalesce->pkt_rate_low) 2110 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW; 2111 if (coalesce->rx_coalesce_usecs_low) 2112 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW; 2113 if (coalesce->rx_max_coalesced_frames_low) 2114 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW; 2115 if (coalesce->tx_coalesce_usecs_low) 2116 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW; 2117 if (coalesce->tx_max_coalesced_frames_low) 2118 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW; 2119 if (coalesce->pkt_rate_high) 2120 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH; 2121 if (coalesce->rx_coalesce_usecs_high) 2122 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH; 2123 if (coalesce->rx_max_coalesced_frames_high) 2124 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH; 2125 if (coalesce->tx_coalesce_usecs_high) 2126 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH; 2127 if (coalesce->tx_max_coalesced_frames_high) 2128 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH; 2129 if (coalesce->rate_sample_interval) 2130 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL; 2131 2132 return (supported_params & nonzero_params) == nonzero_params; 2133 } 2134 2135 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, 2136 void __user *useraddr) 2137 { 2138 struct kernel_ethtool_coalesce kernel_coalesce = {}; 2139 struct ethtool_coalesce coalesce; 2140 int ret; 2141 2142 if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce) 2143 return -EOPNOTSUPP; 2144 2145 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce, 2146 NULL); 2147 if (ret) 2148 return ret; 2149 2150 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) 2151 return -EFAULT; 2152 2153 if (!ethtool_set_coalesce_supported(dev, &coalesce)) 2154 return -EOPNOTSUPP; 2155 2156 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce, 2157 NULL); 2158 if (!ret) 2159 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF); 2160 return ret; 2161 } 2162 2163 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) 2164 { 2165 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; 2166 struct kernel_ethtool_ringparam kernel_ringparam = {}; 2167 2168 if (!dev->ethtool_ops->get_ringparam) 2169 return -EOPNOTSUPP; 2170 2171 dev->ethtool_ops->get_ringparam(dev, &ringparam, 2172 &kernel_ringparam, NULL); 2173 2174 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) 2175 return -EFAULT; 2176 return 0; 2177 } 2178 2179 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) 2180 { 2181 struct kernel_ethtool_ringparam kernel_ringparam; 2182 struct ethtool_ringparam ringparam, max; 2183 int ret; 2184 2185 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) 2186 return -EOPNOTSUPP; 2187 2188 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) 2189 return -EFAULT; 2190 2191 ethtool_ringparam_get_cfg(dev, &max, &kernel_ringparam, NULL); 2192 2193 /* ensure new ring parameters are within the maximums */ 2194 if (ringparam.rx_pending > max.rx_max_pending || 2195 ringparam.rx_mini_pending > max.rx_mini_max_pending || 2196 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || 2197 ringparam.tx_pending > max.tx_max_pending) 2198 return -EINVAL; 2199 2200 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam, 2201 &kernel_ringparam, NULL); 2202 if (!ret) 2203 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF); 2204 return ret; 2205 } 2206 2207 static noinline_for_stack int ethtool_get_channels(struct net_device *dev, 2208 void __user *useraddr) 2209 { 2210 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; 2211 2212 if (!dev->ethtool_ops->get_channels) 2213 return -EOPNOTSUPP; 2214 2215 dev->ethtool_ops->get_channels(dev, &channels); 2216 2217 if (copy_to_user(useraddr, &channels, sizeof(channels))) 2218 return -EFAULT; 2219 return 0; 2220 } 2221 2222 static noinline_for_stack int ethtool_set_channels(struct net_device *dev, 2223 void __user *useraddr) 2224 { 2225 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS }; 2226 u16 from_channel, to_channel; 2227 unsigned int i; 2228 int ret; 2229 2230 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) 2231 return -EOPNOTSUPP; 2232 2233 if (copy_from_user(&channels, useraddr, sizeof(channels))) 2234 return -EFAULT; 2235 2236 dev->ethtool_ops->get_channels(dev, &curr); 2237 2238 if (channels.rx_count == curr.rx_count && 2239 channels.tx_count == curr.tx_count && 2240 channels.combined_count == curr.combined_count && 2241 channels.other_count == curr.other_count) 2242 return 0; 2243 2244 /* ensure new counts are within the maximums */ 2245 if (channels.rx_count > curr.max_rx || 2246 channels.tx_count > curr.max_tx || 2247 channels.combined_count > curr.max_combined || 2248 channels.other_count > curr.max_other) 2249 return -EINVAL; 2250 2251 /* ensure there is at least one RX and one TX channel */ 2252 if (!channels.combined_count && 2253 (!channels.rx_count || !channels.tx_count)) 2254 return -EINVAL; 2255 2256 ret = ethtool_check_max_channel(dev, channels, NULL); 2257 if (ret) 2258 return ret; 2259 2260 /* Disabling channels, query zero-copy AF_XDP sockets */ 2261 from_channel = channels.combined_count + 2262 min(channels.rx_count, channels.tx_count); 2263 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count); 2264 for (i = from_channel; i < to_channel; i++) 2265 if (xsk_get_pool_from_qid(dev, i)) 2266 return -EINVAL; 2267 2268 ret = dev->ethtool_ops->set_channels(dev, &channels); 2269 if (!ret) 2270 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF); 2271 return ret; 2272 } 2273 2274 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) 2275 { 2276 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM }; 2277 2278 if (!dev->ethtool_ops->get_pauseparam) 2279 return -EOPNOTSUPP; 2280 2281 dev->ethtool_ops->get_pauseparam(dev, &pauseparam); 2282 2283 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) 2284 return -EFAULT; 2285 return 0; 2286 } 2287 2288 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) 2289 { 2290 struct ethtool_pauseparam pauseparam; 2291 int ret; 2292 2293 if (!dev->ethtool_ops->set_pauseparam) 2294 return -EOPNOTSUPP; 2295 2296 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) 2297 return -EFAULT; 2298 2299 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam); 2300 if (!ret) 2301 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF); 2302 return ret; 2303 } 2304 2305 static int ethtool_self_test(struct net_device *dev, char __user *useraddr) 2306 { 2307 struct ethtool_test test; 2308 const struct ethtool_ops *ops = dev->ethtool_ops; 2309 u64 *data; 2310 int ret, test_len; 2311 2312 if (!ops->self_test || !ops->get_sset_count) 2313 return -EOPNOTSUPP; 2314 2315 test_len = ops->get_sset_count(dev, ETH_SS_TEST); 2316 if (test_len < 0) 2317 return test_len; 2318 WARN_ON(test_len == 0); 2319 2320 if (copy_from_user(&test, useraddr, sizeof(test))) 2321 return -EFAULT; 2322 2323 test.len = test_len; 2324 data = kcalloc(test_len, sizeof(u64), GFP_USER); 2325 if (!data) 2326 return -ENOMEM; 2327 2328 netif_testing_on(dev); 2329 ops->self_test(dev, &test, data); 2330 netif_testing_off(dev); 2331 2332 ret = -EFAULT; 2333 if (copy_to_user(useraddr, &test, sizeof(test))) 2334 goto out; 2335 useraddr += sizeof(test); 2336 if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64)))) 2337 goto out; 2338 ret = 0; 2339 2340 out: 2341 kfree(data); 2342 return ret; 2343 } 2344 2345 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) 2346 { 2347 struct ethtool_gstrings gstrings; 2348 u8 *data; 2349 int ret; 2350 2351 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) 2352 return -EFAULT; 2353 2354 ret = __ethtool_get_sset_count(dev, gstrings.string_set); 2355 if (ret < 0) 2356 return ret; 2357 if (ret > S32_MAX / ETH_GSTRING_LEN) 2358 return -ENOMEM; 2359 WARN_ON_ONCE(!ret); 2360 2361 gstrings.len = ret; 2362 2363 if (gstrings.len) { 2364 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); 2365 if (!data) 2366 return -ENOMEM; 2367 2368 __ethtool_get_strings(dev, gstrings.string_set, data); 2369 } else { 2370 data = NULL; 2371 } 2372 2373 ret = -EFAULT; 2374 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) 2375 goto out; 2376 useraddr += sizeof(gstrings); 2377 if (gstrings.len && 2378 copy_to_user(useraddr, data, 2379 array_size(gstrings.len, ETH_GSTRING_LEN))) 2380 goto out; 2381 ret = 0; 2382 2383 out: 2384 vfree(data); 2385 return ret; 2386 } 2387 2388 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...) 2389 { 2390 va_list args; 2391 2392 va_start(args, fmt); 2393 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args); 2394 va_end(args); 2395 2396 *data += ETH_GSTRING_LEN; 2397 } 2398 EXPORT_SYMBOL(ethtool_sprintf); 2399 2400 void ethtool_puts(u8 **data, const char *str) 2401 { 2402 strscpy(*data, str, ETH_GSTRING_LEN); 2403 *data += ETH_GSTRING_LEN; 2404 } 2405 EXPORT_SYMBOL(ethtool_puts); 2406 2407 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) 2408 { 2409 struct ethtool_value id; 2410 static bool busy; 2411 const struct ethtool_ops *ops = dev->ethtool_ops; 2412 netdevice_tracker dev_tracker; 2413 int rc; 2414 2415 if (!ops->set_phys_id) 2416 return -EOPNOTSUPP; 2417 2418 if (busy) 2419 return -EBUSY; 2420 2421 if (copy_from_user(&id, useraddr, sizeof(id))) 2422 return -EFAULT; 2423 2424 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); 2425 if (rc < 0) 2426 return rc; 2427 2428 /* Drop the RTNL lock while waiting, but prevent reentry or 2429 * removal of the device. 2430 */ 2431 busy = true; 2432 netdev_hold(dev, &dev_tracker, GFP_KERNEL); 2433 netdev_unlock_ops(dev); 2434 rtnl_unlock(); 2435 2436 if (rc == 0) { 2437 /* Driver will handle this itself */ 2438 schedule_timeout_interruptible( 2439 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 2440 } else { 2441 /* Driver expects to be called at twice the frequency in rc */ 2442 int n = rc * 2, interval = HZ / n; 2443 u64 count = mul_u32_u32(n, id.data); 2444 u64 i = 0; 2445 2446 do { 2447 rtnl_lock(); 2448 netdev_lock_ops(dev); 2449 rc = ops->set_phys_id(dev, 2450 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 2451 netdev_unlock_ops(dev); 2452 rtnl_unlock(); 2453 if (rc) 2454 break; 2455 schedule_timeout_interruptible(interval); 2456 } while (!signal_pending(current) && (!id.data || i < count)); 2457 } 2458 2459 rtnl_lock(); 2460 netdev_lock_ops(dev); 2461 netdev_put(dev, &dev_tracker); 2462 busy = false; 2463 2464 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 2465 return rc; 2466 } 2467 2468 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 2469 { 2470 struct ethtool_stats stats; 2471 const struct ethtool_ops *ops = dev->ethtool_ops; 2472 u64 *data; 2473 int ret, n_stats; 2474 2475 if (!ops->get_ethtool_stats || !ops->get_sset_count) 2476 return -EOPNOTSUPP; 2477 2478 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 2479 if (n_stats < 0) 2480 return n_stats; 2481 if (n_stats > S32_MAX / sizeof(u64)) 2482 return -ENOMEM; 2483 WARN_ON_ONCE(!n_stats); 2484 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2485 return -EFAULT; 2486 2487 stats.n_stats = n_stats; 2488 2489 if (n_stats) { 2490 data = vzalloc(array_size(n_stats, sizeof(u64))); 2491 if (!data) 2492 return -ENOMEM; 2493 ops->get_ethtool_stats(dev, &stats, data); 2494 } else { 2495 data = NULL; 2496 } 2497 2498 ret = -EFAULT; 2499 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2500 goto out; 2501 useraddr += sizeof(stats); 2502 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2503 goto out; 2504 ret = 0; 2505 2506 out: 2507 vfree(data); 2508 return ret; 2509 } 2510 2511 static int ethtool_vzalloc_stats_array(int n_stats, u64 **data) 2512 { 2513 if (n_stats < 0) 2514 return n_stats; 2515 if (n_stats > S32_MAX / sizeof(u64)) 2516 return -ENOMEM; 2517 if (WARN_ON_ONCE(!n_stats)) 2518 return -EOPNOTSUPP; 2519 2520 *data = vzalloc(array_size(n_stats, sizeof(u64))); 2521 if (!*data) 2522 return -ENOMEM; 2523 2524 return 0; 2525 } 2526 2527 static int ethtool_get_phy_stats_phydev(struct phy_device *phydev, 2528 struct ethtool_stats *stats, 2529 u64 **data) 2530 { 2531 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 2532 int n_stats, ret; 2533 2534 if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats) 2535 return -EOPNOTSUPP; 2536 2537 n_stats = phy_ops->get_sset_count(phydev); 2538 2539 ret = ethtool_vzalloc_stats_array(n_stats, data); 2540 if (ret) 2541 return ret; 2542 2543 stats->n_stats = n_stats; 2544 return phy_ops->get_stats(phydev, stats, *data); 2545 } 2546 2547 static int ethtool_get_phy_stats_ethtool(struct net_device *dev, 2548 struct ethtool_stats *stats, 2549 u64 **data) 2550 { 2551 const struct ethtool_ops *ops = dev->ethtool_ops; 2552 int n_stats, ret; 2553 2554 if (!ops || !ops->get_sset_count || !ops->get_ethtool_phy_stats) 2555 return -EOPNOTSUPP; 2556 2557 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 2558 2559 ret = ethtool_vzalloc_stats_array(n_stats, data); 2560 if (ret) 2561 return ret; 2562 2563 stats->n_stats = n_stats; 2564 ops->get_ethtool_phy_stats(dev, stats, *data); 2565 2566 return 0; 2567 } 2568 2569 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 2570 { 2571 struct phy_device *phydev = dev->phydev; 2572 struct ethtool_stats stats; 2573 u64 *data = NULL; 2574 int ret = -EOPNOTSUPP; 2575 2576 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2577 return -EFAULT; 2578 2579 if (phydev) 2580 ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data); 2581 2582 if (ret == -EOPNOTSUPP) 2583 ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data); 2584 2585 if (ret) 2586 goto out; 2587 2588 if (copy_to_user(useraddr, &stats, sizeof(stats))) { 2589 ret = -EFAULT; 2590 goto out; 2591 } 2592 2593 useraddr += sizeof(stats); 2594 if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64)))) 2595 ret = -EFAULT; 2596 2597 out: 2598 vfree(data); 2599 return ret; 2600 } 2601 2602 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2603 { 2604 struct ethtool_perm_addr epaddr; 2605 2606 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2607 return -EFAULT; 2608 2609 if (epaddr.size < dev->addr_len) 2610 return -ETOOSMALL; 2611 epaddr.size = dev->addr_len; 2612 2613 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2614 return -EFAULT; 2615 useraddr += sizeof(epaddr); 2616 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2617 return -EFAULT; 2618 return 0; 2619 } 2620 2621 static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2622 u32 cmd, u32 (*actor)(struct net_device *)) 2623 { 2624 struct ethtool_value edata = { .cmd = cmd }; 2625 2626 if (!actor) 2627 return -EOPNOTSUPP; 2628 2629 edata.data = actor(dev); 2630 2631 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2632 return -EFAULT; 2633 return 0; 2634 } 2635 2636 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2637 void (*actor)(struct net_device *, u32)) 2638 { 2639 struct ethtool_value edata; 2640 2641 if (!actor) 2642 return -EOPNOTSUPP; 2643 2644 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2645 return -EFAULT; 2646 2647 actor(dev, edata.data); 2648 return 0; 2649 } 2650 2651 static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2652 int (*actor)(struct net_device *, u32)) 2653 { 2654 struct ethtool_value edata; 2655 2656 if (!actor) 2657 return -EOPNOTSUPP; 2658 2659 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2660 return -EFAULT; 2661 2662 return actor(dev, edata.data); 2663 } 2664 2665 static int 2666 ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req) 2667 { 2668 if (!dev->ethtool_ops->flash_device) { 2669 req->devlink = netdev_to_devlink_get(dev); 2670 return 0; 2671 } 2672 2673 return dev->ethtool_ops->flash_device(dev, &req->efl); 2674 } 2675 2676 static int ethtool_set_dump(struct net_device *dev, 2677 void __user *useraddr) 2678 { 2679 struct ethtool_dump dump; 2680 2681 if (!dev->ethtool_ops->set_dump) 2682 return -EOPNOTSUPP; 2683 2684 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2685 return -EFAULT; 2686 2687 return dev->ethtool_ops->set_dump(dev, &dump); 2688 } 2689 2690 static int ethtool_get_dump_flag(struct net_device *dev, 2691 void __user *useraddr) 2692 { 2693 int ret; 2694 struct ethtool_dump dump; 2695 const struct ethtool_ops *ops = dev->ethtool_ops; 2696 2697 if (!ops->get_dump_flag) 2698 return -EOPNOTSUPP; 2699 2700 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2701 return -EFAULT; 2702 2703 ret = ops->get_dump_flag(dev, &dump); 2704 if (ret) 2705 return ret; 2706 2707 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2708 return -EFAULT; 2709 return 0; 2710 } 2711 2712 static int ethtool_get_dump_data(struct net_device *dev, 2713 void __user *useraddr) 2714 { 2715 int ret; 2716 __u32 len; 2717 struct ethtool_dump dump, tmp; 2718 const struct ethtool_ops *ops = dev->ethtool_ops; 2719 void *data = NULL; 2720 2721 if (!ops->get_dump_data || !ops->get_dump_flag) 2722 return -EOPNOTSUPP; 2723 2724 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2725 return -EFAULT; 2726 2727 memset(&tmp, 0, sizeof(tmp)); 2728 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2729 ret = ops->get_dump_flag(dev, &tmp); 2730 if (ret) 2731 return ret; 2732 2733 len = min(tmp.len, dump.len); 2734 if (!len) 2735 return -EFAULT; 2736 2737 /* Don't ever let the driver think there's more space available 2738 * than it requested with .get_dump_flag(). 2739 */ 2740 dump.len = len; 2741 2742 /* Always allocate enough space to hold the whole thing so that the 2743 * driver does not need to check the length and bother with partial 2744 * dumping. 2745 */ 2746 data = vzalloc(tmp.len); 2747 if (!data) 2748 return -ENOMEM; 2749 ret = ops->get_dump_data(dev, &dump, data); 2750 if (ret) 2751 goto out; 2752 2753 /* There are two sane possibilities: 2754 * 1. The driver's .get_dump_data() does not touch dump.len. 2755 * 2. Or it may set dump.len to how much it really writes, which 2756 * should be tmp.len (or len if it can do a partial dump). 2757 * In any case respond to userspace with the actual length of data 2758 * it's receiving. 2759 */ 2760 WARN_ON(dump.len != len && dump.len != tmp.len); 2761 dump.len = len; 2762 2763 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2764 ret = -EFAULT; 2765 goto out; 2766 } 2767 useraddr += offsetof(struct ethtool_dump, data); 2768 if (copy_to_user(useraddr, data, len)) 2769 ret = -EFAULT; 2770 out: 2771 vfree(data); 2772 return ret; 2773 } 2774 2775 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2776 { 2777 struct kernel_ethtool_ts_info kernel_info; 2778 struct ethtool_ts_info info = {}; 2779 int err; 2780 2781 err = __ethtool_get_ts_info(dev, &kernel_info); 2782 if (err) 2783 return err; 2784 2785 info.cmd = kernel_info.cmd; 2786 info.so_timestamping = kernel_info.so_timestamping; 2787 info.phc_index = kernel_info.phc_index; 2788 info.tx_types = kernel_info.tx_types; 2789 info.rx_filters = kernel_info.rx_filters; 2790 2791 if (copy_to_user(useraddr, &info, sizeof(info))) 2792 return -EFAULT; 2793 2794 return 0; 2795 } 2796 2797 int ethtool_get_module_info_call(struct net_device *dev, 2798 struct ethtool_modinfo *modinfo) 2799 { 2800 const struct ethtool_ops *ops = dev->ethtool_ops; 2801 struct phy_device *phydev = dev->phydev; 2802 2803 if (dev->ethtool->module_fw_flash_in_progress) 2804 return -EBUSY; 2805 2806 if (dev->sfp_bus) 2807 return sfp_get_module_info(dev->sfp_bus, modinfo); 2808 2809 if (phydev && phydev->drv && phydev->drv->module_info) 2810 return phydev->drv->module_info(phydev, modinfo); 2811 2812 if (ops->get_module_info) 2813 return ops->get_module_info(dev, modinfo); 2814 2815 return -EOPNOTSUPP; 2816 } 2817 2818 static int ethtool_get_module_info(struct net_device *dev, 2819 void __user *useraddr) 2820 { 2821 int ret; 2822 struct ethtool_modinfo modinfo; 2823 2824 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2825 return -EFAULT; 2826 2827 ret = ethtool_get_module_info_call(dev, &modinfo); 2828 if (ret) 2829 return ret; 2830 2831 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2832 return -EFAULT; 2833 2834 return 0; 2835 } 2836 2837 int ethtool_get_module_eeprom_call(struct net_device *dev, 2838 struct ethtool_eeprom *ee, u8 *data) 2839 { 2840 const struct ethtool_ops *ops = dev->ethtool_ops; 2841 struct phy_device *phydev = dev->phydev; 2842 2843 if (dev->ethtool->module_fw_flash_in_progress) 2844 return -EBUSY; 2845 2846 if (dev->sfp_bus) 2847 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2848 2849 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2850 return phydev->drv->module_eeprom(phydev, ee, data); 2851 2852 if (ops->get_module_eeprom) 2853 return ops->get_module_eeprom(dev, ee, data); 2854 2855 return -EOPNOTSUPP; 2856 } 2857 2858 static int ethtool_get_module_eeprom(struct net_device *dev, 2859 void __user *useraddr) 2860 { 2861 int ret; 2862 struct ethtool_modinfo modinfo; 2863 2864 ret = ethtool_get_module_info_call(dev, &modinfo); 2865 if (ret) 2866 return ret; 2867 2868 return ethtool_get_any_eeprom(dev, useraddr, 2869 ethtool_get_module_eeprom_call, 2870 modinfo.eeprom_len); 2871 } 2872 2873 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2874 { 2875 switch (tuna->id) { 2876 case ETHTOOL_RX_COPYBREAK: 2877 case ETHTOOL_TX_COPYBREAK: 2878 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 2879 if (tuna->len != sizeof(u32) || 2880 tuna->type_id != ETHTOOL_TUNABLE_U32) 2881 return -EINVAL; 2882 break; 2883 case ETHTOOL_PFC_PREVENTION_TOUT: 2884 if (tuna->len != sizeof(u16) || 2885 tuna->type_id != ETHTOOL_TUNABLE_U16) 2886 return -EINVAL; 2887 break; 2888 default: 2889 return -EINVAL; 2890 } 2891 2892 return 0; 2893 } 2894 2895 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2896 { 2897 int ret; 2898 struct ethtool_tunable tuna; 2899 const struct ethtool_ops *ops = dev->ethtool_ops; 2900 void *data; 2901 2902 if (!ops->get_tunable) 2903 return -EOPNOTSUPP; 2904 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2905 return -EFAULT; 2906 ret = ethtool_tunable_valid(&tuna); 2907 if (ret) 2908 return ret; 2909 data = kzalloc(tuna.len, GFP_USER); 2910 if (!data) 2911 return -ENOMEM; 2912 ret = ops->get_tunable(dev, &tuna, data); 2913 if (ret) 2914 goto out; 2915 useraddr += sizeof(tuna); 2916 ret = -EFAULT; 2917 if (copy_to_user(useraddr, data, tuna.len)) 2918 goto out; 2919 ret = 0; 2920 2921 out: 2922 kfree(data); 2923 return ret; 2924 } 2925 2926 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2927 { 2928 int ret; 2929 struct ethtool_tunable tuna; 2930 const struct ethtool_ops *ops = dev->ethtool_ops; 2931 void *data; 2932 2933 if (!ops->set_tunable) 2934 return -EOPNOTSUPP; 2935 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2936 return -EFAULT; 2937 ret = ethtool_tunable_valid(&tuna); 2938 if (ret) 2939 return ret; 2940 useraddr += sizeof(tuna); 2941 data = memdup_user(useraddr, tuna.len); 2942 if (IS_ERR(data)) 2943 return PTR_ERR(data); 2944 ret = ops->set_tunable(dev, &tuna, data); 2945 2946 kfree(data); 2947 return ret; 2948 } 2949 2950 static noinline_for_stack int 2951 ethtool_get_per_queue_coalesce(struct net_device *dev, 2952 void __user *useraddr, 2953 struct ethtool_per_queue_op *per_queue_opt) 2954 { 2955 u32 bit; 2956 int ret; 2957 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2958 2959 if (!dev->ethtool_ops->get_per_queue_coalesce) 2960 return -EOPNOTSUPP; 2961 2962 useraddr += sizeof(*per_queue_opt); 2963 2964 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 2965 MAX_NUM_QUEUE); 2966 2967 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2968 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2969 2970 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 2971 if (ret != 0) 2972 return ret; 2973 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2974 return -EFAULT; 2975 useraddr += sizeof(coalesce); 2976 } 2977 2978 return 0; 2979 } 2980 2981 static noinline_for_stack int 2982 ethtool_set_per_queue_coalesce(struct net_device *dev, 2983 void __user *useraddr, 2984 struct ethtool_per_queue_op *per_queue_opt) 2985 { 2986 u32 bit; 2987 int i, ret = 0; 2988 int n_queue; 2989 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 2990 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2991 2992 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 2993 (!dev->ethtool_ops->get_per_queue_coalesce)) 2994 return -EOPNOTSUPP; 2995 2996 useraddr += sizeof(*per_queue_opt); 2997 2998 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 2999 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 3000 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); 3001 if (!backup) 3002 return -ENOMEM; 3003 3004 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 3005 struct ethtool_coalesce coalesce; 3006 3007 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 3008 if (ret != 0) 3009 goto roll_back; 3010 3011 tmp++; 3012 3013 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 3014 ret = -EFAULT; 3015 goto roll_back; 3016 } 3017 3018 if (!ethtool_set_coalesce_supported(dev, &coalesce)) { 3019 ret = -EOPNOTSUPP; 3020 goto roll_back; 3021 } 3022 3023 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 3024 if (ret != 0) 3025 goto roll_back; 3026 3027 useraddr += sizeof(coalesce); 3028 } 3029 3030 roll_back: 3031 if (ret != 0) { 3032 tmp = backup; 3033 for_each_set_bit(i, queue_mask, bit) { 3034 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 3035 tmp++; 3036 } 3037 } 3038 kfree(backup); 3039 3040 return ret; 3041 } 3042 3043 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, 3044 void __user *useraddr, u32 sub_cmd) 3045 { 3046 struct ethtool_per_queue_op per_queue_opt; 3047 3048 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 3049 return -EFAULT; 3050 3051 if (per_queue_opt.sub_command != sub_cmd) 3052 return -EINVAL; 3053 3054 switch (per_queue_opt.sub_command) { 3055 case ETHTOOL_GCOALESCE: 3056 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 3057 case ETHTOOL_SCOALESCE: 3058 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 3059 default: 3060 return -EOPNOTSUPP; 3061 } 3062 } 3063 3064 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 3065 { 3066 switch (tuna->id) { 3067 case ETHTOOL_PHY_DOWNSHIFT: 3068 case ETHTOOL_PHY_FAST_LINK_DOWN: 3069 if (tuna->len != sizeof(u8) || 3070 tuna->type_id != ETHTOOL_TUNABLE_U8) 3071 return -EINVAL; 3072 break; 3073 case ETHTOOL_PHY_EDPD: 3074 if (tuna->len != sizeof(u16) || 3075 tuna->type_id != ETHTOOL_TUNABLE_U16) 3076 return -EINVAL; 3077 break; 3078 default: 3079 return -EINVAL; 3080 } 3081 3082 return 0; 3083 } 3084 3085 static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 3086 { 3087 struct phy_device *phydev = dev->phydev; 3088 struct ethtool_tunable tuna; 3089 bool phy_drv_tunable; 3090 void *data; 3091 int ret; 3092 3093 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 3094 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) 3095 return -EOPNOTSUPP; 3096 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 3097 return -EFAULT; 3098 ret = ethtool_phy_tunable_valid(&tuna); 3099 if (ret) 3100 return ret; 3101 data = kzalloc(tuna.len, GFP_USER); 3102 if (!data) 3103 return -ENOMEM; 3104 if (phy_drv_tunable) { 3105 mutex_lock(&phydev->lock); 3106 ret = phydev->drv->get_tunable(phydev, &tuna, data); 3107 mutex_unlock(&phydev->lock); 3108 } else { 3109 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); 3110 } 3111 if (ret) 3112 goto out; 3113 useraddr += sizeof(tuna); 3114 ret = -EFAULT; 3115 if (copy_to_user(useraddr, data, tuna.len)) 3116 goto out; 3117 ret = 0; 3118 3119 out: 3120 kfree(data); 3121 return ret; 3122 } 3123 3124 static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 3125 { 3126 struct phy_device *phydev = dev->phydev; 3127 struct ethtool_tunable tuna; 3128 bool phy_drv_tunable; 3129 void *data; 3130 int ret; 3131 3132 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 3133 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) 3134 return -EOPNOTSUPP; 3135 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 3136 return -EFAULT; 3137 ret = ethtool_phy_tunable_valid(&tuna); 3138 if (ret) 3139 return ret; 3140 useraddr += sizeof(tuna); 3141 data = memdup_user(useraddr, tuna.len); 3142 if (IS_ERR(data)) 3143 return PTR_ERR(data); 3144 if (phy_drv_tunable) { 3145 mutex_lock(&phydev->lock); 3146 ret = phydev->drv->set_tunable(phydev, &tuna, data); 3147 mutex_unlock(&phydev->lock); 3148 } else { 3149 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); 3150 } 3151 3152 kfree(data); 3153 return ret; 3154 } 3155 3156 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 3157 { 3158 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; 3159 int rc; 3160 3161 if (!dev->ethtool_ops->get_fecparam) 3162 return -EOPNOTSUPP; 3163 3164 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 3165 if (rc) 3166 return rc; 3167 3168 if (WARN_ON_ONCE(fecparam.reserved)) 3169 fecparam.reserved = 0; 3170 3171 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 3172 return -EFAULT; 3173 return 0; 3174 } 3175 3176 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 3177 { 3178 struct ethtool_fecparam fecparam; 3179 3180 if (!dev->ethtool_ops->set_fecparam) 3181 return -EOPNOTSUPP; 3182 3183 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 3184 return -EFAULT; 3185 3186 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) 3187 return -EINVAL; 3188 3189 fecparam.active_fec = 0; 3190 fecparam.reserved = 0; 3191 3192 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 3193 } 3194 3195 /* The main entry point in this file. Called from net/core/dev_ioctl.c */ 3196 3197 static int 3198 __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr, 3199 u32 ethcmd, struct ethtool_devlink_compat *devlink_state) 3200 { 3201 struct net_device *dev; 3202 u32 sub_cmd; 3203 int rc; 3204 netdev_features_t old_features; 3205 3206 dev = __dev_get_by_name(net, ifr->ifr_name); 3207 if (!dev) 3208 return -ENODEV; 3209 3210 if (ethcmd == ETHTOOL_PERQUEUE) { 3211 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 3212 return -EFAULT; 3213 } else { 3214 sub_cmd = ethcmd; 3215 } 3216 /* Allow some commands to be done by anyone */ 3217 switch (sub_cmd) { 3218 case ETHTOOL_GSET: 3219 case ETHTOOL_GDRVINFO: 3220 case ETHTOOL_GMSGLVL: 3221 case ETHTOOL_GLINK: 3222 case ETHTOOL_GCOALESCE: 3223 case ETHTOOL_GRINGPARAM: 3224 case ETHTOOL_GPAUSEPARAM: 3225 case ETHTOOL_GRXCSUM: 3226 case ETHTOOL_GTXCSUM: 3227 case ETHTOOL_GSG: 3228 case ETHTOOL_GSSET_INFO: 3229 case ETHTOOL_GSTRINGS: 3230 case ETHTOOL_GSTATS: 3231 case ETHTOOL_GPHYSTATS: 3232 case ETHTOOL_GTSO: 3233 case ETHTOOL_GPERMADDR: 3234 case ETHTOOL_GUFO: 3235 case ETHTOOL_GGSO: 3236 case ETHTOOL_GGRO: 3237 case ETHTOOL_GFLAGS: 3238 case ETHTOOL_GPFLAGS: 3239 case ETHTOOL_GRXFH: 3240 case ETHTOOL_GRXRINGS: 3241 case ETHTOOL_GRXCLSRLCNT: 3242 case ETHTOOL_GRXCLSRULE: 3243 case ETHTOOL_GRXCLSRLALL: 3244 case ETHTOOL_GRXFHINDIR: 3245 case ETHTOOL_GRSSH: 3246 case ETHTOOL_GFEATURES: 3247 case ETHTOOL_GCHANNELS: 3248 case ETHTOOL_GET_TS_INFO: 3249 case ETHTOOL_GEEE: 3250 case ETHTOOL_GTUNABLE: 3251 case ETHTOOL_PHY_GTUNABLE: 3252 case ETHTOOL_GLINKSETTINGS: 3253 case ETHTOOL_GFECPARAM: 3254 break; 3255 default: 3256 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3257 return -EPERM; 3258 } 3259 3260 netdev_lock_ops(dev); 3261 if (dev->dev.parent) 3262 pm_runtime_get_sync(dev->dev.parent); 3263 3264 if (!netif_device_present(dev)) { 3265 rc = -ENODEV; 3266 goto out; 3267 } 3268 3269 if (dev->ethtool_ops->begin) { 3270 rc = dev->ethtool_ops->begin(dev); 3271 if (rc < 0) 3272 goto out; 3273 } 3274 old_features = dev->features; 3275 3276 switch (ethcmd) { 3277 case ETHTOOL_GSET: 3278 rc = ethtool_get_settings(dev, useraddr); 3279 break; 3280 case ETHTOOL_SSET: 3281 rc = ethtool_set_settings(dev, useraddr); 3282 break; 3283 case ETHTOOL_GDRVINFO: 3284 rc = ethtool_get_drvinfo(dev, devlink_state); 3285 break; 3286 case ETHTOOL_GREGS: 3287 rc = ethtool_get_regs(dev, useraddr); 3288 break; 3289 case ETHTOOL_GWOL: 3290 rc = ethtool_get_wol(dev, useraddr); 3291 break; 3292 case ETHTOOL_SWOL: 3293 rc = ethtool_set_wol(dev, useraddr); 3294 break; 3295 case ETHTOOL_GMSGLVL: 3296 rc = ethtool_get_value(dev, useraddr, ethcmd, 3297 dev->ethtool_ops->get_msglevel); 3298 break; 3299 case ETHTOOL_SMSGLVL: 3300 rc = ethtool_set_value_void(dev, useraddr, 3301 dev->ethtool_ops->set_msglevel); 3302 if (!rc) 3303 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF); 3304 break; 3305 case ETHTOOL_GEEE: 3306 rc = ethtool_get_eee(dev, useraddr); 3307 break; 3308 case ETHTOOL_SEEE: 3309 rc = ethtool_set_eee(dev, useraddr); 3310 break; 3311 case ETHTOOL_NWAY_RST: 3312 rc = ethtool_nway_reset(dev); 3313 break; 3314 case ETHTOOL_GLINK: 3315 rc = ethtool_get_link(dev, useraddr); 3316 break; 3317 case ETHTOOL_GEEPROM: 3318 rc = ethtool_get_eeprom(dev, useraddr); 3319 break; 3320 case ETHTOOL_SEEPROM: 3321 rc = ethtool_set_eeprom(dev, useraddr); 3322 break; 3323 case ETHTOOL_GCOALESCE: 3324 rc = ethtool_get_coalesce(dev, useraddr); 3325 break; 3326 case ETHTOOL_SCOALESCE: 3327 rc = ethtool_set_coalesce(dev, useraddr); 3328 break; 3329 case ETHTOOL_GRINGPARAM: 3330 rc = ethtool_get_ringparam(dev, useraddr); 3331 break; 3332 case ETHTOOL_SRINGPARAM: 3333 rc = ethtool_set_ringparam(dev, useraddr); 3334 break; 3335 case ETHTOOL_GPAUSEPARAM: 3336 rc = ethtool_get_pauseparam(dev, useraddr); 3337 break; 3338 case ETHTOOL_SPAUSEPARAM: 3339 rc = ethtool_set_pauseparam(dev, useraddr); 3340 break; 3341 case ETHTOOL_TEST: 3342 rc = ethtool_self_test(dev, useraddr); 3343 break; 3344 case ETHTOOL_GSTRINGS: 3345 rc = ethtool_get_strings(dev, useraddr); 3346 break; 3347 case ETHTOOL_PHYS_ID: 3348 rc = ethtool_phys_id(dev, useraddr); 3349 break; 3350 case ETHTOOL_GSTATS: 3351 rc = ethtool_get_stats(dev, useraddr); 3352 break; 3353 case ETHTOOL_GPERMADDR: 3354 rc = ethtool_get_perm_addr(dev, useraddr); 3355 break; 3356 case ETHTOOL_GFLAGS: 3357 rc = ethtool_get_value(dev, useraddr, ethcmd, 3358 __ethtool_get_flags); 3359 break; 3360 case ETHTOOL_SFLAGS: 3361 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 3362 break; 3363 case ETHTOOL_GPFLAGS: 3364 rc = ethtool_get_value(dev, useraddr, ethcmd, 3365 dev->ethtool_ops->get_priv_flags); 3366 if (!rc) 3367 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF); 3368 break; 3369 case ETHTOOL_SPFLAGS: 3370 rc = ethtool_set_value(dev, useraddr, 3371 dev->ethtool_ops->set_priv_flags); 3372 break; 3373 case ETHTOOL_GRXFH: 3374 rc = ethtool_get_rxfh_fields(dev, ethcmd, useraddr); 3375 break; 3376 case ETHTOOL_SRXFH: 3377 rc = ethtool_set_rxfh_fields(dev, ethcmd, useraddr); 3378 break; 3379 case ETHTOOL_GRXRINGS: 3380 case ETHTOOL_GRXCLSRLCNT: 3381 case ETHTOOL_GRXCLSRULE: 3382 case ETHTOOL_GRXCLSRLALL: 3383 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 3384 break; 3385 case ETHTOOL_SRXCLSRLDEL: 3386 case ETHTOOL_SRXCLSRLINS: 3387 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 3388 break; 3389 case ETHTOOL_FLASHDEV: 3390 rc = ethtool_flash_device(dev, devlink_state); 3391 break; 3392 case ETHTOOL_RESET: 3393 rc = ethtool_reset(dev, useraddr); 3394 break; 3395 case ETHTOOL_GSSET_INFO: 3396 rc = ethtool_get_sset_info(dev, useraddr); 3397 break; 3398 case ETHTOOL_GRXFHINDIR: 3399 rc = ethtool_get_rxfh_indir(dev, useraddr); 3400 break; 3401 case ETHTOOL_SRXFHINDIR: 3402 rc = ethtool_set_rxfh_indir(dev, useraddr); 3403 break; 3404 case ETHTOOL_GRSSH: 3405 rc = ethtool_get_rxfh(dev, useraddr); 3406 break; 3407 case ETHTOOL_SRSSH: 3408 rc = ethtool_set_rxfh(dev, useraddr); 3409 break; 3410 case ETHTOOL_GFEATURES: 3411 rc = ethtool_get_features(dev, useraddr); 3412 break; 3413 case ETHTOOL_SFEATURES: 3414 rc = ethtool_set_features(dev, useraddr); 3415 break; 3416 case ETHTOOL_GTXCSUM: 3417 case ETHTOOL_GRXCSUM: 3418 case ETHTOOL_GSG: 3419 case ETHTOOL_GTSO: 3420 case ETHTOOL_GGSO: 3421 case ETHTOOL_GGRO: 3422 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 3423 break; 3424 case ETHTOOL_STXCSUM: 3425 case ETHTOOL_SRXCSUM: 3426 case ETHTOOL_SSG: 3427 case ETHTOOL_STSO: 3428 case ETHTOOL_SGSO: 3429 case ETHTOOL_SGRO: 3430 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 3431 break; 3432 case ETHTOOL_GCHANNELS: 3433 rc = ethtool_get_channels(dev, useraddr); 3434 break; 3435 case ETHTOOL_SCHANNELS: 3436 rc = ethtool_set_channels(dev, useraddr); 3437 break; 3438 case ETHTOOL_SET_DUMP: 3439 rc = ethtool_set_dump(dev, useraddr); 3440 break; 3441 case ETHTOOL_GET_DUMP_FLAG: 3442 rc = ethtool_get_dump_flag(dev, useraddr); 3443 break; 3444 case ETHTOOL_GET_DUMP_DATA: 3445 rc = ethtool_get_dump_data(dev, useraddr); 3446 break; 3447 case ETHTOOL_GET_TS_INFO: 3448 rc = ethtool_get_ts_info(dev, useraddr); 3449 break; 3450 case ETHTOOL_GMODULEINFO: 3451 rc = ethtool_get_module_info(dev, useraddr); 3452 break; 3453 case ETHTOOL_GMODULEEEPROM: 3454 rc = ethtool_get_module_eeprom(dev, useraddr); 3455 break; 3456 case ETHTOOL_GTUNABLE: 3457 rc = ethtool_get_tunable(dev, useraddr); 3458 break; 3459 case ETHTOOL_STUNABLE: 3460 rc = ethtool_set_tunable(dev, useraddr); 3461 break; 3462 case ETHTOOL_GPHYSTATS: 3463 rc = ethtool_get_phy_stats(dev, useraddr); 3464 break; 3465 case ETHTOOL_PERQUEUE: 3466 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); 3467 break; 3468 case ETHTOOL_GLINKSETTINGS: 3469 rc = ethtool_get_link_ksettings(dev, useraddr); 3470 break; 3471 case ETHTOOL_SLINKSETTINGS: 3472 rc = ethtool_set_link_ksettings(dev, useraddr); 3473 break; 3474 case ETHTOOL_PHY_GTUNABLE: 3475 rc = get_phy_tunable(dev, useraddr); 3476 break; 3477 case ETHTOOL_PHY_STUNABLE: 3478 rc = set_phy_tunable(dev, useraddr); 3479 break; 3480 case ETHTOOL_GFECPARAM: 3481 rc = ethtool_get_fecparam(dev, useraddr); 3482 break; 3483 case ETHTOOL_SFECPARAM: 3484 rc = ethtool_set_fecparam(dev, useraddr); 3485 break; 3486 default: 3487 rc = -EOPNOTSUPP; 3488 } 3489 3490 if (dev->ethtool_ops->complete) 3491 dev->ethtool_ops->complete(dev); 3492 3493 if (old_features != dev->features) 3494 netdev_features_change(dev); 3495 out: 3496 if (dev->dev.parent) 3497 pm_runtime_put(dev->dev.parent); 3498 netdev_unlock_ops(dev); 3499 3500 return rc; 3501 } 3502 3503 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) 3504 { 3505 struct ethtool_devlink_compat *state; 3506 u32 ethcmd; 3507 int rc; 3508 3509 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 3510 return -EFAULT; 3511 3512 state = kzalloc(sizeof(*state), GFP_KERNEL); 3513 if (!state) 3514 return -ENOMEM; 3515 3516 switch (ethcmd) { 3517 case ETHTOOL_FLASHDEV: 3518 if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) { 3519 rc = -EFAULT; 3520 goto exit_free; 3521 } 3522 state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 3523 break; 3524 } 3525 3526 rtnl_lock(); 3527 rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state); 3528 rtnl_unlock(); 3529 if (rc) 3530 goto exit_free; 3531 3532 switch (ethcmd) { 3533 case ETHTOOL_FLASHDEV: 3534 if (state->devlink) 3535 rc = devlink_compat_flash_update(state->devlink, 3536 state->efl.data); 3537 break; 3538 case ETHTOOL_GDRVINFO: 3539 if (state->devlink) 3540 devlink_compat_running_version(state->devlink, 3541 state->info.fw_version, 3542 sizeof(state->info.fw_version)); 3543 if (copy_to_user(useraddr, &state->info, sizeof(state->info))) { 3544 rc = -EFAULT; 3545 goto exit_free; 3546 } 3547 break; 3548 } 3549 3550 exit_free: 3551 if (state->devlink) 3552 devlink_put(state->devlink); 3553 kfree(state); 3554 return rc; 3555 } 3556 3557 struct ethtool_rx_flow_key { 3558 struct flow_dissector_key_basic basic; 3559 union { 3560 struct flow_dissector_key_ipv4_addrs ipv4; 3561 struct flow_dissector_key_ipv6_addrs ipv6; 3562 }; 3563 struct flow_dissector_key_ports tp; 3564 struct flow_dissector_key_ip ip; 3565 struct flow_dissector_key_vlan vlan; 3566 struct flow_dissector_key_eth_addrs eth_addrs; 3567 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 3568 3569 struct ethtool_rx_flow_match { 3570 struct flow_dissector dissector; 3571 struct ethtool_rx_flow_key key; 3572 struct ethtool_rx_flow_key mask; 3573 }; 3574 3575 struct ethtool_rx_flow_rule * 3576 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) 3577 { 3578 const struct ethtool_rx_flow_spec *fs = input->fs; 3579 struct ethtool_rx_flow_match *match; 3580 struct ethtool_rx_flow_rule *flow; 3581 struct flow_action_entry *act; 3582 3583 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + 3584 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); 3585 if (!flow) 3586 return ERR_PTR(-ENOMEM); 3587 3588 /* ethtool_rx supports only one single action per rule. */ 3589 flow->rule = flow_rule_alloc(1); 3590 if (!flow->rule) { 3591 kfree(flow); 3592 return ERR_PTR(-ENOMEM); 3593 } 3594 3595 match = (struct ethtool_rx_flow_match *)flow->priv; 3596 flow->rule->match.dissector = &match->dissector; 3597 flow->rule->match.mask = &match->mask; 3598 flow->rule->match.key = &match->key; 3599 3600 match->mask.basic.n_proto = htons(0xffff); 3601 3602 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3603 case ETHER_FLOW: { 3604 const struct ethhdr *ether_spec, *ether_m_spec; 3605 3606 ether_spec = &fs->h_u.ether_spec; 3607 ether_m_spec = &fs->m_u.ether_spec; 3608 3609 if (!is_zero_ether_addr(ether_m_spec->h_source)) { 3610 ether_addr_copy(match->key.eth_addrs.src, 3611 ether_spec->h_source); 3612 ether_addr_copy(match->mask.eth_addrs.src, 3613 ether_m_spec->h_source); 3614 } 3615 if (!is_zero_ether_addr(ether_m_spec->h_dest)) { 3616 ether_addr_copy(match->key.eth_addrs.dst, 3617 ether_spec->h_dest); 3618 ether_addr_copy(match->mask.eth_addrs.dst, 3619 ether_m_spec->h_dest); 3620 } 3621 if (ether_m_spec->h_proto) { 3622 match->key.basic.n_proto = ether_spec->h_proto; 3623 match->mask.basic.n_proto = ether_m_spec->h_proto; 3624 } 3625 } 3626 break; 3627 case TCP_V4_FLOW: 3628 case UDP_V4_FLOW: { 3629 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; 3630 3631 match->key.basic.n_proto = htons(ETH_P_IP); 3632 3633 v4_spec = &fs->h_u.tcp_ip4_spec; 3634 v4_m_spec = &fs->m_u.tcp_ip4_spec; 3635 3636 if (v4_m_spec->ip4src) { 3637 match->key.ipv4.src = v4_spec->ip4src; 3638 match->mask.ipv4.src = v4_m_spec->ip4src; 3639 } 3640 if (v4_m_spec->ip4dst) { 3641 match->key.ipv4.dst = v4_spec->ip4dst; 3642 match->mask.ipv4.dst = v4_m_spec->ip4dst; 3643 } 3644 if (v4_m_spec->ip4src || 3645 v4_m_spec->ip4dst) { 3646 match->dissector.used_keys |= 3647 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS); 3648 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 3649 offsetof(struct ethtool_rx_flow_key, ipv4); 3650 } 3651 if (v4_m_spec->psrc) { 3652 match->key.tp.src = v4_spec->psrc; 3653 match->mask.tp.src = v4_m_spec->psrc; 3654 } 3655 if (v4_m_spec->pdst) { 3656 match->key.tp.dst = v4_spec->pdst; 3657 match->mask.tp.dst = v4_m_spec->pdst; 3658 } 3659 if (v4_m_spec->psrc || 3660 v4_m_spec->pdst) { 3661 match->dissector.used_keys |= 3662 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3663 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3664 offsetof(struct ethtool_rx_flow_key, tp); 3665 } 3666 if (v4_m_spec->tos) { 3667 match->key.ip.tos = v4_spec->tos; 3668 match->mask.ip.tos = v4_m_spec->tos; 3669 match->dissector.used_keys |= 3670 BIT(FLOW_DISSECTOR_KEY_IP); 3671 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3672 offsetof(struct ethtool_rx_flow_key, ip); 3673 } 3674 } 3675 break; 3676 case TCP_V6_FLOW: 3677 case UDP_V6_FLOW: { 3678 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; 3679 3680 match->key.basic.n_proto = htons(ETH_P_IPV6); 3681 3682 v6_spec = &fs->h_u.tcp_ip6_spec; 3683 v6_m_spec = &fs->m_u.tcp_ip6_spec; 3684 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) { 3685 memcpy(&match->key.ipv6.src, v6_spec->ip6src, 3686 sizeof(match->key.ipv6.src)); 3687 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, 3688 sizeof(match->mask.ipv6.src)); 3689 } 3690 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3691 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, 3692 sizeof(match->key.ipv6.dst)); 3693 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, 3694 sizeof(match->mask.ipv6.dst)); 3695 } 3696 if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) || 3697 !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) { 3698 match->dissector.used_keys |= 3699 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS); 3700 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = 3701 offsetof(struct ethtool_rx_flow_key, ipv6); 3702 } 3703 if (v6_m_spec->psrc) { 3704 match->key.tp.src = v6_spec->psrc; 3705 match->mask.tp.src = v6_m_spec->psrc; 3706 } 3707 if (v6_m_spec->pdst) { 3708 match->key.tp.dst = v6_spec->pdst; 3709 match->mask.tp.dst = v6_m_spec->pdst; 3710 } 3711 if (v6_m_spec->psrc || 3712 v6_m_spec->pdst) { 3713 match->dissector.used_keys |= 3714 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS); 3715 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3716 offsetof(struct ethtool_rx_flow_key, tp); 3717 } 3718 if (v6_m_spec->tclass) { 3719 match->key.ip.tos = v6_spec->tclass; 3720 match->mask.ip.tos = v6_m_spec->tclass; 3721 match->dissector.used_keys |= 3722 BIT_ULL(FLOW_DISSECTOR_KEY_IP); 3723 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3724 offsetof(struct ethtool_rx_flow_key, ip); 3725 } 3726 } 3727 break; 3728 default: 3729 ethtool_rx_flow_rule_destroy(flow); 3730 return ERR_PTR(-EINVAL); 3731 } 3732 3733 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3734 case TCP_V4_FLOW: 3735 case TCP_V6_FLOW: 3736 match->key.basic.ip_proto = IPPROTO_TCP; 3737 match->mask.basic.ip_proto = 0xff; 3738 break; 3739 case UDP_V4_FLOW: 3740 case UDP_V6_FLOW: 3741 match->key.basic.ip_proto = IPPROTO_UDP; 3742 match->mask.basic.ip_proto = 0xff; 3743 break; 3744 } 3745 3746 match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC); 3747 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = 3748 offsetof(struct ethtool_rx_flow_key, basic); 3749 3750 if (fs->flow_type & FLOW_EXT) { 3751 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3752 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3753 3754 if (ext_m_spec->vlan_etype) { 3755 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; 3756 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; 3757 } 3758 3759 if (ext_m_spec->vlan_tci) { 3760 match->key.vlan.vlan_id = 3761 ntohs(ext_h_spec->vlan_tci) & 0x0fff; 3762 match->mask.vlan.vlan_id = 3763 ntohs(ext_m_spec->vlan_tci) & 0x0fff; 3764 3765 match->key.vlan.vlan_dei = 3766 !!(ext_h_spec->vlan_tci & htons(0x1000)); 3767 match->mask.vlan.vlan_dei = 3768 !!(ext_m_spec->vlan_tci & htons(0x1000)); 3769 3770 match->key.vlan.vlan_priority = 3771 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; 3772 match->mask.vlan.vlan_priority = 3773 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; 3774 } 3775 3776 if (ext_m_spec->vlan_etype || 3777 ext_m_spec->vlan_tci) { 3778 match->dissector.used_keys |= 3779 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN); 3780 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = 3781 offsetof(struct ethtool_rx_flow_key, vlan); 3782 } 3783 } 3784 if (fs->flow_type & FLOW_MAC_EXT) { 3785 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3786 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3787 3788 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, 3789 ETH_ALEN); 3790 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, 3791 ETH_ALEN); 3792 3793 match->dissector.used_keys |= 3794 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS); 3795 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = 3796 offsetof(struct ethtool_rx_flow_key, eth_addrs); 3797 } 3798 3799 act = &flow->rule->action.entries[0]; 3800 switch (fs->ring_cookie) { 3801 case RX_CLS_FLOW_DISC: 3802 act->id = FLOW_ACTION_DROP; 3803 break; 3804 case RX_CLS_FLOW_WAKE: 3805 act->id = FLOW_ACTION_WAKE; 3806 break; 3807 default: 3808 act->id = FLOW_ACTION_QUEUE; 3809 if (fs->flow_type & FLOW_RSS) 3810 act->queue.ctx = input->rss_ctx; 3811 3812 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); 3813 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); 3814 break; 3815 } 3816 3817 return flow; 3818 } 3819 EXPORT_SYMBOL(ethtool_rx_flow_rule_create); 3820 3821 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) 3822 { 3823 kfree(flow->rule); 3824 kfree(flow); 3825 } 3826 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); 3827