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