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