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, copy_size * sizeof(*features))) 93 return -EFAULT; 94 95 return 0; 96 } 97 98 static int ethtool_set_features(struct net_device *dev, void __user *useraddr) 99 { 100 struct ethtool_sfeatures cmd; 101 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; 102 netdev_features_t wanted = 0, valid = 0; 103 int i, ret = 0; 104 105 if (copy_from_user(&cmd, useraddr, sizeof(cmd))) 106 return -EFAULT; 107 useraddr += sizeof(cmd); 108 109 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) 110 return -EINVAL; 111 112 if (copy_from_user(features, useraddr, sizeof(features))) 113 return -EFAULT; 114 115 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { 116 valid |= (netdev_features_t)features[i].valid << (32 * i); 117 wanted |= (netdev_features_t)features[i].requested << (32 * i); 118 } 119 120 if (valid & ~NETIF_F_ETHTOOL_BITS) 121 return -EINVAL; 122 123 if (valid & ~dev->hw_features) { 124 valid &= dev->hw_features; 125 ret |= ETHTOOL_F_UNSUPPORTED; 126 } 127 128 dev->wanted_features &= ~valid; 129 dev->wanted_features |= wanted & valid; 130 __netdev_update_features(dev); 131 132 if ((dev->wanted_features ^ dev->features) & valid) 133 ret |= ETHTOOL_F_WISH; 134 135 return ret; 136 } 137 138 static int __ethtool_get_sset_count(struct net_device *dev, int sset) 139 { 140 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 141 const struct ethtool_ops *ops = dev->ethtool_ops; 142 143 if (sset == ETH_SS_FEATURES) 144 return ARRAY_SIZE(netdev_features_strings); 145 146 if (sset == ETH_SS_RSS_HASH_FUNCS) 147 return ARRAY_SIZE(rss_hash_func_strings); 148 149 if (sset == ETH_SS_TUNABLES) 150 return ARRAY_SIZE(tunable_strings); 151 152 if (sset == ETH_SS_PHY_TUNABLES) 153 return ARRAY_SIZE(phy_tunable_strings); 154 155 if (sset == ETH_SS_PHY_STATS && dev->phydev && 156 !ops->get_ethtool_phy_stats && 157 phy_ops && phy_ops->get_sset_count) 158 return phy_ops->get_sset_count(dev->phydev); 159 160 if (sset == ETH_SS_LINK_MODES) 161 return __ETHTOOL_LINK_MODE_MASK_NBITS; 162 163 if (ops->get_sset_count && ops->get_strings) 164 return ops->get_sset_count(dev, sset); 165 else 166 return -EOPNOTSUPP; 167 } 168 169 static void __ethtool_get_strings(struct net_device *dev, 170 u32 stringset, u8 *data) 171 { 172 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 173 const struct ethtool_ops *ops = dev->ethtool_ops; 174 175 if (stringset == ETH_SS_FEATURES) 176 memcpy(data, netdev_features_strings, 177 sizeof(netdev_features_strings)); 178 else if (stringset == ETH_SS_RSS_HASH_FUNCS) 179 memcpy(data, rss_hash_func_strings, 180 sizeof(rss_hash_func_strings)); 181 else if (stringset == ETH_SS_TUNABLES) 182 memcpy(data, tunable_strings, sizeof(tunable_strings)); 183 else if (stringset == ETH_SS_PHY_TUNABLES) 184 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings)); 185 else if (stringset == ETH_SS_PHY_STATS && dev->phydev && 186 !ops->get_ethtool_phy_stats && phy_ops && 187 phy_ops->get_strings) 188 phy_ops->get_strings(dev->phydev, data); 189 else if (stringset == ETH_SS_LINK_MODES) 190 memcpy(data, link_mode_names, 191 __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN); 192 else 193 /* ops->get_strings is valid because checked earlier */ 194 ops->get_strings(dev, stringset, data); 195 } 196 197 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) 198 { 199 /* feature masks of legacy discrete ethtool ops */ 200 201 switch (eth_cmd) { 202 case ETHTOOL_GTXCSUM: 203 case ETHTOOL_STXCSUM: 204 return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC | 205 NETIF_F_SCTP_CRC; 206 case ETHTOOL_GRXCSUM: 207 case ETHTOOL_SRXCSUM: 208 return NETIF_F_RXCSUM; 209 case ETHTOOL_GSG: 210 case ETHTOOL_SSG: 211 return NETIF_F_SG | NETIF_F_FRAGLIST; 212 case ETHTOOL_GTSO: 213 case ETHTOOL_STSO: 214 return NETIF_F_ALL_TSO; 215 case ETHTOOL_GGSO: 216 case ETHTOOL_SGSO: 217 return NETIF_F_GSO; 218 case ETHTOOL_GGRO: 219 case ETHTOOL_SGRO: 220 return NETIF_F_GRO; 221 default: 222 BUG(); 223 } 224 } 225 226 static int ethtool_get_one_feature(struct net_device *dev, 227 char __user *useraddr, u32 ethcmd) 228 { 229 netdev_features_t mask = ethtool_get_feature_mask(ethcmd); 230 struct ethtool_value edata = { 231 .cmd = ethcmd, 232 .data = !!(dev->features & mask), 233 }; 234 235 if (copy_to_user(useraddr, &edata, sizeof(edata))) 236 return -EFAULT; 237 return 0; 238 } 239 240 static int ethtool_set_one_feature(struct net_device *dev, 241 void __user *useraddr, u32 ethcmd) 242 { 243 struct ethtool_value edata; 244 netdev_features_t mask; 245 246 if (copy_from_user(&edata, useraddr, sizeof(edata))) 247 return -EFAULT; 248 249 mask = ethtool_get_feature_mask(ethcmd); 250 mask &= dev->hw_features; 251 if (!mask) 252 return -EOPNOTSUPP; 253 254 if (edata.data) 255 dev->wanted_features |= mask; 256 else 257 dev->wanted_features &= ~mask; 258 259 __netdev_update_features(dev); 260 261 return 0; 262 } 263 264 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ 265 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) 266 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ 267 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ 268 NETIF_F_RXHASH) 269 270 static u32 __ethtool_get_flags(struct net_device *dev) 271 { 272 u32 flags = 0; 273 274 if (dev->features & NETIF_F_LRO) 275 flags |= ETH_FLAG_LRO; 276 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) 277 flags |= ETH_FLAG_RXVLAN; 278 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) 279 flags |= ETH_FLAG_TXVLAN; 280 if (dev->features & NETIF_F_NTUPLE) 281 flags |= ETH_FLAG_NTUPLE; 282 if (dev->features & NETIF_F_RXHASH) 283 flags |= ETH_FLAG_RXHASH; 284 285 return flags; 286 } 287 288 static int __ethtool_set_flags(struct net_device *dev, u32 data) 289 { 290 netdev_features_t features = 0, changed; 291 292 if (data & ~ETH_ALL_FLAGS) 293 return -EINVAL; 294 295 if (data & ETH_FLAG_LRO) 296 features |= NETIF_F_LRO; 297 if (data & ETH_FLAG_RXVLAN) 298 features |= NETIF_F_HW_VLAN_CTAG_RX; 299 if (data & ETH_FLAG_TXVLAN) 300 features |= NETIF_F_HW_VLAN_CTAG_TX; 301 if (data & ETH_FLAG_NTUPLE) 302 features |= NETIF_F_NTUPLE; 303 if (data & ETH_FLAG_RXHASH) 304 features |= NETIF_F_RXHASH; 305 306 /* allow changing only bits set in hw_features */ 307 changed = (features ^ dev->features) & ETH_ALL_FEATURES; 308 if (changed & ~dev->hw_features) 309 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; 310 311 dev->wanted_features = 312 (dev->wanted_features & ~changed) | (features & changed); 313 314 __netdev_update_features(dev); 315 316 return 0; 317 } 318 319 /* Given two link masks, AND them together and save the result in dst. */ 320 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst, 321 struct ethtool_link_ksettings *src) 322 { 323 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS); 324 unsigned int idx = 0; 325 326 for (; idx < size; idx++) { 327 dst->link_modes.supported[idx] &= 328 src->link_modes.supported[idx]; 329 dst->link_modes.advertising[idx] &= 330 src->link_modes.advertising[idx]; 331 } 332 } 333 EXPORT_SYMBOL(ethtool_intersect_link_masks); 334 335 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst, 336 u32 legacy_u32) 337 { 338 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); 339 dst[0] = legacy_u32; 340 } 341 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode); 342 343 /* return false if src had higher bits set. lower bits always updated. */ 344 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32, 345 const unsigned long *src) 346 { 347 bool retval = true; 348 349 /* TODO: following test will soon always be true */ 350 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) { 351 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext); 352 353 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS); 354 bitmap_fill(ext, 32); 355 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS); 356 if (bitmap_intersects(ext, src, 357 __ETHTOOL_LINK_MODE_MASK_NBITS)) { 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, 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, 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, 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, gstrings.len * ETH_GSTRING_LEN)) 1941 goto out; 1942 ret = 0; 1943 1944 out: 1945 vfree(data); 1946 return ret; 1947 } 1948 1949 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...) 1950 { 1951 va_list args; 1952 1953 va_start(args, fmt); 1954 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args); 1955 va_end(args); 1956 1957 *data += ETH_GSTRING_LEN; 1958 } 1959 EXPORT_SYMBOL(ethtool_sprintf); 1960 1961 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) 1962 { 1963 struct ethtool_value id; 1964 static bool busy; 1965 const struct ethtool_ops *ops = dev->ethtool_ops; 1966 int rc; 1967 1968 if (!ops->set_phys_id) 1969 return -EOPNOTSUPP; 1970 1971 if (busy) 1972 return -EBUSY; 1973 1974 if (copy_from_user(&id, useraddr, sizeof(id))) 1975 return -EFAULT; 1976 1977 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); 1978 if (rc < 0) 1979 return rc; 1980 1981 /* Drop the RTNL lock while waiting, but prevent reentry or 1982 * removal of the device. 1983 */ 1984 busy = true; 1985 dev_hold(dev); 1986 rtnl_unlock(); 1987 1988 if (rc == 0) { 1989 /* Driver will handle this itself */ 1990 schedule_timeout_interruptible( 1991 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 1992 } else { 1993 /* Driver expects to be called at twice the frequency in rc */ 1994 int n = rc * 2, interval = HZ / n; 1995 u64 count = n * id.data, i = 0; 1996 1997 do { 1998 rtnl_lock(); 1999 rc = ops->set_phys_id(dev, 2000 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 2001 rtnl_unlock(); 2002 if (rc) 2003 break; 2004 schedule_timeout_interruptible(interval); 2005 } while (!signal_pending(current) && (!id.data || i < count)); 2006 } 2007 2008 rtnl_lock(); 2009 dev_put(dev); 2010 busy = false; 2011 2012 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 2013 return rc; 2014 } 2015 2016 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 2017 { 2018 struct ethtool_stats stats; 2019 const struct ethtool_ops *ops = dev->ethtool_ops; 2020 u64 *data; 2021 int ret, n_stats; 2022 2023 if (!ops->get_ethtool_stats || !ops->get_sset_count) 2024 return -EOPNOTSUPP; 2025 2026 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 2027 if (n_stats < 0) 2028 return n_stats; 2029 if (n_stats > S32_MAX / sizeof(u64)) 2030 return -ENOMEM; 2031 WARN_ON_ONCE(!n_stats); 2032 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2033 return -EFAULT; 2034 2035 stats.n_stats = n_stats; 2036 2037 if (n_stats) { 2038 data = vzalloc(array_size(n_stats, sizeof(u64))); 2039 if (!data) 2040 return -ENOMEM; 2041 ops->get_ethtool_stats(dev, &stats, data); 2042 } else { 2043 data = NULL; 2044 } 2045 2046 ret = -EFAULT; 2047 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2048 goto out; 2049 useraddr += sizeof(stats); 2050 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2051 goto out; 2052 ret = 0; 2053 2054 out: 2055 vfree(data); 2056 return ret; 2057 } 2058 2059 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 2060 { 2061 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 2062 const struct ethtool_ops *ops = dev->ethtool_ops; 2063 struct phy_device *phydev = dev->phydev; 2064 struct ethtool_stats stats; 2065 u64 *data; 2066 int ret, n_stats; 2067 2068 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count)) 2069 return -EOPNOTSUPP; 2070 2071 if (dev->phydev && !ops->get_ethtool_phy_stats && 2072 phy_ops && phy_ops->get_sset_count) 2073 n_stats = phy_ops->get_sset_count(dev->phydev); 2074 else 2075 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 2076 if (n_stats < 0) 2077 return n_stats; 2078 if (n_stats > S32_MAX / sizeof(u64)) 2079 return -ENOMEM; 2080 WARN_ON_ONCE(!n_stats); 2081 2082 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2083 return -EFAULT; 2084 2085 stats.n_stats = n_stats; 2086 2087 if (n_stats) { 2088 data = vzalloc(array_size(n_stats, sizeof(u64))); 2089 if (!data) 2090 return -ENOMEM; 2091 2092 if (dev->phydev && !ops->get_ethtool_phy_stats && 2093 phy_ops && phy_ops->get_stats) { 2094 ret = phy_ops->get_stats(dev->phydev, &stats, data); 2095 if (ret < 0) 2096 goto out; 2097 } else { 2098 ops->get_ethtool_phy_stats(dev, &stats, data); 2099 } 2100 } else { 2101 data = NULL; 2102 } 2103 2104 ret = -EFAULT; 2105 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2106 goto out; 2107 useraddr += sizeof(stats); 2108 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2109 goto out; 2110 ret = 0; 2111 2112 out: 2113 vfree(data); 2114 return ret; 2115 } 2116 2117 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2118 { 2119 struct ethtool_perm_addr epaddr; 2120 2121 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2122 return -EFAULT; 2123 2124 if (epaddr.size < dev->addr_len) 2125 return -ETOOSMALL; 2126 epaddr.size = dev->addr_len; 2127 2128 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2129 return -EFAULT; 2130 useraddr += sizeof(epaddr); 2131 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2132 return -EFAULT; 2133 return 0; 2134 } 2135 2136 static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2137 u32 cmd, u32 (*actor)(struct net_device *)) 2138 { 2139 struct ethtool_value edata = { .cmd = cmd }; 2140 2141 if (!actor) 2142 return -EOPNOTSUPP; 2143 2144 edata.data = actor(dev); 2145 2146 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2147 return -EFAULT; 2148 return 0; 2149 } 2150 2151 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2152 void (*actor)(struct net_device *, u32)) 2153 { 2154 struct ethtool_value edata; 2155 2156 if (!actor) 2157 return -EOPNOTSUPP; 2158 2159 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2160 return -EFAULT; 2161 2162 actor(dev, edata.data); 2163 return 0; 2164 } 2165 2166 static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2167 int (*actor)(struct net_device *, u32)) 2168 { 2169 struct ethtool_value edata; 2170 2171 if (!actor) 2172 return -EOPNOTSUPP; 2173 2174 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2175 return -EFAULT; 2176 2177 return actor(dev, edata.data); 2178 } 2179 2180 static noinline_for_stack int ethtool_flash_device(struct net_device *dev, 2181 char __user *useraddr) 2182 { 2183 struct ethtool_flash efl; 2184 2185 if (copy_from_user(&efl, useraddr, sizeof(efl))) 2186 return -EFAULT; 2187 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 2188 2189 if (!dev->ethtool_ops->flash_device) 2190 return devlink_compat_flash_update(dev, efl.data); 2191 2192 return dev->ethtool_ops->flash_device(dev, &efl); 2193 } 2194 2195 static int ethtool_set_dump(struct net_device *dev, 2196 void __user *useraddr) 2197 { 2198 struct ethtool_dump dump; 2199 2200 if (!dev->ethtool_ops->set_dump) 2201 return -EOPNOTSUPP; 2202 2203 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2204 return -EFAULT; 2205 2206 return dev->ethtool_ops->set_dump(dev, &dump); 2207 } 2208 2209 static int ethtool_get_dump_flag(struct net_device *dev, 2210 void __user *useraddr) 2211 { 2212 int ret; 2213 struct ethtool_dump dump; 2214 const struct ethtool_ops *ops = dev->ethtool_ops; 2215 2216 if (!ops->get_dump_flag) 2217 return -EOPNOTSUPP; 2218 2219 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2220 return -EFAULT; 2221 2222 ret = ops->get_dump_flag(dev, &dump); 2223 if (ret) 2224 return ret; 2225 2226 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2227 return -EFAULT; 2228 return 0; 2229 } 2230 2231 static int ethtool_get_dump_data(struct net_device *dev, 2232 void __user *useraddr) 2233 { 2234 int ret; 2235 __u32 len; 2236 struct ethtool_dump dump, tmp; 2237 const struct ethtool_ops *ops = dev->ethtool_ops; 2238 void *data = NULL; 2239 2240 if (!ops->get_dump_data || !ops->get_dump_flag) 2241 return -EOPNOTSUPP; 2242 2243 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2244 return -EFAULT; 2245 2246 memset(&tmp, 0, sizeof(tmp)); 2247 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2248 ret = ops->get_dump_flag(dev, &tmp); 2249 if (ret) 2250 return ret; 2251 2252 len = min(tmp.len, dump.len); 2253 if (!len) 2254 return -EFAULT; 2255 2256 /* Don't ever let the driver think there's more space available 2257 * than it requested with .get_dump_flag(). 2258 */ 2259 dump.len = len; 2260 2261 /* Always allocate enough space to hold the whole thing so that the 2262 * driver does not need to check the length and bother with partial 2263 * dumping. 2264 */ 2265 data = vzalloc(tmp.len); 2266 if (!data) 2267 return -ENOMEM; 2268 ret = ops->get_dump_data(dev, &dump, data); 2269 if (ret) 2270 goto out; 2271 2272 /* There are two sane possibilities: 2273 * 1. The driver's .get_dump_data() does not touch dump.len. 2274 * 2. Or it may set dump.len to how much it really writes, which 2275 * should be tmp.len (or len if it can do a partial dump). 2276 * In any case respond to userspace with the actual length of data 2277 * it's receiving. 2278 */ 2279 WARN_ON(dump.len != len && dump.len != tmp.len); 2280 dump.len = len; 2281 2282 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2283 ret = -EFAULT; 2284 goto out; 2285 } 2286 useraddr += offsetof(struct ethtool_dump, data); 2287 if (copy_to_user(useraddr, data, len)) 2288 ret = -EFAULT; 2289 out: 2290 vfree(data); 2291 return ret; 2292 } 2293 2294 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2295 { 2296 struct ethtool_ts_info info; 2297 int err; 2298 2299 err = __ethtool_get_ts_info(dev, &info); 2300 if (err) 2301 return err; 2302 2303 if (copy_to_user(useraddr, &info, sizeof(info))) 2304 return -EFAULT; 2305 2306 return 0; 2307 } 2308 2309 int ethtool_get_module_info_call(struct net_device *dev, 2310 struct ethtool_modinfo *modinfo) 2311 { 2312 const struct ethtool_ops *ops = dev->ethtool_ops; 2313 struct phy_device *phydev = dev->phydev; 2314 2315 if (dev->sfp_bus) 2316 return sfp_get_module_info(dev->sfp_bus, modinfo); 2317 2318 if (phydev && phydev->drv && phydev->drv->module_info) 2319 return phydev->drv->module_info(phydev, modinfo); 2320 2321 if (ops->get_module_info) 2322 return ops->get_module_info(dev, modinfo); 2323 2324 return -EOPNOTSUPP; 2325 } 2326 2327 static int ethtool_get_module_info(struct net_device *dev, 2328 void __user *useraddr) 2329 { 2330 int ret; 2331 struct ethtool_modinfo modinfo; 2332 2333 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2334 return -EFAULT; 2335 2336 ret = ethtool_get_module_info_call(dev, &modinfo); 2337 if (ret) 2338 return ret; 2339 2340 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2341 return -EFAULT; 2342 2343 return 0; 2344 } 2345 2346 int ethtool_get_module_eeprom_call(struct net_device *dev, 2347 struct ethtool_eeprom *ee, u8 *data) 2348 { 2349 const struct ethtool_ops *ops = dev->ethtool_ops; 2350 struct phy_device *phydev = dev->phydev; 2351 2352 if (dev->sfp_bus) 2353 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2354 2355 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2356 return phydev->drv->module_eeprom(phydev, ee, data); 2357 2358 if (ops->get_module_eeprom) 2359 return ops->get_module_eeprom(dev, ee, data); 2360 2361 return -EOPNOTSUPP; 2362 } 2363 2364 static int ethtool_get_module_eeprom(struct net_device *dev, 2365 void __user *useraddr) 2366 { 2367 int ret; 2368 struct ethtool_modinfo modinfo; 2369 2370 ret = ethtool_get_module_info_call(dev, &modinfo); 2371 if (ret) 2372 return ret; 2373 2374 return ethtool_get_any_eeprom(dev, useraddr, 2375 ethtool_get_module_eeprom_call, 2376 modinfo.eeprom_len); 2377 } 2378 2379 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2380 { 2381 switch (tuna->id) { 2382 case ETHTOOL_RX_COPYBREAK: 2383 case ETHTOOL_TX_COPYBREAK: 2384 if (tuna->len != sizeof(u32) || 2385 tuna->type_id != ETHTOOL_TUNABLE_U32) 2386 return -EINVAL; 2387 break; 2388 case ETHTOOL_PFC_PREVENTION_TOUT: 2389 if (tuna->len != sizeof(u16) || 2390 tuna->type_id != ETHTOOL_TUNABLE_U16) 2391 return -EINVAL; 2392 break; 2393 default: 2394 return -EINVAL; 2395 } 2396 2397 return 0; 2398 } 2399 2400 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2401 { 2402 int ret; 2403 struct ethtool_tunable tuna; 2404 const struct ethtool_ops *ops = dev->ethtool_ops; 2405 void *data; 2406 2407 if (!ops->get_tunable) 2408 return -EOPNOTSUPP; 2409 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2410 return -EFAULT; 2411 ret = ethtool_tunable_valid(&tuna); 2412 if (ret) 2413 return ret; 2414 data = kzalloc(tuna.len, GFP_USER); 2415 if (!data) 2416 return -ENOMEM; 2417 ret = ops->get_tunable(dev, &tuna, data); 2418 if (ret) 2419 goto out; 2420 useraddr += sizeof(tuna); 2421 ret = -EFAULT; 2422 if (copy_to_user(useraddr, data, tuna.len)) 2423 goto out; 2424 ret = 0; 2425 2426 out: 2427 kfree(data); 2428 return ret; 2429 } 2430 2431 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2432 { 2433 int ret; 2434 struct ethtool_tunable tuna; 2435 const struct ethtool_ops *ops = dev->ethtool_ops; 2436 void *data; 2437 2438 if (!ops->set_tunable) 2439 return -EOPNOTSUPP; 2440 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2441 return -EFAULT; 2442 ret = ethtool_tunable_valid(&tuna); 2443 if (ret) 2444 return ret; 2445 useraddr += sizeof(tuna); 2446 data = memdup_user(useraddr, tuna.len); 2447 if (IS_ERR(data)) 2448 return PTR_ERR(data); 2449 ret = ops->set_tunable(dev, &tuna, data); 2450 2451 kfree(data); 2452 return ret; 2453 } 2454 2455 static noinline_for_stack int 2456 ethtool_get_per_queue_coalesce(struct net_device *dev, 2457 void __user *useraddr, 2458 struct ethtool_per_queue_op *per_queue_opt) 2459 { 2460 u32 bit; 2461 int ret; 2462 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2463 2464 if (!dev->ethtool_ops->get_per_queue_coalesce) 2465 return -EOPNOTSUPP; 2466 2467 useraddr += sizeof(*per_queue_opt); 2468 2469 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 2470 MAX_NUM_QUEUE); 2471 2472 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2473 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2474 2475 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 2476 if (ret != 0) 2477 return ret; 2478 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2479 return -EFAULT; 2480 useraddr += sizeof(coalesce); 2481 } 2482 2483 return 0; 2484 } 2485 2486 static noinline_for_stack int 2487 ethtool_set_per_queue_coalesce(struct net_device *dev, 2488 void __user *useraddr, 2489 struct ethtool_per_queue_op *per_queue_opt) 2490 { 2491 u32 bit; 2492 int i, ret = 0; 2493 int n_queue; 2494 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 2495 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2496 2497 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 2498 (!dev->ethtool_ops->get_per_queue_coalesce)) 2499 return -EOPNOTSUPP; 2500 2501 useraddr += sizeof(*per_queue_opt); 2502 2503 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 2504 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 2505 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); 2506 if (!backup) 2507 return -ENOMEM; 2508 2509 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2510 struct ethtool_coalesce coalesce; 2511 2512 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 2513 if (ret != 0) 2514 goto roll_back; 2515 2516 tmp++; 2517 2518 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 2519 ret = -EFAULT; 2520 goto roll_back; 2521 } 2522 2523 if (!ethtool_set_coalesce_supported(dev, &coalesce)) { 2524 ret = -EOPNOTSUPP; 2525 goto roll_back; 2526 } 2527 2528 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 2529 if (ret != 0) 2530 goto roll_back; 2531 2532 useraddr += sizeof(coalesce); 2533 } 2534 2535 roll_back: 2536 if (ret != 0) { 2537 tmp = backup; 2538 for_each_set_bit(i, queue_mask, bit) { 2539 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 2540 tmp++; 2541 } 2542 } 2543 kfree(backup); 2544 2545 return ret; 2546 } 2547 2548 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, 2549 void __user *useraddr, u32 sub_cmd) 2550 { 2551 struct ethtool_per_queue_op per_queue_opt; 2552 2553 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 2554 return -EFAULT; 2555 2556 if (per_queue_opt.sub_command != sub_cmd) 2557 return -EINVAL; 2558 2559 switch (per_queue_opt.sub_command) { 2560 case ETHTOOL_GCOALESCE: 2561 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2562 case ETHTOOL_SCOALESCE: 2563 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2564 default: 2565 return -EOPNOTSUPP; 2566 } 2567 } 2568 2569 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 2570 { 2571 switch (tuna->id) { 2572 case ETHTOOL_PHY_DOWNSHIFT: 2573 case ETHTOOL_PHY_FAST_LINK_DOWN: 2574 if (tuna->len != sizeof(u8) || 2575 tuna->type_id != ETHTOOL_TUNABLE_U8) 2576 return -EINVAL; 2577 break; 2578 case ETHTOOL_PHY_EDPD: 2579 if (tuna->len != sizeof(u16) || 2580 tuna->type_id != ETHTOOL_TUNABLE_U16) 2581 return -EINVAL; 2582 break; 2583 default: 2584 return -EINVAL; 2585 } 2586 2587 return 0; 2588 } 2589 2590 static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 2591 { 2592 struct phy_device *phydev = dev->phydev; 2593 struct ethtool_tunable tuna; 2594 bool phy_drv_tunable; 2595 void *data; 2596 int ret; 2597 2598 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2599 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) 2600 return -EOPNOTSUPP; 2601 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2602 return -EFAULT; 2603 ret = ethtool_phy_tunable_valid(&tuna); 2604 if (ret) 2605 return ret; 2606 data = kzalloc(tuna.len, GFP_USER); 2607 if (!data) 2608 return -ENOMEM; 2609 if (phy_drv_tunable) { 2610 mutex_lock(&phydev->lock); 2611 ret = phydev->drv->get_tunable(phydev, &tuna, data); 2612 mutex_unlock(&phydev->lock); 2613 } else { 2614 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); 2615 } 2616 if (ret) 2617 goto out; 2618 useraddr += sizeof(tuna); 2619 ret = -EFAULT; 2620 if (copy_to_user(useraddr, data, tuna.len)) 2621 goto out; 2622 ret = 0; 2623 2624 out: 2625 kfree(data); 2626 return ret; 2627 } 2628 2629 static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 2630 { 2631 struct phy_device *phydev = dev->phydev; 2632 struct ethtool_tunable tuna; 2633 bool phy_drv_tunable; 2634 void *data; 2635 int ret; 2636 2637 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2638 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) 2639 return -EOPNOTSUPP; 2640 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2641 return -EFAULT; 2642 ret = ethtool_phy_tunable_valid(&tuna); 2643 if (ret) 2644 return ret; 2645 useraddr += sizeof(tuna); 2646 data = memdup_user(useraddr, tuna.len); 2647 if (IS_ERR(data)) 2648 return PTR_ERR(data); 2649 if (phy_drv_tunable) { 2650 mutex_lock(&phydev->lock); 2651 ret = phydev->drv->set_tunable(phydev, &tuna, data); 2652 mutex_unlock(&phydev->lock); 2653 } else { 2654 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); 2655 } 2656 2657 kfree(data); 2658 return ret; 2659 } 2660 2661 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 2662 { 2663 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; 2664 int rc; 2665 2666 if (!dev->ethtool_ops->get_fecparam) 2667 return -EOPNOTSUPP; 2668 2669 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 2670 if (rc) 2671 return rc; 2672 2673 if (WARN_ON_ONCE(fecparam.reserved)) 2674 fecparam.reserved = 0; 2675 2676 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 2677 return -EFAULT; 2678 return 0; 2679 } 2680 2681 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 2682 { 2683 struct ethtool_fecparam fecparam; 2684 2685 if (!dev->ethtool_ops->set_fecparam) 2686 return -EOPNOTSUPP; 2687 2688 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 2689 return -EFAULT; 2690 2691 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) 2692 return -EINVAL; 2693 2694 fecparam.active_fec = 0; 2695 fecparam.reserved = 0; 2696 2697 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 2698 } 2699 2700 /* The main entry point in this file. Called from net/core/dev_ioctl.c */ 2701 2702 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) 2703 { 2704 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); 2705 u32 ethcmd, sub_cmd; 2706 int rc; 2707 netdev_features_t old_features; 2708 2709 if (!dev) 2710 return -ENODEV; 2711 2712 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 2713 return -EFAULT; 2714 2715 if (ethcmd == ETHTOOL_PERQUEUE) { 2716 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 2717 return -EFAULT; 2718 } else { 2719 sub_cmd = ethcmd; 2720 } 2721 /* Allow some commands to be done by anyone */ 2722 switch (sub_cmd) { 2723 case ETHTOOL_GSET: 2724 case ETHTOOL_GDRVINFO: 2725 case ETHTOOL_GMSGLVL: 2726 case ETHTOOL_GLINK: 2727 case ETHTOOL_GCOALESCE: 2728 case ETHTOOL_GRINGPARAM: 2729 case ETHTOOL_GPAUSEPARAM: 2730 case ETHTOOL_GRXCSUM: 2731 case ETHTOOL_GTXCSUM: 2732 case ETHTOOL_GSG: 2733 case ETHTOOL_GSSET_INFO: 2734 case ETHTOOL_GSTRINGS: 2735 case ETHTOOL_GSTATS: 2736 case ETHTOOL_GPHYSTATS: 2737 case ETHTOOL_GTSO: 2738 case ETHTOOL_GPERMADDR: 2739 case ETHTOOL_GUFO: 2740 case ETHTOOL_GGSO: 2741 case ETHTOOL_GGRO: 2742 case ETHTOOL_GFLAGS: 2743 case ETHTOOL_GPFLAGS: 2744 case ETHTOOL_GRXFH: 2745 case ETHTOOL_GRXRINGS: 2746 case ETHTOOL_GRXCLSRLCNT: 2747 case ETHTOOL_GRXCLSRULE: 2748 case ETHTOOL_GRXCLSRLALL: 2749 case ETHTOOL_GRXFHINDIR: 2750 case ETHTOOL_GRSSH: 2751 case ETHTOOL_GFEATURES: 2752 case ETHTOOL_GCHANNELS: 2753 case ETHTOOL_GET_TS_INFO: 2754 case ETHTOOL_GEEE: 2755 case ETHTOOL_GTUNABLE: 2756 case ETHTOOL_PHY_GTUNABLE: 2757 case ETHTOOL_GLINKSETTINGS: 2758 case ETHTOOL_GFECPARAM: 2759 break; 2760 default: 2761 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2762 return -EPERM; 2763 } 2764 2765 if (dev->dev.parent) 2766 pm_runtime_get_sync(dev->dev.parent); 2767 2768 if (!netif_device_present(dev)) { 2769 rc = -ENODEV; 2770 goto out; 2771 } 2772 2773 if (dev->ethtool_ops->begin) { 2774 rc = dev->ethtool_ops->begin(dev); 2775 if (rc < 0) 2776 goto out; 2777 } 2778 old_features = dev->features; 2779 2780 switch (ethcmd) { 2781 case ETHTOOL_GSET: 2782 rc = ethtool_get_settings(dev, useraddr); 2783 break; 2784 case ETHTOOL_SSET: 2785 rc = ethtool_set_settings(dev, useraddr); 2786 break; 2787 case ETHTOOL_GDRVINFO: 2788 rc = ethtool_get_drvinfo(dev, useraddr); 2789 break; 2790 case ETHTOOL_GREGS: 2791 rc = ethtool_get_regs(dev, useraddr); 2792 break; 2793 case ETHTOOL_GWOL: 2794 rc = ethtool_get_wol(dev, useraddr); 2795 break; 2796 case ETHTOOL_SWOL: 2797 rc = ethtool_set_wol(dev, useraddr); 2798 break; 2799 case ETHTOOL_GMSGLVL: 2800 rc = ethtool_get_value(dev, useraddr, ethcmd, 2801 dev->ethtool_ops->get_msglevel); 2802 break; 2803 case ETHTOOL_SMSGLVL: 2804 rc = ethtool_set_value_void(dev, useraddr, 2805 dev->ethtool_ops->set_msglevel); 2806 if (!rc) 2807 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL); 2808 break; 2809 case ETHTOOL_GEEE: 2810 rc = ethtool_get_eee(dev, useraddr); 2811 break; 2812 case ETHTOOL_SEEE: 2813 rc = ethtool_set_eee(dev, useraddr); 2814 break; 2815 case ETHTOOL_NWAY_RST: 2816 rc = ethtool_nway_reset(dev); 2817 break; 2818 case ETHTOOL_GLINK: 2819 rc = ethtool_get_link(dev, useraddr); 2820 break; 2821 case ETHTOOL_GEEPROM: 2822 rc = ethtool_get_eeprom(dev, useraddr); 2823 break; 2824 case ETHTOOL_SEEPROM: 2825 rc = ethtool_set_eeprom(dev, useraddr); 2826 break; 2827 case ETHTOOL_GCOALESCE: 2828 rc = ethtool_get_coalesce(dev, useraddr); 2829 break; 2830 case ETHTOOL_SCOALESCE: 2831 rc = ethtool_set_coalesce(dev, useraddr); 2832 break; 2833 case ETHTOOL_GRINGPARAM: 2834 rc = ethtool_get_ringparam(dev, useraddr); 2835 break; 2836 case ETHTOOL_SRINGPARAM: 2837 rc = ethtool_set_ringparam(dev, useraddr); 2838 break; 2839 case ETHTOOL_GPAUSEPARAM: 2840 rc = ethtool_get_pauseparam(dev, useraddr); 2841 break; 2842 case ETHTOOL_SPAUSEPARAM: 2843 rc = ethtool_set_pauseparam(dev, useraddr); 2844 break; 2845 case ETHTOOL_TEST: 2846 rc = ethtool_self_test(dev, useraddr); 2847 break; 2848 case ETHTOOL_GSTRINGS: 2849 rc = ethtool_get_strings(dev, useraddr); 2850 break; 2851 case ETHTOOL_PHYS_ID: 2852 rc = ethtool_phys_id(dev, useraddr); 2853 break; 2854 case ETHTOOL_GSTATS: 2855 rc = ethtool_get_stats(dev, useraddr); 2856 break; 2857 case ETHTOOL_GPERMADDR: 2858 rc = ethtool_get_perm_addr(dev, useraddr); 2859 break; 2860 case ETHTOOL_GFLAGS: 2861 rc = ethtool_get_value(dev, useraddr, ethcmd, 2862 __ethtool_get_flags); 2863 break; 2864 case ETHTOOL_SFLAGS: 2865 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 2866 break; 2867 case ETHTOOL_GPFLAGS: 2868 rc = ethtool_get_value(dev, useraddr, ethcmd, 2869 dev->ethtool_ops->get_priv_flags); 2870 if (!rc) 2871 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL); 2872 break; 2873 case ETHTOOL_SPFLAGS: 2874 rc = ethtool_set_value(dev, useraddr, 2875 dev->ethtool_ops->set_priv_flags); 2876 break; 2877 case ETHTOOL_GRXFH: 2878 case ETHTOOL_GRXRINGS: 2879 case ETHTOOL_GRXCLSRLCNT: 2880 case ETHTOOL_GRXCLSRULE: 2881 case ETHTOOL_GRXCLSRLALL: 2882 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 2883 break; 2884 case ETHTOOL_SRXFH: 2885 case ETHTOOL_SRXCLSRLDEL: 2886 case ETHTOOL_SRXCLSRLINS: 2887 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 2888 break; 2889 case ETHTOOL_FLASHDEV: 2890 rc = ethtool_flash_device(dev, useraddr); 2891 break; 2892 case ETHTOOL_RESET: 2893 rc = ethtool_reset(dev, useraddr); 2894 break; 2895 case ETHTOOL_GSSET_INFO: 2896 rc = ethtool_get_sset_info(dev, useraddr); 2897 break; 2898 case ETHTOOL_GRXFHINDIR: 2899 rc = ethtool_get_rxfh_indir(dev, useraddr); 2900 break; 2901 case ETHTOOL_SRXFHINDIR: 2902 rc = ethtool_set_rxfh_indir(dev, useraddr); 2903 break; 2904 case ETHTOOL_GRSSH: 2905 rc = ethtool_get_rxfh(dev, useraddr); 2906 break; 2907 case ETHTOOL_SRSSH: 2908 rc = ethtool_set_rxfh(dev, useraddr); 2909 break; 2910 case ETHTOOL_GFEATURES: 2911 rc = ethtool_get_features(dev, useraddr); 2912 break; 2913 case ETHTOOL_SFEATURES: 2914 rc = ethtool_set_features(dev, useraddr); 2915 break; 2916 case ETHTOOL_GTXCSUM: 2917 case ETHTOOL_GRXCSUM: 2918 case ETHTOOL_GSG: 2919 case ETHTOOL_GTSO: 2920 case ETHTOOL_GGSO: 2921 case ETHTOOL_GGRO: 2922 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 2923 break; 2924 case ETHTOOL_STXCSUM: 2925 case ETHTOOL_SRXCSUM: 2926 case ETHTOOL_SSG: 2927 case ETHTOOL_STSO: 2928 case ETHTOOL_SGSO: 2929 case ETHTOOL_SGRO: 2930 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 2931 break; 2932 case ETHTOOL_GCHANNELS: 2933 rc = ethtool_get_channels(dev, useraddr); 2934 break; 2935 case ETHTOOL_SCHANNELS: 2936 rc = ethtool_set_channels(dev, useraddr); 2937 break; 2938 case ETHTOOL_SET_DUMP: 2939 rc = ethtool_set_dump(dev, useraddr); 2940 break; 2941 case ETHTOOL_GET_DUMP_FLAG: 2942 rc = ethtool_get_dump_flag(dev, useraddr); 2943 break; 2944 case ETHTOOL_GET_DUMP_DATA: 2945 rc = ethtool_get_dump_data(dev, useraddr); 2946 break; 2947 case ETHTOOL_GET_TS_INFO: 2948 rc = ethtool_get_ts_info(dev, useraddr); 2949 break; 2950 case ETHTOOL_GMODULEINFO: 2951 rc = ethtool_get_module_info(dev, useraddr); 2952 break; 2953 case ETHTOOL_GMODULEEEPROM: 2954 rc = ethtool_get_module_eeprom(dev, useraddr); 2955 break; 2956 case ETHTOOL_GTUNABLE: 2957 rc = ethtool_get_tunable(dev, useraddr); 2958 break; 2959 case ETHTOOL_STUNABLE: 2960 rc = ethtool_set_tunable(dev, useraddr); 2961 break; 2962 case ETHTOOL_GPHYSTATS: 2963 rc = ethtool_get_phy_stats(dev, useraddr); 2964 break; 2965 case ETHTOOL_PERQUEUE: 2966 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); 2967 break; 2968 case ETHTOOL_GLINKSETTINGS: 2969 rc = ethtool_get_link_ksettings(dev, useraddr); 2970 break; 2971 case ETHTOOL_SLINKSETTINGS: 2972 rc = ethtool_set_link_ksettings(dev, useraddr); 2973 break; 2974 case ETHTOOL_PHY_GTUNABLE: 2975 rc = get_phy_tunable(dev, useraddr); 2976 break; 2977 case ETHTOOL_PHY_STUNABLE: 2978 rc = set_phy_tunable(dev, useraddr); 2979 break; 2980 case ETHTOOL_GFECPARAM: 2981 rc = ethtool_get_fecparam(dev, useraddr); 2982 break; 2983 case ETHTOOL_SFECPARAM: 2984 rc = ethtool_set_fecparam(dev, useraddr); 2985 break; 2986 default: 2987 rc = -EOPNOTSUPP; 2988 } 2989 2990 if (dev->ethtool_ops->complete) 2991 dev->ethtool_ops->complete(dev); 2992 2993 if (old_features != dev->features) 2994 netdev_features_change(dev); 2995 out: 2996 if (dev->dev.parent) 2997 pm_runtime_put(dev->dev.parent); 2998 2999 return rc; 3000 } 3001 3002 struct ethtool_rx_flow_key { 3003 struct flow_dissector_key_basic basic; 3004 union { 3005 struct flow_dissector_key_ipv4_addrs ipv4; 3006 struct flow_dissector_key_ipv6_addrs ipv6; 3007 }; 3008 struct flow_dissector_key_ports tp; 3009 struct flow_dissector_key_ip ip; 3010 struct flow_dissector_key_vlan vlan; 3011 struct flow_dissector_key_eth_addrs eth_addrs; 3012 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 3013 3014 struct ethtool_rx_flow_match { 3015 struct flow_dissector dissector; 3016 struct ethtool_rx_flow_key key; 3017 struct ethtool_rx_flow_key mask; 3018 }; 3019 3020 struct ethtool_rx_flow_rule * 3021 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) 3022 { 3023 const struct ethtool_rx_flow_spec *fs = input->fs; 3024 static struct in6_addr zero_addr = {}; 3025 struct ethtool_rx_flow_match *match; 3026 struct ethtool_rx_flow_rule *flow; 3027 struct flow_action_entry *act; 3028 3029 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + 3030 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); 3031 if (!flow) 3032 return ERR_PTR(-ENOMEM); 3033 3034 /* ethtool_rx supports only one single action per rule. */ 3035 flow->rule = flow_rule_alloc(1); 3036 if (!flow->rule) { 3037 kfree(flow); 3038 return ERR_PTR(-ENOMEM); 3039 } 3040 3041 match = (struct ethtool_rx_flow_match *)flow->priv; 3042 flow->rule->match.dissector = &match->dissector; 3043 flow->rule->match.mask = &match->mask; 3044 flow->rule->match.key = &match->key; 3045 3046 match->mask.basic.n_proto = htons(0xffff); 3047 3048 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3049 case ETHER_FLOW: { 3050 const struct ethhdr *ether_spec, *ether_m_spec; 3051 3052 ether_spec = &fs->h_u.ether_spec; 3053 ether_m_spec = &fs->m_u.ether_spec; 3054 3055 if (!is_zero_ether_addr(ether_m_spec->h_source)) { 3056 ether_addr_copy(match->key.eth_addrs.src, 3057 ether_spec->h_source); 3058 ether_addr_copy(match->mask.eth_addrs.src, 3059 ether_m_spec->h_source); 3060 } 3061 if (!is_zero_ether_addr(ether_m_spec->h_dest)) { 3062 ether_addr_copy(match->key.eth_addrs.dst, 3063 ether_spec->h_dest); 3064 ether_addr_copy(match->mask.eth_addrs.dst, 3065 ether_m_spec->h_dest); 3066 } 3067 if (ether_m_spec->h_proto) { 3068 match->key.basic.n_proto = ether_spec->h_proto; 3069 match->mask.basic.n_proto = ether_m_spec->h_proto; 3070 } 3071 } 3072 break; 3073 case TCP_V4_FLOW: 3074 case UDP_V4_FLOW: { 3075 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; 3076 3077 match->key.basic.n_proto = htons(ETH_P_IP); 3078 3079 v4_spec = &fs->h_u.tcp_ip4_spec; 3080 v4_m_spec = &fs->m_u.tcp_ip4_spec; 3081 3082 if (v4_m_spec->ip4src) { 3083 match->key.ipv4.src = v4_spec->ip4src; 3084 match->mask.ipv4.src = v4_m_spec->ip4src; 3085 } 3086 if (v4_m_spec->ip4dst) { 3087 match->key.ipv4.dst = v4_spec->ip4dst; 3088 match->mask.ipv4.dst = v4_m_spec->ip4dst; 3089 } 3090 if (v4_m_spec->ip4src || 3091 v4_m_spec->ip4dst) { 3092 match->dissector.used_keys |= 3093 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS); 3094 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 3095 offsetof(struct ethtool_rx_flow_key, ipv4); 3096 } 3097 if (v4_m_spec->psrc) { 3098 match->key.tp.src = v4_spec->psrc; 3099 match->mask.tp.src = v4_m_spec->psrc; 3100 } 3101 if (v4_m_spec->pdst) { 3102 match->key.tp.dst = v4_spec->pdst; 3103 match->mask.tp.dst = v4_m_spec->pdst; 3104 } 3105 if (v4_m_spec->psrc || 3106 v4_m_spec->pdst) { 3107 match->dissector.used_keys |= 3108 BIT(FLOW_DISSECTOR_KEY_PORTS); 3109 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3110 offsetof(struct ethtool_rx_flow_key, tp); 3111 } 3112 if (v4_m_spec->tos) { 3113 match->key.ip.tos = v4_spec->tos; 3114 match->mask.ip.tos = v4_m_spec->tos; 3115 match->dissector.used_keys |= 3116 BIT(FLOW_DISSECTOR_KEY_IP); 3117 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3118 offsetof(struct ethtool_rx_flow_key, ip); 3119 } 3120 } 3121 break; 3122 case TCP_V6_FLOW: 3123 case UDP_V6_FLOW: { 3124 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; 3125 3126 match->key.basic.n_proto = htons(ETH_P_IPV6); 3127 3128 v6_spec = &fs->h_u.tcp_ip6_spec; 3129 v6_m_spec = &fs->m_u.tcp_ip6_spec; 3130 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) { 3131 memcpy(&match->key.ipv6.src, v6_spec->ip6src, 3132 sizeof(match->key.ipv6.src)); 3133 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, 3134 sizeof(match->mask.ipv6.src)); 3135 } 3136 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { 3137 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, 3138 sizeof(match->key.ipv6.dst)); 3139 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, 3140 sizeof(match->mask.ipv6.dst)); 3141 } 3142 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) || 3143 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { 3144 match->dissector.used_keys |= 3145 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS); 3146 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = 3147 offsetof(struct ethtool_rx_flow_key, ipv6); 3148 } 3149 if (v6_m_spec->psrc) { 3150 match->key.tp.src = v6_spec->psrc; 3151 match->mask.tp.src = v6_m_spec->psrc; 3152 } 3153 if (v6_m_spec->pdst) { 3154 match->key.tp.dst = v6_spec->pdst; 3155 match->mask.tp.dst = v6_m_spec->pdst; 3156 } 3157 if (v6_m_spec->psrc || 3158 v6_m_spec->pdst) { 3159 match->dissector.used_keys |= 3160 BIT(FLOW_DISSECTOR_KEY_PORTS); 3161 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3162 offsetof(struct ethtool_rx_flow_key, tp); 3163 } 3164 if (v6_m_spec->tclass) { 3165 match->key.ip.tos = v6_spec->tclass; 3166 match->mask.ip.tos = v6_m_spec->tclass; 3167 match->dissector.used_keys |= 3168 BIT(FLOW_DISSECTOR_KEY_IP); 3169 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3170 offsetof(struct ethtool_rx_flow_key, ip); 3171 } 3172 } 3173 break; 3174 default: 3175 ethtool_rx_flow_rule_destroy(flow); 3176 return ERR_PTR(-EINVAL); 3177 } 3178 3179 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3180 case TCP_V4_FLOW: 3181 case TCP_V6_FLOW: 3182 match->key.basic.ip_proto = IPPROTO_TCP; 3183 match->mask.basic.ip_proto = 0xff; 3184 break; 3185 case UDP_V4_FLOW: 3186 case UDP_V6_FLOW: 3187 match->key.basic.ip_proto = IPPROTO_UDP; 3188 match->mask.basic.ip_proto = 0xff; 3189 break; 3190 } 3191 3192 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC); 3193 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = 3194 offsetof(struct ethtool_rx_flow_key, basic); 3195 3196 if (fs->flow_type & FLOW_EXT) { 3197 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3198 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3199 3200 if (ext_m_spec->vlan_etype) { 3201 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; 3202 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; 3203 } 3204 3205 if (ext_m_spec->vlan_tci) { 3206 match->key.vlan.vlan_id = 3207 ntohs(ext_h_spec->vlan_tci) & 0x0fff; 3208 match->mask.vlan.vlan_id = 3209 ntohs(ext_m_spec->vlan_tci) & 0x0fff; 3210 3211 match->key.vlan.vlan_dei = 3212 !!(ext_h_spec->vlan_tci & htons(0x1000)); 3213 match->mask.vlan.vlan_dei = 3214 !!(ext_m_spec->vlan_tci & htons(0x1000)); 3215 3216 match->key.vlan.vlan_priority = 3217 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; 3218 match->mask.vlan.vlan_priority = 3219 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; 3220 } 3221 3222 if (ext_m_spec->vlan_etype || 3223 ext_m_spec->vlan_tci) { 3224 match->dissector.used_keys |= 3225 BIT(FLOW_DISSECTOR_KEY_VLAN); 3226 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = 3227 offsetof(struct ethtool_rx_flow_key, vlan); 3228 } 3229 } 3230 if (fs->flow_type & FLOW_MAC_EXT) { 3231 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3232 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3233 3234 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, 3235 ETH_ALEN); 3236 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, 3237 ETH_ALEN); 3238 3239 match->dissector.used_keys |= 3240 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS); 3241 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = 3242 offsetof(struct ethtool_rx_flow_key, eth_addrs); 3243 } 3244 3245 act = &flow->rule->action.entries[0]; 3246 switch (fs->ring_cookie) { 3247 case RX_CLS_FLOW_DISC: 3248 act->id = FLOW_ACTION_DROP; 3249 break; 3250 case RX_CLS_FLOW_WAKE: 3251 act->id = FLOW_ACTION_WAKE; 3252 break; 3253 default: 3254 act->id = FLOW_ACTION_QUEUE; 3255 if (fs->flow_type & FLOW_RSS) 3256 act->queue.ctx = input->rss_ctx; 3257 3258 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); 3259 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); 3260 break; 3261 } 3262 3263 return flow; 3264 } 3265 EXPORT_SYMBOL(ethtool_rx_flow_rule_create); 3266 3267 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) 3268 { 3269 kfree(flow->rule); 3270 kfree(flow); 3271 } 3272 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); 3273