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 (copy_to_user(userbuf, data, eeprom.len)) { 1541 ret = -EFAULT; 1542 break; 1543 } 1544 userbuf += eeprom.len; 1545 eeprom.offset += eeprom.len; 1546 bytes_remaining -= eeprom.len; 1547 } 1548 1549 eeprom.len = userbuf - (useraddr + sizeof(eeprom)); 1550 eeprom.offset -= eeprom.len; 1551 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) 1552 ret = -EFAULT; 1553 1554 kfree(data); 1555 return ret; 1556 } 1557 1558 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) 1559 { 1560 const struct ethtool_ops *ops = dev->ethtool_ops; 1561 1562 if (!ops->get_eeprom || !ops->get_eeprom_len || 1563 !ops->get_eeprom_len(dev)) 1564 return -EOPNOTSUPP; 1565 1566 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, 1567 ops->get_eeprom_len(dev)); 1568 } 1569 1570 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) 1571 { 1572 struct ethtool_eeprom eeprom; 1573 const struct ethtool_ops *ops = dev->ethtool_ops; 1574 void __user *userbuf = useraddr + sizeof(eeprom); 1575 u32 bytes_remaining; 1576 u8 *data; 1577 int ret = 0; 1578 1579 if (!ops->set_eeprom || !ops->get_eeprom_len || 1580 !ops->get_eeprom_len(dev)) 1581 return -EOPNOTSUPP; 1582 1583 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) 1584 return -EFAULT; 1585 1586 /* Check for wrap and zero */ 1587 if (eeprom.offset + eeprom.len <= eeprom.offset) 1588 return -EINVAL; 1589 1590 /* Check for exceeding total eeprom len */ 1591 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) 1592 return -EINVAL; 1593 1594 data = kzalloc(PAGE_SIZE, GFP_USER); 1595 if (!data) 1596 return -ENOMEM; 1597 1598 bytes_remaining = eeprom.len; 1599 while (bytes_remaining > 0) { 1600 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); 1601 1602 if (copy_from_user(data, userbuf, eeprom.len)) { 1603 ret = -EFAULT; 1604 break; 1605 } 1606 ret = ops->set_eeprom(dev, &eeprom, data); 1607 if (ret) 1608 break; 1609 userbuf += eeprom.len; 1610 eeprom.offset += eeprom.len; 1611 bytes_remaining -= eeprom.len; 1612 } 1613 1614 kfree(data); 1615 return ret; 1616 } 1617 1618 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, 1619 void __user *useraddr) 1620 { 1621 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 1622 int ret; 1623 1624 if (!dev->ethtool_ops->get_coalesce) 1625 return -EOPNOTSUPP; 1626 1627 ret = dev->ethtool_ops->get_coalesce(dev, &coalesce); 1628 if (ret) 1629 return ret; 1630 1631 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 1632 return -EFAULT; 1633 return 0; 1634 } 1635 1636 static bool 1637 ethtool_set_coalesce_supported(struct net_device *dev, 1638 struct ethtool_coalesce *coalesce) 1639 { 1640 u32 supported_params = dev->ethtool_ops->supported_coalesce_params; 1641 u32 nonzero_params = 0; 1642 1643 if (coalesce->rx_coalesce_usecs) 1644 nonzero_params |= ETHTOOL_COALESCE_RX_USECS; 1645 if (coalesce->rx_max_coalesced_frames) 1646 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES; 1647 if (coalesce->rx_coalesce_usecs_irq) 1648 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ; 1649 if (coalesce->rx_max_coalesced_frames_irq) 1650 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ; 1651 if (coalesce->tx_coalesce_usecs) 1652 nonzero_params |= ETHTOOL_COALESCE_TX_USECS; 1653 if (coalesce->tx_max_coalesced_frames) 1654 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES; 1655 if (coalesce->tx_coalesce_usecs_irq) 1656 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ; 1657 if (coalesce->tx_max_coalesced_frames_irq) 1658 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ; 1659 if (coalesce->stats_block_coalesce_usecs) 1660 nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS; 1661 if (coalesce->use_adaptive_rx_coalesce) 1662 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX; 1663 if (coalesce->use_adaptive_tx_coalesce) 1664 nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX; 1665 if (coalesce->pkt_rate_low) 1666 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW; 1667 if (coalesce->rx_coalesce_usecs_low) 1668 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW; 1669 if (coalesce->rx_max_coalesced_frames_low) 1670 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW; 1671 if (coalesce->tx_coalesce_usecs_low) 1672 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW; 1673 if (coalesce->tx_max_coalesced_frames_low) 1674 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW; 1675 if (coalesce->pkt_rate_high) 1676 nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH; 1677 if (coalesce->rx_coalesce_usecs_high) 1678 nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH; 1679 if (coalesce->rx_max_coalesced_frames_high) 1680 nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH; 1681 if (coalesce->tx_coalesce_usecs_high) 1682 nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH; 1683 if (coalesce->tx_max_coalesced_frames_high) 1684 nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH; 1685 if (coalesce->rate_sample_interval) 1686 nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL; 1687 1688 return (supported_params & nonzero_params) == nonzero_params; 1689 } 1690 1691 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, 1692 void __user *useraddr) 1693 { 1694 struct ethtool_coalesce coalesce; 1695 int ret; 1696 1697 if (!dev->ethtool_ops->set_coalesce) 1698 return -EOPNOTSUPP; 1699 1700 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) 1701 return -EFAULT; 1702 1703 if (!ethtool_set_coalesce_supported(dev, &coalesce)) 1704 return -EOPNOTSUPP; 1705 1706 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce); 1707 if (!ret) 1708 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL); 1709 return ret; 1710 } 1711 1712 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) 1713 { 1714 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; 1715 1716 if (!dev->ethtool_ops->get_ringparam) 1717 return -EOPNOTSUPP; 1718 1719 dev->ethtool_ops->get_ringparam(dev, &ringparam); 1720 1721 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) 1722 return -EFAULT; 1723 return 0; 1724 } 1725 1726 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) 1727 { 1728 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM }; 1729 int ret; 1730 1731 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam) 1732 return -EOPNOTSUPP; 1733 1734 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) 1735 return -EFAULT; 1736 1737 dev->ethtool_ops->get_ringparam(dev, &max); 1738 1739 /* ensure new ring parameters are within the maximums */ 1740 if (ringparam.rx_pending > max.rx_max_pending || 1741 ringparam.rx_mini_pending > max.rx_mini_max_pending || 1742 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending || 1743 ringparam.tx_pending > max.tx_max_pending) 1744 return -EINVAL; 1745 1746 ret = dev->ethtool_ops->set_ringparam(dev, &ringparam); 1747 if (!ret) 1748 ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL); 1749 return ret; 1750 } 1751 1752 static noinline_for_stack int ethtool_get_channels(struct net_device *dev, 1753 void __user *useraddr) 1754 { 1755 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; 1756 1757 if (!dev->ethtool_ops->get_channels) 1758 return -EOPNOTSUPP; 1759 1760 dev->ethtool_ops->get_channels(dev, &channels); 1761 1762 if (copy_to_user(useraddr, &channels, sizeof(channels))) 1763 return -EFAULT; 1764 return 0; 1765 } 1766 1767 static noinline_for_stack int ethtool_set_channels(struct net_device *dev, 1768 void __user *useraddr) 1769 { 1770 struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS }; 1771 u16 from_channel, to_channel; 1772 u32 max_rx_in_use = 0; 1773 unsigned int i; 1774 int ret; 1775 1776 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) 1777 return -EOPNOTSUPP; 1778 1779 if (copy_from_user(&channels, useraddr, sizeof(channels))) 1780 return -EFAULT; 1781 1782 dev->ethtool_ops->get_channels(dev, &curr); 1783 1784 if (channels.rx_count == curr.rx_count && 1785 channels.tx_count == curr.tx_count && 1786 channels.combined_count == curr.combined_count && 1787 channels.other_count == curr.other_count) 1788 return 0; 1789 1790 /* ensure new counts are within the maximums */ 1791 if (channels.rx_count > curr.max_rx || 1792 channels.tx_count > curr.max_tx || 1793 channels.combined_count > curr.max_combined || 1794 channels.other_count > curr.max_other) 1795 return -EINVAL; 1796 1797 /* ensure there is at least one RX and one TX channel */ 1798 if (!channels.combined_count && 1799 (!channels.rx_count || !channels.tx_count)) 1800 return -EINVAL; 1801 1802 /* ensure the new Rx count fits within the configured Rx flow 1803 * indirection table settings */ 1804 if (netif_is_rxfh_configured(dev) && 1805 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && 1806 (channels.combined_count + channels.rx_count) <= max_rx_in_use) 1807 return -EINVAL; 1808 1809 /* Disabling channels, query zero-copy AF_XDP sockets */ 1810 from_channel = channels.combined_count + 1811 min(channels.rx_count, channels.tx_count); 1812 to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count); 1813 for (i = from_channel; i < to_channel; i++) 1814 if (xsk_get_pool_from_qid(dev, i)) 1815 return -EINVAL; 1816 1817 ret = dev->ethtool_ops->set_channels(dev, &channels); 1818 if (!ret) 1819 ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL); 1820 return ret; 1821 } 1822 1823 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) 1824 { 1825 struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM }; 1826 1827 if (!dev->ethtool_ops->get_pauseparam) 1828 return -EOPNOTSUPP; 1829 1830 dev->ethtool_ops->get_pauseparam(dev, &pauseparam); 1831 1832 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) 1833 return -EFAULT; 1834 return 0; 1835 } 1836 1837 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) 1838 { 1839 struct ethtool_pauseparam pauseparam; 1840 int ret; 1841 1842 if (!dev->ethtool_ops->set_pauseparam) 1843 return -EOPNOTSUPP; 1844 1845 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) 1846 return -EFAULT; 1847 1848 ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam); 1849 if (!ret) 1850 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 1851 return ret; 1852 } 1853 1854 static int ethtool_self_test(struct net_device *dev, char __user *useraddr) 1855 { 1856 struct ethtool_test test; 1857 const struct ethtool_ops *ops = dev->ethtool_ops; 1858 u64 *data; 1859 int ret, test_len; 1860 1861 if (!ops->self_test || !ops->get_sset_count) 1862 return -EOPNOTSUPP; 1863 1864 test_len = ops->get_sset_count(dev, ETH_SS_TEST); 1865 if (test_len < 0) 1866 return test_len; 1867 WARN_ON(test_len == 0); 1868 1869 if (copy_from_user(&test, useraddr, sizeof(test))) 1870 return -EFAULT; 1871 1872 test.len = test_len; 1873 data = kcalloc(test_len, sizeof(u64), GFP_USER); 1874 if (!data) 1875 return -ENOMEM; 1876 1877 netif_testing_on(dev); 1878 ops->self_test(dev, &test, data); 1879 netif_testing_off(dev); 1880 1881 ret = -EFAULT; 1882 if (copy_to_user(useraddr, &test, sizeof(test))) 1883 goto out; 1884 useraddr += sizeof(test); 1885 if (copy_to_user(useraddr, data, test.len * sizeof(u64))) 1886 goto out; 1887 ret = 0; 1888 1889 out: 1890 kfree(data); 1891 return ret; 1892 } 1893 1894 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) 1895 { 1896 struct ethtool_gstrings gstrings; 1897 u8 *data; 1898 int ret; 1899 1900 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) 1901 return -EFAULT; 1902 1903 ret = __ethtool_get_sset_count(dev, gstrings.string_set); 1904 if (ret < 0) 1905 return ret; 1906 if (ret > S32_MAX / ETH_GSTRING_LEN) 1907 return -ENOMEM; 1908 WARN_ON_ONCE(!ret); 1909 1910 gstrings.len = ret; 1911 1912 if (gstrings.len) { 1913 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); 1914 if (!data) 1915 return -ENOMEM; 1916 1917 __ethtool_get_strings(dev, gstrings.string_set, data); 1918 } else { 1919 data = NULL; 1920 } 1921 1922 ret = -EFAULT; 1923 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) 1924 goto out; 1925 useraddr += sizeof(gstrings); 1926 if (gstrings.len && 1927 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) 1928 goto out; 1929 ret = 0; 1930 1931 out: 1932 vfree(data); 1933 return ret; 1934 } 1935 1936 __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...) 1937 { 1938 va_list args; 1939 1940 va_start(args, fmt); 1941 vsnprintf(*data, ETH_GSTRING_LEN, fmt, args); 1942 va_end(args); 1943 1944 *data += ETH_GSTRING_LEN; 1945 } 1946 EXPORT_SYMBOL(ethtool_sprintf); 1947 1948 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) 1949 { 1950 struct ethtool_value id; 1951 static bool busy; 1952 const struct ethtool_ops *ops = dev->ethtool_ops; 1953 int rc; 1954 1955 if (!ops->set_phys_id) 1956 return -EOPNOTSUPP; 1957 1958 if (busy) 1959 return -EBUSY; 1960 1961 if (copy_from_user(&id, useraddr, sizeof(id))) 1962 return -EFAULT; 1963 1964 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); 1965 if (rc < 0) 1966 return rc; 1967 1968 /* Drop the RTNL lock while waiting, but prevent reentry or 1969 * removal of the device. 1970 */ 1971 busy = true; 1972 dev_hold(dev); 1973 rtnl_unlock(); 1974 1975 if (rc == 0) { 1976 /* Driver will handle this itself */ 1977 schedule_timeout_interruptible( 1978 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); 1979 } else { 1980 /* Driver expects to be called at twice the frequency in rc */ 1981 int n = rc * 2, interval = HZ / n; 1982 u64 count = n * id.data, i = 0; 1983 1984 do { 1985 rtnl_lock(); 1986 rc = ops->set_phys_id(dev, 1987 (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); 1988 rtnl_unlock(); 1989 if (rc) 1990 break; 1991 schedule_timeout_interruptible(interval); 1992 } while (!signal_pending(current) && (!id.data || i < count)); 1993 } 1994 1995 rtnl_lock(); 1996 dev_put(dev); 1997 busy = false; 1998 1999 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); 2000 return rc; 2001 } 2002 2003 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) 2004 { 2005 struct ethtool_stats stats; 2006 const struct ethtool_ops *ops = dev->ethtool_ops; 2007 u64 *data; 2008 int ret, n_stats; 2009 2010 if (!ops->get_ethtool_stats || !ops->get_sset_count) 2011 return -EOPNOTSUPP; 2012 2013 n_stats = ops->get_sset_count(dev, ETH_SS_STATS); 2014 if (n_stats < 0) 2015 return n_stats; 2016 if (n_stats > S32_MAX / sizeof(u64)) 2017 return -ENOMEM; 2018 WARN_ON_ONCE(!n_stats); 2019 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2020 return -EFAULT; 2021 2022 stats.n_stats = n_stats; 2023 2024 if (n_stats) { 2025 data = vzalloc(array_size(n_stats, sizeof(u64))); 2026 if (!data) 2027 return -ENOMEM; 2028 ops->get_ethtool_stats(dev, &stats, data); 2029 } else { 2030 data = NULL; 2031 } 2032 2033 ret = -EFAULT; 2034 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2035 goto out; 2036 useraddr += sizeof(stats); 2037 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2038 goto out; 2039 ret = 0; 2040 2041 out: 2042 vfree(data); 2043 return ret; 2044 } 2045 2046 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) 2047 { 2048 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops; 2049 const struct ethtool_ops *ops = dev->ethtool_ops; 2050 struct phy_device *phydev = dev->phydev; 2051 struct ethtool_stats stats; 2052 u64 *data; 2053 int ret, n_stats; 2054 2055 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count)) 2056 return -EOPNOTSUPP; 2057 2058 if (dev->phydev && !ops->get_ethtool_phy_stats && 2059 phy_ops && phy_ops->get_sset_count) 2060 n_stats = phy_ops->get_sset_count(dev->phydev); 2061 else 2062 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 2063 if (n_stats < 0) 2064 return n_stats; 2065 if (n_stats > S32_MAX / sizeof(u64)) 2066 return -ENOMEM; 2067 WARN_ON_ONCE(!n_stats); 2068 2069 if (copy_from_user(&stats, useraddr, sizeof(stats))) 2070 return -EFAULT; 2071 2072 stats.n_stats = n_stats; 2073 2074 if (n_stats) { 2075 data = vzalloc(array_size(n_stats, sizeof(u64))); 2076 if (!data) 2077 return -ENOMEM; 2078 2079 if (dev->phydev && !ops->get_ethtool_phy_stats && 2080 phy_ops && phy_ops->get_stats) { 2081 ret = phy_ops->get_stats(dev->phydev, &stats, data); 2082 if (ret < 0) 2083 goto out; 2084 } else { 2085 ops->get_ethtool_phy_stats(dev, &stats, data); 2086 } 2087 } else { 2088 data = NULL; 2089 } 2090 2091 ret = -EFAULT; 2092 if (copy_to_user(useraddr, &stats, sizeof(stats))) 2093 goto out; 2094 useraddr += sizeof(stats); 2095 if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64)))) 2096 goto out; 2097 ret = 0; 2098 2099 out: 2100 vfree(data); 2101 return ret; 2102 } 2103 2104 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) 2105 { 2106 struct ethtool_perm_addr epaddr; 2107 2108 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) 2109 return -EFAULT; 2110 2111 if (epaddr.size < dev->addr_len) 2112 return -ETOOSMALL; 2113 epaddr.size = dev->addr_len; 2114 2115 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) 2116 return -EFAULT; 2117 useraddr += sizeof(epaddr); 2118 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) 2119 return -EFAULT; 2120 return 0; 2121 } 2122 2123 static int ethtool_get_value(struct net_device *dev, char __user *useraddr, 2124 u32 cmd, u32 (*actor)(struct net_device *)) 2125 { 2126 struct ethtool_value edata = { .cmd = cmd }; 2127 2128 if (!actor) 2129 return -EOPNOTSUPP; 2130 2131 edata.data = actor(dev); 2132 2133 if (copy_to_user(useraddr, &edata, sizeof(edata))) 2134 return -EFAULT; 2135 return 0; 2136 } 2137 2138 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, 2139 void (*actor)(struct net_device *, u32)) 2140 { 2141 struct ethtool_value edata; 2142 2143 if (!actor) 2144 return -EOPNOTSUPP; 2145 2146 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2147 return -EFAULT; 2148 2149 actor(dev, edata.data); 2150 return 0; 2151 } 2152 2153 static int ethtool_set_value(struct net_device *dev, char __user *useraddr, 2154 int (*actor)(struct net_device *, u32)) 2155 { 2156 struct ethtool_value edata; 2157 2158 if (!actor) 2159 return -EOPNOTSUPP; 2160 2161 if (copy_from_user(&edata, useraddr, sizeof(edata))) 2162 return -EFAULT; 2163 2164 return actor(dev, edata.data); 2165 } 2166 2167 static noinline_for_stack int ethtool_flash_device(struct net_device *dev, 2168 char __user *useraddr) 2169 { 2170 struct ethtool_flash efl; 2171 2172 if (copy_from_user(&efl, useraddr, sizeof(efl))) 2173 return -EFAULT; 2174 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; 2175 2176 if (!dev->ethtool_ops->flash_device) 2177 return devlink_compat_flash_update(dev, efl.data); 2178 2179 return dev->ethtool_ops->flash_device(dev, &efl); 2180 } 2181 2182 static int ethtool_set_dump(struct net_device *dev, 2183 void __user *useraddr) 2184 { 2185 struct ethtool_dump dump; 2186 2187 if (!dev->ethtool_ops->set_dump) 2188 return -EOPNOTSUPP; 2189 2190 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2191 return -EFAULT; 2192 2193 return dev->ethtool_ops->set_dump(dev, &dump); 2194 } 2195 2196 static int ethtool_get_dump_flag(struct net_device *dev, 2197 void __user *useraddr) 2198 { 2199 int ret; 2200 struct ethtool_dump dump; 2201 const struct ethtool_ops *ops = dev->ethtool_ops; 2202 2203 if (!ops->get_dump_flag) 2204 return -EOPNOTSUPP; 2205 2206 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2207 return -EFAULT; 2208 2209 ret = ops->get_dump_flag(dev, &dump); 2210 if (ret) 2211 return ret; 2212 2213 if (copy_to_user(useraddr, &dump, sizeof(dump))) 2214 return -EFAULT; 2215 return 0; 2216 } 2217 2218 static int ethtool_get_dump_data(struct net_device *dev, 2219 void __user *useraddr) 2220 { 2221 int ret; 2222 __u32 len; 2223 struct ethtool_dump dump, tmp; 2224 const struct ethtool_ops *ops = dev->ethtool_ops; 2225 void *data = NULL; 2226 2227 if (!ops->get_dump_data || !ops->get_dump_flag) 2228 return -EOPNOTSUPP; 2229 2230 if (copy_from_user(&dump, useraddr, sizeof(dump))) 2231 return -EFAULT; 2232 2233 memset(&tmp, 0, sizeof(tmp)); 2234 tmp.cmd = ETHTOOL_GET_DUMP_FLAG; 2235 ret = ops->get_dump_flag(dev, &tmp); 2236 if (ret) 2237 return ret; 2238 2239 len = min(tmp.len, dump.len); 2240 if (!len) 2241 return -EFAULT; 2242 2243 /* Don't ever let the driver think there's more space available 2244 * than it requested with .get_dump_flag(). 2245 */ 2246 dump.len = len; 2247 2248 /* Always allocate enough space to hold the whole thing so that the 2249 * driver does not need to check the length and bother with partial 2250 * dumping. 2251 */ 2252 data = vzalloc(tmp.len); 2253 if (!data) 2254 return -ENOMEM; 2255 ret = ops->get_dump_data(dev, &dump, data); 2256 if (ret) 2257 goto out; 2258 2259 /* There are two sane possibilities: 2260 * 1. The driver's .get_dump_data() does not touch dump.len. 2261 * 2. Or it may set dump.len to how much it really writes, which 2262 * should be tmp.len (or len if it can do a partial dump). 2263 * In any case respond to userspace with the actual length of data 2264 * it's receiving. 2265 */ 2266 WARN_ON(dump.len != len && dump.len != tmp.len); 2267 dump.len = len; 2268 2269 if (copy_to_user(useraddr, &dump, sizeof(dump))) { 2270 ret = -EFAULT; 2271 goto out; 2272 } 2273 useraddr += offsetof(struct ethtool_dump, data); 2274 if (copy_to_user(useraddr, data, len)) 2275 ret = -EFAULT; 2276 out: 2277 vfree(data); 2278 return ret; 2279 } 2280 2281 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) 2282 { 2283 struct ethtool_ts_info info; 2284 int err; 2285 2286 err = __ethtool_get_ts_info(dev, &info); 2287 if (err) 2288 return err; 2289 2290 if (copy_to_user(useraddr, &info, sizeof(info))) 2291 return -EFAULT; 2292 2293 return 0; 2294 } 2295 2296 int ethtool_get_module_info_call(struct net_device *dev, 2297 struct ethtool_modinfo *modinfo) 2298 { 2299 const struct ethtool_ops *ops = dev->ethtool_ops; 2300 struct phy_device *phydev = dev->phydev; 2301 2302 if (dev->sfp_bus) 2303 return sfp_get_module_info(dev->sfp_bus, modinfo); 2304 2305 if (phydev && phydev->drv && phydev->drv->module_info) 2306 return phydev->drv->module_info(phydev, modinfo); 2307 2308 if (ops->get_module_info) 2309 return ops->get_module_info(dev, modinfo); 2310 2311 return -EOPNOTSUPP; 2312 } 2313 2314 static int ethtool_get_module_info(struct net_device *dev, 2315 void __user *useraddr) 2316 { 2317 int ret; 2318 struct ethtool_modinfo modinfo; 2319 2320 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) 2321 return -EFAULT; 2322 2323 ret = ethtool_get_module_info_call(dev, &modinfo); 2324 if (ret) 2325 return ret; 2326 2327 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) 2328 return -EFAULT; 2329 2330 return 0; 2331 } 2332 2333 int ethtool_get_module_eeprom_call(struct net_device *dev, 2334 struct ethtool_eeprom *ee, u8 *data) 2335 { 2336 const struct ethtool_ops *ops = dev->ethtool_ops; 2337 struct phy_device *phydev = dev->phydev; 2338 2339 if (dev->sfp_bus) 2340 return sfp_get_module_eeprom(dev->sfp_bus, ee, data); 2341 2342 if (phydev && phydev->drv && phydev->drv->module_eeprom) 2343 return phydev->drv->module_eeprom(phydev, ee, data); 2344 2345 if (ops->get_module_eeprom) 2346 return ops->get_module_eeprom(dev, ee, data); 2347 2348 return -EOPNOTSUPP; 2349 } 2350 2351 static int ethtool_get_module_eeprom(struct net_device *dev, 2352 void __user *useraddr) 2353 { 2354 int ret; 2355 struct ethtool_modinfo modinfo; 2356 2357 ret = ethtool_get_module_info_call(dev, &modinfo); 2358 if (ret) 2359 return ret; 2360 2361 return ethtool_get_any_eeprom(dev, useraddr, 2362 ethtool_get_module_eeprom_call, 2363 modinfo.eeprom_len); 2364 } 2365 2366 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) 2367 { 2368 switch (tuna->id) { 2369 case ETHTOOL_RX_COPYBREAK: 2370 case ETHTOOL_TX_COPYBREAK: 2371 if (tuna->len != sizeof(u32) || 2372 tuna->type_id != ETHTOOL_TUNABLE_U32) 2373 return -EINVAL; 2374 break; 2375 case ETHTOOL_PFC_PREVENTION_TOUT: 2376 if (tuna->len != sizeof(u16) || 2377 tuna->type_id != ETHTOOL_TUNABLE_U16) 2378 return -EINVAL; 2379 break; 2380 default: 2381 return -EINVAL; 2382 } 2383 2384 return 0; 2385 } 2386 2387 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) 2388 { 2389 int ret; 2390 struct ethtool_tunable tuna; 2391 const struct ethtool_ops *ops = dev->ethtool_ops; 2392 void *data; 2393 2394 if (!ops->get_tunable) 2395 return -EOPNOTSUPP; 2396 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2397 return -EFAULT; 2398 ret = ethtool_tunable_valid(&tuna); 2399 if (ret) 2400 return ret; 2401 data = kzalloc(tuna.len, GFP_USER); 2402 if (!data) 2403 return -ENOMEM; 2404 ret = ops->get_tunable(dev, &tuna, data); 2405 if (ret) 2406 goto out; 2407 useraddr += sizeof(tuna); 2408 ret = -EFAULT; 2409 if (copy_to_user(useraddr, data, tuna.len)) 2410 goto out; 2411 ret = 0; 2412 2413 out: 2414 kfree(data); 2415 return ret; 2416 } 2417 2418 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) 2419 { 2420 int ret; 2421 struct ethtool_tunable tuna; 2422 const struct ethtool_ops *ops = dev->ethtool_ops; 2423 void *data; 2424 2425 if (!ops->set_tunable) 2426 return -EOPNOTSUPP; 2427 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2428 return -EFAULT; 2429 ret = ethtool_tunable_valid(&tuna); 2430 if (ret) 2431 return ret; 2432 useraddr += sizeof(tuna); 2433 data = memdup_user(useraddr, tuna.len); 2434 if (IS_ERR(data)) 2435 return PTR_ERR(data); 2436 ret = ops->set_tunable(dev, &tuna, data); 2437 2438 kfree(data); 2439 return ret; 2440 } 2441 2442 static noinline_for_stack int 2443 ethtool_get_per_queue_coalesce(struct net_device *dev, 2444 void __user *useraddr, 2445 struct ethtool_per_queue_op *per_queue_opt) 2446 { 2447 u32 bit; 2448 int ret; 2449 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2450 2451 if (!dev->ethtool_ops->get_per_queue_coalesce) 2452 return -EOPNOTSUPP; 2453 2454 useraddr += sizeof(*per_queue_opt); 2455 2456 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, 2457 MAX_NUM_QUEUE); 2458 2459 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2460 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; 2461 2462 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); 2463 if (ret != 0) 2464 return ret; 2465 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) 2466 return -EFAULT; 2467 useraddr += sizeof(coalesce); 2468 } 2469 2470 return 0; 2471 } 2472 2473 static noinline_for_stack int 2474 ethtool_set_per_queue_coalesce(struct net_device *dev, 2475 void __user *useraddr, 2476 struct ethtool_per_queue_op *per_queue_opt) 2477 { 2478 u32 bit; 2479 int i, ret = 0; 2480 int n_queue; 2481 struct ethtool_coalesce *backup = NULL, *tmp = NULL; 2482 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); 2483 2484 if ((!dev->ethtool_ops->set_per_queue_coalesce) || 2485 (!dev->ethtool_ops->get_per_queue_coalesce)) 2486 return -EOPNOTSUPP; 2487 2488 useraddr += sizeof(*per_queue_opt); 2489 2490 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE); 2491 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); 2492 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); 2493 if (!backup) 2494 return -ENOMEM; 2495 2496 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { 2497 struct ethtool_coalesce coalesce; 2498 2499 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); 2500 if (ret != 0) 2501 goto roll_back; 2502 2503 tmp++; 2504 2505 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { 2506 ret = -EFAULT; 2507 goto roll_back; 2508 } 2509 2510 if (!ethtool_set_coalesce_supported(dev, &coalesce)) { 2511 ret = -EOPNOTSUPP; 2512 goto roll_back; 2513 } 2514 2515 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); 2516 if (ret != 0) 2517 goto roll_back; 2518 2519 useraddr += sizeof(coalesce); 2520 } 2521 2522 roll_back: 2523 if (ret != 0) { 2524 tmp = backup; 2525 for_each_set_bit(i, queue_mask, bit) { 2526 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); 2527 tmp++; 2528 } 2529 } 2530 kfree(backup); 2531 2532 return ret; 2533 } 2534 2535 static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev, 2536 void __user *useraddr, u32 sub_cmd) 2537 { 2538 struct ethtool_per_queue_op per_queue_opt; 2539 2540 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) 2541 return -EFAULT; 2542 2543 if (per_queue_opt.sub_command != sub_cmd) 2544 return -EINVAL; 2545 2546 switch (per_queue_opt.sub_command) { 2547 case ETHTOOL_GCOALESCE: 2548 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2549 case ETHTOOL_SCOALESCE: 2550 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); 2551 default: 2552 return -EOPNOTSUPP; 2553 } 2554 } 2555 2556 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) 2557 { 2558 switch (tuna->id) { 2559 case ETHTOOL_PHY_DOWNSHIFT: 2560 case ETHTOOL_PHY_FAST_LINK_DOWN: 2561 if (tuna->len != sizeof(u8) || 2562 tuna->type_id != ETHTOOL_TUNABLE_U8) 2563 return -EINVAL; 2564 break; 2565 case ETHTOOL_PHY_EDPD: 2566 if (tuna->len != sizeof(u16) || 2567 tuna->type_id != ETHTOOL_TUNABLE_U16) 2568 return -EINVAL; 2569 break; 2570 default: 2571 return -EINVAL; 2572 } 2573 2574 return 0; 2575 } 2576 2577 static int get_phy_tunable(struct net_device *dev, void __user *useraddr) 2578 { 2579 struct phy_device *phydev = dev->phydev; 2580 struct ethtool_tunable tuna; 2581 bool phy_drv_tunable; 2582 void *data; 2583 int ret; 2584 2585 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2586 if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable) 2587 return -EOPNOTSUPP; 2588 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2589 return -EFAULT; 2590 ret = ethtool_phy_tunable_valid(&tuna); 2591 if (ret) 2592 return ret; 2593 data = kzalloc(tuna.len, GFP_USER); 2594 if (!data) 2595 return -ENOMEM; 2596 if (phy_drv_tunable) { 2597 mutex_lock(&phydev->lock); 2598 ret = phydev->drv->get_tunable(phydev, &tuna, data); 2599 mutex_unlock(&phydev->lock); 2600 } else { 2601 ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data); 2602 } 2603 if (ret) 2604 goto out; 2605 useraddr += sizeof(tuna); 2606 ret = -EFAULT; 2607 if (copy_to_user(useraddr, data, tuna.len)) 2608 goto out; 2609 ret = 0; 2610 2611 out: 2612 kfree(data); 2613 return ret; 2614 } 2615 2616 static int set_phy_tunable(struct net_device *dev, void __user *useraddr) 2617 { 2618 struct phy_device *phydev = dev->phydev; 2619 struct ethtool_tunable tuna; 2620 bool phy_drv_tunable; 2621 void *data; 2622 int ret; 2623 2624 phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable; 2625 if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable) 2626 return -EOPNOTSUPP; 2627 if (copy_from_user(&tuna, useraddr, sizeof(tuna))) 2628 return -EFAULT; 2629 ret = ethtool_phy_tunable_valid(&tuna); 2630 if (ret) 2631 return ret; 2632 useraddr += sizeof(tuna); 2633 data = memdup_user(useraddr, tuna.len); 2634 if (IS_ERR(data)) 2635 return PTR_ERR(data); 2636 if (phy_drv_tunable) { 2637 mutex_lock(&phydev->lock); 2638 ret = phydev->drv->set_tunable(phydev, &tuna, data); 2639 mutex_unlock(&phydev->lock); 2640 } else { 2641 ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data); 2642 } 2643 2644 kfree(data); 2645 return ret; 2646 } 2647 2648 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr) 2649 { 2650 struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM }; 2651 int rc; 2652 2653 if (!dev->ethtool_ops->get_fecparam) 2654 return -EOPNOTSUPP; 2655 2656 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam); 2657 if (rc) 2658 return rc; 2659 2660 if (WARN_ON_ONCE(fecparam.reserved)) 2661 fecparam.reserved = 0; 2662 2663 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam))) 2664 return -EFAULT; 2665 return 0; 2666 } 2667 2668 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr) 2669 { 2670 struct ethtool_fecparam fecparam; 2671 2672 if (!dev->ethtool_ops->set_fecparam) 2673 return -EOPNOTSUPP; 2674 2675 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam))) 2676 return -EFAULT; 2677 2678 if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE) 2679 return -EINVAL; 2680 2681 fecparam.active_fec = 0; 2682 fecparam.reserved = 0; 2683 2684 return dev->ethtool_ops->set_fecparam(dev, &fecparam); 2685 } 2686 2687 /* The main entry point in this file. Called from net/core/dev_ioctl.c */ 2688 2689 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr) 2690 { 2691 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); 2692 u32 ethcmd, sub_cmd; 2693 int rc; 2694 netdev_features_t old_features; 2695 2696 if (!dev) 2697 return -ENODEV; 2698 2699 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 2700 return -EFAULT; 2701 2702 if (ethcmd == ETHTOOL_PERQUEUE) { 2703 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) 2704 return -EFAULT; 2705 } else { 2706 sub_cmd = ethcmd; 2707 } 2708 /* Allow some commands to be done by anyone */ 2709 switch (sub_cmd) { 2710 case ETHTOOL_GSET: 2711 case ETHTOOL_GDRVINFO: 2712 case ETHTOOL_GMSGLVL: 2713 case ETHTOOL_GLINK: 2714 case ETHTOOL_GCOALESCE: 2715 case ETHTOOL_GRINGPARAM: 2716 case ETHTOOL_GPAUSEPARAM: 2717 case ETHTOOL_GRXCSUM: 2718 case ETHTOOL_GTXCSUM: 2719 case ETHTOOL_GSG: 2720 case ETHTOOL_GSSET_INFO: 2721 case ETHTOOL_GSTRINGS: 2722 case ETHTOOL_GSTATS: 2723 case ETHTOOL_GPHYSTATS: 2724 case ETHTOOL_GTSO: 2725 case ETHTOOL_GPERMADDR: 2726 case ETHTOOL_GUFO: 2727 case ETHTOOL_GGSO: 2728 case ETHTOOL_GGRO: 2729 case ETHTOOL_GFLAGS: 2730 case ETHTOOL_GPFLAGS: 2731 case ETHTOOL_GRXFH: 2732 case ETHTOOL_GRXRINGS: 2733 case ETHTOOL_GRXCLSRLCNT: 2734 case ETHTOOL_GRXCLSRULE: 2735 case ETHTOOL_GRXCLSRLALL: 2736 case ETHTOOL_GRXFHINDIR: 2737 case ETHTOOL_GRSSH: 2738 case ETHTOOL_GFEATURES: 2739 case ETHTOOL_GCHANNELS: 2740 case ETHTOOL_GET_TS_INFO: 2741 case ETHTOOL_GEEE: 2742 case ETHTOOL_GTUNABLE: 2743 case ETHTOOL_PHY_GTUNABLE: 2744 case ETHTOOL_GLINKSETTINGS: 2745 case ETHTOOL_GFECPARAM: 2746 break; 2747 default: 2748 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2749 return -EPERM; 2750 } 2751 2752 if (dev->dev.parent) 2753 pm_runtime_get_sync(dev->dev.parent); 2754 2755 if (!netif_device_present(dev)) { 2756 rc = -ENODEV; 2757 goto out; 2758 } 2759 2760 if (dev->ethtool_ops->begin) { 2761 rc = dev->ethtool_ops->begin(dev); 2762 if (rc < 0) 2763 goto out; 2764 } 2765 old_features = dev->features; 2766 2767 switch (ethcmd) { 2768 case ETHTOOL_GSET: 2769 rc = ethtool_get_settings(dev, useraddr); 2770 break; 2771 case ETHTOOL_SSET: 2772 rc = ethtool_set_settings(dev, useraddr); 2773 break; 2774 case ETHTOOL_GDRVINFO: 2775 rc = ethtool_get_drvinfo(dev, useraddr); 2776 break; 2777 case ETHTOOL_GREGS: 2778 rc = ethtool_get_regs(dev, useraddr); 2779 break; 2780 case ETHTOOL_GWOL: 2781 rc = ethtool_get_wol(dev, useraddr); 2782 break; 2783 case ETHTOOL_SWOL: 2784 rc = ethtool_set_wol(dev, useraddr); 2785 break; 2786 case ETHTOOL_GMSGLVL: 2787 rc = ethtool_get_value(dev, useraddr, ethcmd, 2788 dev->ethtool_ops->get_msglevel); 2789 break; 2790 case ETHTOOL_SMSGLVL: 2791 rc = ethtool_set_value_void(dev, useraddr, 2792 dev->ethtool_ops->set_msglevel); 2793 if (!rc) 2794 ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL); 2795 break; 2796 case ETHTOOL_GEEE: 2797 rc = ethtool_get_eee(dev, useraddr); 2798 break; 2799 case ETHTOOL_SEEE: 2800 rc = ethtool_set_eee(dev, useraddr); 2801 break; 2802 case ETHTOOL_NWAY_RST: 2803 rc = ethtool_nway_reset(dev); 2804 break; 2805 case ETHTOOL_GLINK: 2806 rc = ethtool_get_link(dev, useraddr); 2807 break; 2808 case ETHTOOL_GEEPROM: 2809 rc = ethtool_get_eeprom(dev, useraddr); 2810 break; 2811 case ETHTOOL_SEEPROM: 2812 rc = ethtool_set_eeprom(dev, useraddr); 2813 break; 2814 case ETHTOOL_GCOALESCE: 2815 rc = ethtool_get_coalesce(dev, useraddr); 2816 break; 2817 case ETHTOOL_SCOALESCE: 2818 rc = ethtool_set_coalesce(dev, useraddr); 2819 break; 2820 case ETHTOOL_GRINGPARAM: 2821 rc = ethtool_get_ringparam(dev, useraddr); 2822 break; 2823 case ETHTOOL_SRINGPARAM: 2824 rc = ethtool_set_ringparam(dev, useraddr); 2825 break; 2826 case ETHTOOL_GPAUSEPARAM: 2827 rc = ethtool_get_pauseparam(dev, useraddr); 2828 break; 2829 case ETHTOOL_SPAUSEPARAM: 2830 rc = ethtool_set_pauseparam(dev, useraddr); 2831 break; 2832 case ETHTOOL_TEST: 2833 rc = ethtool_self_test(dev, useraddr); 2834 break; 2835 case ETHTOOL_GSTRINGS: 2836 rc = ethtool_get_strings(dev, useraddr); 2837 break; 2838 case ETHTOOL_PHYS_ID: 2839 rc = ethtool_phys_id(dev, useraddr); 2840 break; 2841 case ETHTOOL_GSTATS: 2842 rc = ethtool_get_stats(dev, useraddr); 2843 break; 2844 case ETHTOOL_GPERMADDR: 2845 rc = ethtool_get_perm_addr(dev, useraddr); 2846 break; 2847 case ETHTOOL_GFLAGS: 2848 rc = ethtool_get_value(dev, useraddr, ethcmd, 2849 __ethtool_get_flags); 2850 break; 2851 case ETHTOOL_SFLAGS: 2852 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); 2853 break; 2854 case ETHTOOL_GPFLAGS: 2855 rc = ethtool_get_value(dev, useraddr, ethcmd, 2856 dev->ethtool_ops->get_priv_flags); 2857 if (!rc) 2858 ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL); 2859 break; 2860 case ETHTOOL_SPFLAGS: 2861 rc = ethtool_set_value(dev, useraddr, 2862 dev->ethtool_ops->set_priv_flags); 2863 break; 2864 case ETHTOOL_GRXFH: 2865 case ETHTOOL_GRXRINGS: 2866 case ETHTOOL_GRXCLSRLCNT: 2867 case ETHTOOL_GRXCLSRULE: 2868 case ETHTOOL_GRXCLSRLALL: 2869 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); 2870 break; 2871 case ETHTOOL_SRXFH: 2872 case ETHTOOL_SRXCLSRLDEL: 2873 case ETHTOOL_SRXCLSRLINS: 2874 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); 2875 break; 2876 case ETHTOOL_FLASHDEV: 2877 rc = ethtool_flash_device(dev, useraddr); 2878 break; 2879 case ETHTOOL_RESET: 2880 rc = ethtool_reset(dev, useraddr); 2881 break; 2882 case ETHTOOL_GSSET_INFO: 2883 rc = ethtool_get_sset_info(dev, useraddr); 2884 break; 2885 case ETHTOOL_GRXFHINDIR: 2886 rc = ethtool_get_rxfh_indir(dev, useraddr); 2887 break; 2888 case ETHTOOL_SRXFHINDIR: 2889 rc = ethtool_set_rxfh_indir(dev, useraddr); 2890 break; 2891 case ETHTOOL_GRSSH: 2892 rc = ethtool_get_rxfh(dev, useraddr); 2893 break; 2894 case ETHTOOL_SRSSH: 2895 rc = ethtool_set_rxfh(dev, useraddr); 2896 break; 2897 case ETHTOOL_GFEATURES: 2898 rc = ethtool_get_features(dev, useraddr); 2899 break; 2900 case ETHTOOL_SFEATURES: 2901 rc = ethtool_set_features(dev, useraddr); 2902 break; 2903 case ETHTOOL_GTXCSUM: 2904 case ETHTOOL_GRXCSUM: 2905 case ETHTOOL_GSG: 2906 case ETHTOOL_GTSO: 2907 case ETHTOOL_GGSO: 2908 case ETHTOOL_GGRO: 2909 rc = ethtool_get_one_feature(dev, useraddr, ethcmd); 2910 break; 2911 case ETHTOOL_STXCSUM: 2912 case ETHTOOL_SRXCSUM: 2913 case ETHTOOL_SSG: 2914 case ETHTOOL_STSO: 2915 case ETHTOOL_SGSO: 2916 case ETHTOOL_SGRO: 2917 rc = ethtool_set_one_feature(dev, useraddr, ethcmd); 2918 break; 2919 case ETHTOOL_GCHANNELS: 2920 rc = ethtool_get_channels(dev, useraddr); 2921 break; 2922 case ETHTOOL_SCHANNELS: 2923 rc = ethtool_set_channels(dev, useraddr); 2924 break; 2925 case ETHTOOL_SET_DUMP: 2926 rc = ethtool_set_dump(dev, useraddr); 2927 break; 2928 case ETHTOOL_GET_DUMP_FLAG: 2929 rc = ethtool_get_dump_flag(dev, useraddr); 2930 break; 2931 case ETHTOOL_GET_DUMP_DATA: 2932 rc = ethtool_get_dump_data(dev, useraddr); 2933 break; 2934 case ETHTOOL_GET_TS_INFO: 2935 rc = ethtool_get_ts_info(dev, useraddr); 2936 break; 2937 case ETHTOOL_GMODULEINFO: 2938 rc = ethtool_get_module_info(dev, useraddr); 2939 break; 2940 case ETHTOOL_GMODULEEEPROM: 2941 rc = ethtool_get_module_eeprom(dev, useraddr); 2942 break; 2943 case ETHTOOL_GTUNABLE: 2944 rc = ethtool_get_tunable(dev, useraddr); 2945 break; 2946 case ETHTOOL_STUNABLE: 2947 rc = ethtool_set_tunable(dev, useraddr); 2948 break; 2949 case ETHTOOL_GPHYSTATS: 2950 rc = ethtool_get_phy_stats(dev, useraddr); 2951 break; 2952 case ETHTOOL_PERQUEUE: 2953 rc = ethtool_set_per_queue(dev, useraddr, sub_cmd); 2954 break; 2955 case ETHTOOL_GLINKSETTINGS: 2956 rc = ethtool_get_link_ksettings(dev, useraddr); 2957 break; 2958 case ETHTOOL_SLINKSETTINGS: 2959 rc = ethtool_set_link_ksettings(dev, useraddr); 2960 break; 2961 case ETHTOOL_PHY_GTUNABLE: 2962 rc = get_phy_tunable(dev, useraddr); 2963 break; 2964 case ETHTOOL_PHY_STUNABLE: 2965 rc = set_phy_tunable(dev, useraddr); 2966 break; 2967 case ETHTOOL_GFECPARAM: 2968 rc = ethtool_get_fecparam(dev, useraddr); 2969 break; 2970 case ETHTOOL_SFECPARAM: 2971 rc = ethtool_set_fecparam(dev, useraddr); 2972 break; 2973 default: 2974 rc = -EOPNOTSUPP; 2975 } 2976 2977 if (dev->ethtool_ops->complete) 2978 dev->ethtool_ops->complete(dev); 2979 2980 if (old_features != dev->features) 2981 netdev_features_change(dev); 2982 out: 2983 if (dev->dev.parent) 2984 pm_runtime_put(dev->dev.parent); 2985 2986 return rc; 2987 } 2988 2989 struct ethtool_rx_flow_key { 2990 struct flow_dissector_key_basic basic; 2991 union { 2992 struct flow_dissector_key_ipv4_addrs ipv4; 2993 struct flow_dissector_key_ipv6_addrs ipv6; 2994 }; 2995 struct flow_dissector_key_ports tp; 2996 struct flow_dissector_key_ip ip; 2997 struct flow_dissector_key_vlan vlan; 2998 struct flow_dissector_key_eth_addrs eth_addrs; 2999 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 3000 3001 struct ethtool_rx_flow_match { 3002 struct flow_dissector dissector; 3003 struct ethtool_rx_flow_key key; 3004 struct ethtool_rx_flow_key mask; 3005 }; 3006 3007 struct ethtool_rx_flow_rule * 3008 ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input) 3009 { 3010 const struct ethtool_rx_flow_spec *fs = input->fs; 3011 static struct in6_addr zero_addr = {}; 3012 struct ethtool_rx_flow_match *match; 3013 struct ethtool_rx_flow_rule *flow; 3014 struct flow_action_entry *act; 3015 3016 flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) + 3017 sizeof(struct ethtool_rx_flow_match), GFP_KERNEL); 3018 if (!flow) 3019 return ERR_PTR(-ENOMEM); 3020 3021 /* ethtool_rx supports only one single action per rule. */ 3022 flow->rule = flow_rule_alloc(1); 3023 if (!flow->rule) { 3024 kfree(flow); 3025 return ERR_PTR(-ENOMEM); 3026 } 3027 3028 match = (struct ethtool_rx_flow_match *)flow->priv; 3029 flow->rule->match.dissector = &match->dissector; 3030 flow->rule->match.mask = &match->mask; 3031 flow->rule->match.key = &match->key; 3032 3033 match->mask.basic.n_proto = htons(0xffff); 3034 3035 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3036 case ETHER_FLOW: { 3037 const struct ethhdr *ether_spec, *ether_m_spec; 3038 3039 ether_spec = &fs->h_u.ether_spec; 3040 ether_m_spec = &fs->m_u.ether_spec; 3041 3042 if (!is_zero_ether_addr(ether_m_spec->h_source)) { 3043 ether_addr_copy(match->key.eth_addrs.src, 3044 ether_spec->h_source); 3045 ether_addr_copy(match->mask.eth_addrs.src, 3046 ether_m_spec->h_source); 3047 } 3048 if (!is_zero_ether_addr(ether_m_spec->h_dest)) { 3049 ether_addr_copy(match->key.eth_addrs.dst, 3050 ether_spec->h_dest); 3051 ether_addr_copy(match->mask.eth_addrs.dst, 3052 ether_m_spec->h_dest); 3053 } 3054 if (ether_m_spec->h_proto) { 3055 match->key.basic.n_proto = ether_spec->h_proto; 3056 match->mask.basic.n_proto = ether_m_spec->h_proto; 3057 } 3058 } 3059 break; 3060 case TCP_V4_FLOW: 3061 case UDP_V4_FLOW: { 3062 const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec; 3063 3064 match->key.basic.n_proto = htons(ETH_P_IP); 3065 3066 v4_spec = &fs->h_u.tcp_ip4_spec; 3067 v4_m_spec = &fs->m_u.tcp_ip4_spec; 3068 3069 if (v4_m_spec->ip4src) { 3070 match->key.ipv4.src = v4_spec->ip4src; 3071 match->mask.ipv4.src = v4_m_spec->ip4src; 3072 } 3073 if (v4_m_spec->ip4dst) { 3074 match->key.ipv4.dst = v4_spec->ip4dst; 3075 match->mask.ipv4.dst = v4_m_spec->ip4dst; 3076 } 3077 if (v4_m_spec->ip4src || 3078 v4_m_spec->ip4dst) { 3079 match->dissector.used_keys |= 3080 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS); 3081 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 3082 offsetof(struct ethtool_rx_flow_key, ipv4); 3083 } 3084 if (v4_m_spec->psrc) { 3085 match->key.tp.src = v4_spec->psrc; 3086 match->mask.tp.src = v4_m_spec->psrc; 3087 } 3088 if (v4_m_spec->pdst) { 3089 match->key.tp.dst = v4_spec->pdst; 3090 match->mask.tp.dst = v4_m_spec->pdst; 3091 } 3092 if (v4_m_spec->psrc || 3093 v4_m_spec->pdst) { 3094 match->dissector.used_keys |= 3095 BIT(FLOW_DISSECTOR_KEY_PORTS); 3096 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3097 offsetof(struct ethtool_rx_flow_key, tp); 3098 } 3099 if (v4_m_spec->tos) { 3100 match->key.ip.tos = v4_spec->tos; 3101 match->mask.ip.tos = v4_m_spec->tos; 3102 match->dissector.used_keys |= 3103 BIT(FLOW_DISSECTOR_KEY_IP); 3104 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3105 offsetof(struct ethtool_rx_flow_key, ip); 3106 } 3107 } 3108 break; 3109 case TCP_V6_FLOW: 3110 case UDP_V6_FLOW: { 3111 const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec; 3112 3113 match->key.basic.n_proto = htons(ETH_P_IPV6); 3114 3115 v6_spec = &fs->h_u.tcp_ip6_spec; 3116 v6_m_spec = &fs->m_u.tcp_ip6_spec; 3117 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) { 3118 memcpy(&match->key.ipv6.src, v6_spec->ip6src, 3119 sizeof(match->key.ipv6.src)); 3120 memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src, 3121 sizeof(match->mask.ipv6.src)); 3122 } 3123 if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { 3124 memcpy(&match->key.ipv6.dst, v6_spec->ip6dst, 3125 sizeof(match->key.ipv6.dst)); 3126 memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst, 3127 sizeof(match->mask.ipv6.dst)); 3128 } 3129 if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) || 3130 memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) { 3131 match->dissector.used_keys |= 3132 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS); 3133 match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] = 3134 offsetof(struct ethtool_rx_flow_key, ipv6); 3135 } 3136 if (v6_m_spec->psrc) { 3137 match->key.tp.src = v6_spec->psrc; 3138 match->mask.tp.src = v6_m_spec->psrc; 3139 } 3140 if (v6_m_spec->pdst) { 3141 match->key.tp.dst = v6_spec->pdst; 3142 match->mask.tp.dst = v6_m_spec->pdst; 3143 } 3144 if (v6_m_spec->psrc || 3145 v6_m_spec->pdst) { 3146 match->dissector.used_keys |= 3147 BIT(FLOW_DISSECTOR_KEY_PORTS); 3148 match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] = 3149 offsetof(struct ethtool_rx_flow_key, tp); 3150 } 3151 if (v6_m_spec->tclass) { 3152 match->key.ip.tos = v6_spec->tclass; 3153 match->mask.ip.tos = v6_m_spec->tclass; 3154 match->dissector.used_keys |= 3155 BIT(FLOW_DISSECTOR_KEY_IP); 3156 match->dissector.offset[FLOW_DISSECTOR_KEY_IP] = 3157 offsetof(struct ethtool_rx_flow_key, ip); 3158 } 3159 } 3160 break; 3161 default: 3162 ethtool_rx_flow_rule_destroy(flow); 3163 return ERR_PTR(-EINVAL); 3164 } 3165 3166 switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) { 3167 case TCP_V4_FLOW: 3168 case TCP_V6_FLOW: 3169 match->key.basic.ip_proto = IPPROTO_TCP; 3170 match->mask.basic.ip_proto = 0xff; 3171 break; 3172 case UDP_V4_FLOW: 3173 case UDP_V6_FLOW: 3174 match->key.basic.ip_proto = IPPROTO_UDP; 3175 match->mask.basic.ip_proto = 0xff; 3176 break; 3177 } 3178 3179 match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC); 3180 match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] = 3181 offsetof(struct ethtool_rx_flow_key, basic); 3182 3183 if (fs->flow_type & FLOW_EXT) { 3184 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3185 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3186 3187 if (ext_m_spec->vlan_etype) { 3188 match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype; 3189 match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype; 3190 } 3191 3192 if (ext_m_spec->vlan_tci) { 3193 match->key.vlan.vlan_id = 3194 ntohs(ext_h_spec->vlan_tci) & 0x0fff; 3195 match->mask.vlan.vlan_id = 3196 ntohs(ext_m_spec->vlan_tci) & 0x0fff; 3197 3198 match->key.vlan.vlan_dei = 3199 !!(ext_h_spec->vlan_tci & htons(0x1000)); 3200 match->mask.vlan.vlan_dei = 3201 !!(ext_m_spec->vlan_tci & htons(0x1000)); 3202 3203 match->key.vlan.vlan_priority = 3204 (ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13; 3205 match->mask.vlan.vlan_priority = 3206 (ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13; 3207 } 3208 3209 if (ext_m_spec->vlan_etype || 3210 ext_m_spec->vlan_tci) { 3211 match->dissector.used_keys |= 3212 BIT(FLOW_DISSECTOR_KEY_VLAN); 3213 match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] = 3214 offsetof(struct ethtool_rx_flow_key, vlan); 3215 } 3216 } 3217 if (fs->flow_type & FLOW_MAC_EXT) { 3218 const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext; 3219 const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext; 3220 3221 memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest, 3222 ETH_ALEN); 3223 memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest, 3224 ETH_ALEN); 3225 3226 match->dissector.used_keys |= 3227 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS); 3228 match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] = 3229 offsetof(struct ethtool_rx_flow_key, eth_addrs); 3230 } 3231 3232 act = &flow->rule->action.entries[0]; 3233 switch (fs->ring_cookie) { 3234 case RX_CLS_FLOW_DISC: 3235 act->id = FLOW_ACTION_DROP; 3236 break; 3237 case RX_CLS_FLOW_WAKE: 3238 act->id = FLOW_ACTION_WAKE; 3239 break; 3240 default: 3241 act->id = FLOW_ACTION_QUEUE; 3242 if (fs->flow_type & FLOW_RSS) 3243 act->queue.ctx = input->rss_ctx; 3244 3245 act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie); 3246 act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie); 3247 break; 3248 } 3249 3250 return flow; 3251 } 3252 EXPORT_SYMBOL(ethtool_rx_flow_rule_create); 3253 3254 void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow) 3255 { 3256 kfree(flow->rule); 3257 kfree(flow); 3258 } 3259 EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy); 3260