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