ethtool.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * net/core/ethtool.c - Ethtool ioctl handler
  3. * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
  4. *
  5. * This file is where we call all the ethtool_ops commands to get
  6. * the information ethtool needs.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/capability.h>
  16. #include <linux/errno.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/slab.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/sched.h>
  25. /*
  26. * Some useful ethtool_ops methods that're device independent.
  27. * If we find that all drivers want to do the same thing here,
  28. * we can turn these into dev_() function calls.
  29. */
  30. u32 ethtool_op_get_link(struct net_device *dev)
  31. {
  32. return netif_carrier_ok(dev) ? 1 : 0;
  33. }
  34. EXPORT_SYMBOL(ethtool_op_get_link);
  35. /* Handlers for each ethtool command */
  36. #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
  37. static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
  38. [NETIF_F_SG_BIT] = "tx-scatter-gather",
  39. [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
  40. [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
  41. [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
  42. [NETIF_F_HIGHDMA_BIT] = "highdma",
  43. [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
  44. [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
  45. [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
  46. [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
  47. [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
  48. [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
  49. [NETIF_F_LLTX_BIT] = "tx-lockless",
  50. [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
  51. [NETIF_F_GRO_BIT] = "rx-gro",
  52. [NETIF_F_LRO_BIT] = "rx-lro",
  53. [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
  54. [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
  55. [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
  56. [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
  57. [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
  58. [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
  59. [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
  60. [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
  61. [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
  62. [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
  63. [NETIF_F_RXHASH_BIT] = "rx-hashing",
  64. [NETIF_F_RXCSUM_BIT] = "rx-checksum",
  65. [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
  66. [NETIF_F_LOOPBACK_BIT] = "loopback",
  67. [NETIF_F_RXFCS_BIT] = "rx-fcs",
  68. [NETIF_F_RXALL_BIT] = "rx-all",
  69. };
  70. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  71. {
  72. struct ethtool_gfeatures cmd = {
  73. .cmd = ETHTOOL_GFEATURES,
  74. .size = ETHTOOL_DEV_FEATURE_WORDS,
  75. };
  76. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  77. u32 __user *sizeaddr;
  78. u32 copy_size;
  79. int i;
  80. /* in case feature bits run out again */
  81. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  82. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  83. features[i].available = (u32)(dev->hw_features >> (32 * i));
  84. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  85. features[i].active = (u32)(dev->features >> (32 * i));
  86. features[i].never_changed =
  87. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  88. }
  89. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  90. if (get_user(copy_size, sizeaddr))
  91. return -EFAULT;
  92. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  93. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  94. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  95. return -EFAULT;
  96. useraddr += sizeof(cmd);
  97. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  98. return -EFAULT;
  99. return 0;
  100. }
  101. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  102. {
  103. struct ethtool_sfeatures cmd;
  104. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  105. netdev_features_t wanted = 0, valid = 0;
  106. int i, ret = 0;
  107. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  108. return -EFAULT;
  109. useraddr += sizeof(cmd);
  110. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  111. return -EINVAL;
  112. if (copy_from_user(features, useraddr, sizeof(features)))
  113. return -EFAULT;
  114. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  115. valid |= (netdev_features_t)features[i].valid << (32 * i);
  116. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  117. }
  118. if (valid & ~NETIF_F_ETHTOOL_BITS)
  119. return -EINVAL;
  120. if (valid & ~dev->hw_features) {
  121. valid &= dev->hw_features;
  122. ret |= ETHTOOL_F_UNSUPPORTED;
  123. }
  124. dev->wanted_features &= ~valid;
  125. dev->wanted_features |= wanted & valid;
  126. __netdev_update_features(dev);
  127. if ((dev->wanted_features ^ dev->features) & valid)
  128. ret |= ETHTOOL_F_WISH;
  129. return ret;
  130. }
  131. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  132. {
  133. const struct ethtool_ops *ops = dev->ethtool_ops;
  134. if (sset == ETH_SS_FEATURES)
  135. return ARRAY_SIZE(netdev_features_strings);
  136. if (ops && ops->get_sset_count && ops->get_strings)
  137. return ops->get_sset_count(dev, sset);
  138. else
  139. return -EOPNOTSUPP;
  140. }
  141. static void __ethtool_get_strings(struct net_device *dev,
  142. u32 stringset, u8 *data)
  143. {
  144. const struct ethtool_ops *ops = dev->ethtool_ops;
  145. if (stringset == ETH_SS_FEATURES)
  146. memcpy(data, netdev_features_strings,
  147. sizeof(netdev_features_strings));
  148. else
  149. /* ops->get_strings is valid because checked earlier */
  150. ops->get_strings(dev, stringset, data);
  151. }
  152. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  153. {
  154. /* feature masks of legacy discrete ethtool ops */
  155. switch (eth_cmd) {
  156. case ETHTOOL_GTXCSUM:
  157. case ETHTOOL_STXCSUM:
  158. return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
  159. case ETHTOOL_GRXCSUM:
  160. case ETHTOOL_SRXCSUM:
  161. return NETIF_F_RXCSUM;
  162. case ETHTOOL_GSG:
  163. case ETHTOOL_SSG:
  164. return NETIF_F_SG;
  165. case ETHTOOL_GTSO:
  166. case ETHTOOL_STSO:
  167. return NETIF_F_ALL_TSO;
  168. case ETHTOOL_GUFO:
  169. case ETHTOOL_SUFO:
  170. return NETIF_F_UFO;
  171. case ETHTOOL_GGSO:
  172. case ETHTOOL_SGSO:
  173. return NETIF_F_GSO;
  174. case ETHTOOL_GGRO:
  175. case ETHTOOL_SGRO:
  176. return NETIF_F_GRO;
  177. default:
  178. BUG();
  179. }
  180. }
  181. static int ethtool_get_one_feature(struct net_device *dev,
  182. char __user *useraddr, u32 ethcmd)
  183. {
  184. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  185. struct ethtool_value edata = {
  186. .cmd = ethcmd,
  187. .data = !!(dev->features & mask),
  188. };
  189. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  190. return -EFAULT;
  191. return 0;
  192. }
  193. static int ethtool_set_one_feature(struct net_device *dev,
  194. void __user *useraddr, u32 ethcmd)
  195. {
  196. struct ethtool_value edata;
  197. netdev_features_t mask;
  198. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  199. return -EFAULT;
  200. mask = ethtool_get_feature_mask(ethcmd);
  201. mask &= dev->hw_features;
  202. if (!mask)
  203. return -EOPNOTSUPP;
  204. if (edata.data)
  205. dev->wanted_features |= mask;
  206. else
  207. dev->wanted_features &= ~mask;
  208. __netdev_update_features(dev);
  209. return 0;
  210. }
  211. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  212. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  213. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
  214. NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
  215. static u32 __ethtool_get_flags(struct net_device *dev)
  216. {
  217. u32 flags = 0;
  218. if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
  219. if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
  220. if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
  221. if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
  222. if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
  223. return flags;
  224. }
  225. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  226. {
  227. netdev_features_t features = 0, changed;
  228. if (data & ~ETH_ALL_FLAGS)
  229. return -EINVAL;
  230. if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
  231. if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
  232. if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
  233. if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
  234. if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
  235. /* allow changing only bits set in hw_features */
  236. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  237. if (changed & ~dev->hw_features)
  238. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  239. dev->wanted_features =
  240. (dev->wanted_features & ~changed) | (features & changed);
  241. __netdev_update_features(dev);
  242. return 0;
  243. }
  244. int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  245. {
  246. ASSERT_RTNL();
  247. if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
  248. return -EOPNOTSUPP;
  249. memset(cmd, 0, sizeof(struct ethtool_cmd));
  250. cmd->cmd = ETHTOOL_GSET;
  251. return dev->ethtool_ops->get_settings(dev, cmd);
  252. }
  253. EXPORT_SYMBOL(__ethtool_get_settings);
  254. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  255. {
  256. int err;
  257. struct ethtool_cmd cmd;
  258. err = __ethtool_get_settings(dev, &cmd);
  259. if (err < 0)
  260. return err;
  261. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  262. return -EFAULT;
  263. return 0;
  264. }
  265. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  266. {
  267. struct ethtool_cmd cmd;
  268. if (!dev->ethtool_ops->set_settings)
  269. return -EOPNOTSUPP;
  270. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  271. return -EFAULT;
  272. return dev->ethtool_ops->set_settings(dev, &cmd);
  273. }
  274. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  275. void __user *useraddr)
  276. {
  277. struct ethtool_drvinfo info;
  278. const struct ethtool_ops *ops = dev->ethtool_ops;
  279. memset(&info, 0, sizeof(info));
  280. info.cmd = ETHTOOL_GDRVINFO;
  281. if (ops && ops->get_drvinfo) {
  282. ops->get_drvinfo(dev, &info);
  283. } else if (dev->dev.parent && dev->dev.parent->driver) {
  284. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  285. sizeof(info.bus_info));
  286. strlcpy(info.driver, dev->dev.parent->driver->name,
  287. sizeof(info.driver));
  288. } else {
  289. return -EOPNOTSUPP;
  290. }
  291. /*
  292. * this method of obtaining string set info is deprecated;
  293. * Use ETHTOOL_GSSET_INFO instead.
  294. */
  295. if (ops && ops->get_sset_count) {
  296. int rc;
  297. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  298. if (rc >= 0)
  299. info.testinfo_len = rc;
  300. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  301. if (rc >= 0)
  302. info.n_stats = rc;
  303. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  304. if (rc >= 0)
  305. info.n_priv_flags = rc;
  306. }
  307. if (ops && ops->get_regs_len)
  308. info.regdump_len = ops->get_regs_len(dev);
  309. if (ops && ops->get_eeprom_len)
  310. info.eedump_len = ops->get_eeprom_len(dev);
  311. if (copy_to_user(useraddr, &info, sizeof(info)))
  312. return -EFAULT;
  313. return 0;
  314. }
  315. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  316. void __user *useraddr)
  317. {
  318. struct ethtool_sset_info info;
  319. u64 sset_mask;
  320. int i, idx = 0, n_bits = 0, ret, rc;
  321. u32 *info_buf = NULL;
  322. if (copy_from_user(&info, useraddr, sizeof(info)))
  323. return -EFAULT;
  324. /* store copy of mask, because we zero struct later on */
  325. sset_mask = info.sset_mask;
  326. if (!sset_mask)
  327. return 0;
  328. /* calculate size of return buffer */
  329. n_bits = hweight64(sset_mask);
  330. memset(&info, 0, sizeof(info));
  331. info.cmd = ETHTOOL_GSSET_INFO;
  332. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  333. if (!info_buf)
  334. return -ENOMEM;
  335. /*
  336. * fill return buffer based on input bitmask and successful
  337. * get_sset_count return
  338. */
  339. for (i = 0; i < 64; i++) {
  340. if (!(sset_mask & (1ULL << i)))
  341. continue;
  342. rc = __ethtool_get_sset_count(dev, i);
  343. if (rc >= 0) {
  344. info.sset_mask |= (1ULL << i);
  345. info_buf[idx++] = rc;
  346. }
  347. }
  348. ret = -EFAULT;
  349. if (copy_to_user(useraddr, &info, sizeof(info)))
  350. goto out;
  351. useraddr += offsetof(struct ethtool_sset_info, data);
  352. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  353. goto out;
  354. ret = 0;
  355. out:
  356. kfree(info_buf);
  357. return ret;
  358. }
  359. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  360. u32 cmd, void __user *useraddr)
  361. {
  362. struct ethtool_rxnfc info;
  363. size_t info_size = sizeof(info);
  364. int rc;
  365. if (!dev->ethtool_ops->set_rxnfc)
  366. return -EOPNOTSUPP;
  367. /* struct ethtool_rxnfc was originally defined for
  368. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  369. * members. User-space might still be using that
  370. * definition. */
  371. if (cmd == ETHTOOL_SRXFH)
  372. info_size = (offsetof(struct ethtool_rxnfc, data) +
  373. sizeof(info.data));
  374. if (copy_from_user(&info, useraddr, info_size))
  375. return -EFAULT;
  376. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  377. if (rc)
  378. return rc;
  379. if (cmd == ETHTOOL_SRXCLSRLINS &&
  380. copy_to_user(useraddr, &info, info_size))
  381. return -EFAULT;
  382. return 0;
  383. }
  384. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  385. u32 cmd, void __user *useraddr)
  386. {
  387. struct ethtool_rxnfc info;
  388. size_t info_size = sizeof(info);
  389. const struct ethtool_ops *ops = dev->ethtool_ops;
  390. int ret;
  391. void *rule_buf = NULL;
  392. if (!ops->get_rxnfc)
  393. return -EOPNOTSUPP;
  394. /* struct ethtool_rxnfc was originally defined for
  395. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  396. * members. User-space might still be using that
  397. * definition. */
  398. if (cmd == ETHTOOL_GRXFH)
  399. info_size = (offsetof(struct ethtool_rxnfc, data) +
  400. sizeof(info.data));
  401. if (copy_from_user(&info, useraddr, info_size))
  402. return -EFAULT;
  403. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  404. if (info.rule_cnt > 0) {
  405. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  406. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  407. GFP_USER);
  408. if (!rule_buf)
  409. return -ENOMEM;
  410. }
  411. }
  412. ret = ops->get_rxnfc(dev, &info, rule_buf);
  413. if (ret < 0)
  414. goto err_out;
  415. ret = -EFAULT;
  416. if (copy_to_user(useraddr, &info, info_size))
  417. goto err_out;
  418. if (rule_buf) {
  419. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  420. if (copy_to_user(useraddr, rule_buf,
  421. info.rule_cnt * sizeof(u32)))
  422. goto err_out;
  423. }
  424. ret = 0;
  425. err_out:
  426. kfree(rule_buf);
  427. return ret;
  428. }
  429. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  430. void __user *useraddr)
  431. {
  432. u32 user_size, dev_size;
  433. u32 *indir;
  434. int ret;
  435. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  436. !dev->ethtool_ops->get_rxfh_indir)
  437. return -EOPNOTSUPP;
  438. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  439. if (dev_size == 0)
  440. return -EOPNOTSUPP;
  441. if (copy_from_user(&user_size,
  442. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  443. sizeof(user_size)))
  444. return -EFAULT;
  445. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  446. &dev_size, sizeof(dev_size)))
  447. return -EFAULT;
  448. /* If the user buffer size is 0, this is just a query for the
  449. * device table size. Otherwise, if it's smaller than the
  450. * device table size it's an error.
  451. */
  452. if (user_size < dev_size)
  453. return user_size == 0 ? 0 : -EINVAL;
  454. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  455. if (!indir)
  456. return -ENOMEM;
  457. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  458. if (ret)
  459. goto out;
  460. if (copy_to_user(useraddr +
  461. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  462. indir, dev_size * sizeof(indir[0])))
  463. ret = -EFAULT;
  464. out:
  465. kfree(indir);
  466. return ret;
  467. }
  468. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  469. void __user *useraddr)
  470. {
  471. struct ethtool_rxnfc rx_rings;
  472. u32 user_size, dev_size, i;
  473. u32 *indir;
  474. int ret;
  475. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  476. !dev->ethtool_ops->set_rxfh_indir ||
  477. !dev->ethtool_ops->get_rxnfc)
  478. return -EOPNOTSUPP;
  479. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  480. if (dev_size == 0)
  481. return -EOPNOTSUPP;
  482. if (copy_from_user(&user_size,
  483. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  484. sizeof(user_size)))
  485. return -EFAULT;
  486. if (user_size != 0 && user_size != dev_size)
  487. return -EINVAL;
  488. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  489. if (!indir)
  490. return -ENOMEM;
  491. rx_rings.cmd = ETHTOOL_GRXRINGS;
  492. ret = dev->ethtool_ops->get_rxnfc(dev, &rx_rings, NULL);
  493. if (ret)
  494. goto out;
  495. if (user_size == 0) {
  496. for (i = 0; i < dev_size; i++)
  497. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  498. } else {
  499. if (copy_from_user(indir,
  500. useraddr +
  501. offsetof(struct ethtool_rxfh_indir,
  502. ring_index[0]),
  503. dev_size * sizeof(indir[0]))) {
  504. ret = -EFAULT;
  505. goto out;
  506. }
  507. /* Validate ring indices */
  508. for (i = 0; i < dev_size; i++) {
  509. if (indir[i] >= rx_rings.data) {
  510. ret = -EINVAL;
  511. goto out;
  512. }
  513. }
  514. }
  515. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  516. out:
  517. kfree(indir);
  518. return ret;
  519. }
  520. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  521. {
  522. struct ethtool_regs regs;
  523. const struct ethtool_ops *ops = dev->ethtool_ops;
  524. void *regbuf;
  525. int reglen, ret;
  526. if (!ops->get_regs || !ops->get_regs_len)
  527. return -EOPNOTSUPP;
  528. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  529. return -EFAULT;
  530. reglen = ops->get_regs_len(dev);
  531. if (regs.len > reglen)
  532. regs.len = reglen;
  533. regbuf = vzalloc(reglen);
  534. if (reglen && !regbuf)
  535. return -ENOMEM;
  536. ops->get_regs(dev, &regs, regbuf);
  537. ret = -EFAULT;
  538. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  539. goto out;
  540. useraddr += offsetof(struct ethtool_regs, data);
  541. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  542. goto out;
  543. ret = 0;
  544. out:
  545. vfree(regbuf);
  546. return ret;
  547. }
  548. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  549. {
  550. struct ethtool_value reset;
  551. int ret;
  552. if (!dev->ethtool_ops->reset)
  553. return -EOPNOTSUPP;
  554. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  555. return -EFAULT;
  556. ret = dev->ethtool_ops->reset(dev, &reset.data);
  557. if (ret)
  558. return ret;
  559. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  560. return -EFAULT;
  561. return 0;
  562. }
  563. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  564. {
  565. struct ethtool_wolinfo wol;
  566. if (!dev->ethtool_ops->get_wol)
  567. return -EOPNOTSUPP;
  568. memset(&wol, 0, sizeof(struct ethtool_wolinfo));
  569. wol.cmd = ETHTOOL_GWOL;
  570. dev->ethtool_ops->get_wol(dev, &wol);
  571. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  572. return -EFAULT;
  573. return 0;
  574. }
  575. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  576. {
  577. struct ethtool_wolinfo wol;
  578. if (!dev->ethtool_ops->set_wol)
  579. return -EOPNOTSUPP;
  580. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  581. return -EFAULT;
  582. return dev->ethtool_ops->set_wol(dev, &wol);
  583. }
  584. static int ethtool_nway_reset(struct net_device *dev)
  585. {
  586. if (!dev->ethtool_ops->nway_reset)
  587. return -EOPNOTSUPP;
  588. return dev->ethtool_ops->nway_reset(dev);
  589. }
  590. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  591. {
  592. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  593. if (!dev->ethtool_ops->get_link)
  594. return -EOPNOTSUPP;
  595. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  596. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  597. return -EFAULT;
  598. return 0;
  599. }
  600. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  601. {
  602. struct ethtool_eeprom eeprom;
  603. const struct ethtool_ops *ops = dev->ethtool_ops;
  604. void __user *userbuf = useraddr + sizeof(eeprom);
  605. u32 bytes_remaining;
  606. u8 *data;
  607. int ret = 0;
  608. if (!ops->get_eeprom || !ops->get_eeprom_len)
  609. return -EOPNOTSUPP;
  610. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  611. return -EFAULT;
  612. /* Check for wrap and zero */
  613. if (eeprom.offset + eeprom.len <= eeprom.offset)
  614. return -EINVAL;
  615. /* Check for exceeding total eeprom len */
  616. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  617. return -EINVAL;
  618. data = kmalloc(PAGE_SIZE, GFP_USER);
  619. if (!data)
  620. return -ENOMEM;
  621. bytes_remaining = eeprom.len;
  622. while (bytes_remaining > 0) {
  623. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  624. ret = ops->get_eeprom(dev, &eeprom, data);
  625. if (ret)
  626. break;
  627. if (copy_to_user(userbuf, data, eeprom.len)) {
  628. ret = -EFAULT;
  629. break;
  630. }
  631. userbuf += eeprom.len;
  632. eeprom.offset += eeprom.len;
  633. bytes_remaining -= eeprom.len;
  634. }
  635. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  636. eeprom.offset -= eeprom.len;
  637. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  638. ret = -EFAULT;
  639. kfree(data);
  640. return ret;
  641. }
  642. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  643. {
  644. struct ethtool_eeprom eeprom;
  645. const struct ethtool_ops *ops = dev->ethtool_ops;
  646. void __user *userbuf = useraddr + sizeof(eeprom);
  647. u32 bytes_remaining;
  648. u8 *data;
  649. int ret = 0;
  650. if (!ops->set_eeprom || !ops->get_eeprom_len)
  651. return -EOPNOTSUPP;
  652. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  653. return -EFAULT;
  654. /* Check for wrap and zero */
  655. if (eeprom.offset + eeprom.len <= eeprom.offset)
  656. return -EINVAL;
  657. /* Check for exceeding total eeprom len */
  658. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  659. return -EINVAL;
  660. data = kmalloc(PAGE_SIZE, GFP_USER);
  661. if (!data)
  662. return -ENOMEM;
  663. bytes_remaining = eeprom.len;
  664. while (bytes_remaining > 0) {
  665. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  666. if (copy_from_user(data, userbuf, eeprom.len)) {
  667. ret = -EFAULT;
  668. break;
  669. }
  670. ret = ops->set_eeprom(dev, &eeprom, data);
  671. if (ret)
  672. break;
  673. userbuf += eeprom.len;
  674. eeprom.offset += eeprom.len;
  675. bytes_remaining -= eeprom.len;
  676. }
  677. kfree(data);
  678. return ret;
  679. }
  680. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  681. void __user *useraddr)
  682. {
  683. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  684. if (!dev->ethtool_ops->get_coalesce)
  685. return -EOPNOTSUPP;
  686. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  687. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  688. return -EFAULT;
  689. return 0;
  690. }
  691. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  692. void __user *useraddr)
  693. {
  694. struct ethtool_coalesce coalesce;
  695. if (!dev->ethtool_ops->set_coalesce)
  696. return -EOPNOTSUPP;
  697. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  698. return -EFAULT;
  699. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  700. }
  701. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  702. {
  703. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  704. if (!dev->ethtool_ops->get_ringparam)
  705. return -EOPNOTSUPP;
  706. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  707. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  708. return -EFAULT;
  709. return 0;
  710. }
  711. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  712. {
  713. struct ethtool_ringparam ringparam;
  714. if (!dev->ethtool_ops->set_ringparam)
  715. return -EOPNOTSUPP;
  716. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  717. return -EFAULT;
  718. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  719. }
  720. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  721. void __user *useraddr)
  722. {
  723. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  724. if (!dev->ethtool_ops->get_channels)
  725. return -EOPNOTSUPP;
  726. dev->ethtool_ops->get_channels(dev, &channels);
  727. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  728. return -EFAULT;
  729. return 0;
  730. }
  731. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  732. void __user *useraddr)
  733. {
  734. struct ethtool_channels channels;
  735. if (!dev->ethtool_ops->set_channels)
  736. return -EOPNOTSUPP;
  737. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  738. return -EFAULT;
  739. return dev->ethtool_ops->set_channels(dev, &channels);
  740. }
  741. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  742. {
  743. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  744. if (!dev->ethtool_ops->get_pauseparam)
  745. return -EOPNOTSUPP;
  746. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  747. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  748. return -EFAULT;
  749. return 0;
  750. }
  751. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  752. {
  753. struct ethtool_pauseparam pauseparam;
  754. if (!dev->ethtool_ops->set_pauseparam)
  755. return -EOPNOTSUPP;
  756. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  757. return -EFAULT;
  758. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  759. }
  760. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  761. {
  762. struct ethtool_test test;
  763. const struct ethtool_ops *ops = dev->ethtool_ops;
  764. u64 *data;
  765. int ret, test_len;
  766. if (!ops->self_test || !ops->get_sset_count)
  767. return -EOPNOTSUPP;
  768. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  769. if (test_len < 0)
  770. return test_len;
  771. WARN_ON(test_len == 0);
  772. if (copy_from_user(&test, useraddr, sizeof(test)))
  773. return -EFAULT;
  774. test.len = test_len;
  775. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  776. if (!data)
  777. return -ENOMEM;
  778. ops->self_test(dev, &test, data);
  779. ret = -EFAULT;
  780. if (copy_to_user(useraddr, &test, sizeof(test)))
  781. goto out;
  782. useraddr += sizeof(test);
  783. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  784. goto out;
  785. ret = 0;
  786. out:
  787. kfree(data);
  788. return ret;
  789. }
  790. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  791. {
  792. struct ethtool_gstrings gstrings;
  793. u8 *data;
  794. int ret;
  795. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  796. return -EFAULT;
  797. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  798. if (ret < 0)
  799. return ret;
  800. gstrings.len = ret;
  801. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  802. if (!data)
  803. return -ENOMEM;
  804. __ethtool_get_strings(dev, gstrings.string_set, data);
  805. ret = -EFAULT;
  806. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  807. goto out;
  808. useraddr += sizeof(gstrings);
  809. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  810. goto out;
  811. ret = 0;
  812. out:
  813. kfree(data);
  814. return ret;
  815. }
  816. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  817. {
  818. struct ethtool_value id;
  819. static bool busy;
  820. int rc;
  821. if (!dev->ethtool_ops->set_phys_id)
  822. return -EOPNOTSUPP;
  823. if (busy)
  824. return -EBUSY;
  825. if (copy_from_user(&id, useraddr, sizeof(id)))
  826. return -EFAULT;
  827. rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  828. if (rc < 0)
  829. return rc;
  830. /* Drop the RTNL lock while waiting, but prevent reentry or
  831. * removal of the device.
  832. */
  833. busy = true;
  834. dev_hold(dev);
  835. rtnl_unlock();
  836. if (rc == 0) {
  837. /* Driver will handle this itself */
  838. schedule_timeout_interruptible(
  839. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  840. } else {
  841. /* Driver expects to be called at twice the frequency in rc */
  842. int n = rc * 2, i, interval = HZ / n;
  843. /* Count down seconds */
  844. do {
  845. /* Count down iterations per second */
  846. i = n;
  847. do {
  848. rtnl_lock();
  849. rc = dev->ethtool_ops->set_phys_id(dev,
  850. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  851. rtnl_unlock();
  852. if (rc)
  853. break;
  854. schedule_timeout_interruptible(interval);
  855. } while (!signal_pending(current) && --i != 0);
  856. } while (!signal_pending(current) &&
  857. (id.data == 0 || --id.data != 0));
  858. }
  859. rtnl_lock();
  860. dev_put(dev);
  861. busy = false;
  862. (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  863. return rc;
  864. }
  865. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  866. {
  867. struct ethtool_stats stats;
  868. const struct ethtool_ops *ops = dev->ethtool_ops;
  869. u64 *data;
  870. int ret, n_stats;
  871. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  872. return -EOPNOTSUPP;
  873. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  874. if (n_stats < 0)
  875. return n_stats;
  876. WARN_ON(n_stats == 0);
  877. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  878. return -EFAULT;
  879. stats.n_stats = n_stats;
  880. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  881. if (!data)
  882. return -ENOMEM;
  883. ops->get_ethtool_stats(dev, &stats, data);
  884. ret = -EFAULT;
  885. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  886. goto out;
  887. useraddr += sizeof(stats);
  888. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  889. goto out;
  890. ret = 0;
  891. out:
  892. kfree(data);
  893. return ret;
  894. }
  895. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  896. {
  897. struct ethtool_perm_addr epaddr;
  898. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  899. return -EFAULT;
  900. if (epaddr.size < dev->addr_len)
  901. return -ETOOSMALL;
  902. epaddr.size = dev->addr_len;
  903. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  904. return -EFAULT;
  905. useraddr += sizeof(epaddr);
  906. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  907. return -EFAULT;
  908. return 0;
  909. }
  910. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  911. u32 cmd, u32 (*actor)(struct net_device *))
  912. {
  913. struct ethtool_value edata = { .cmd = cmd };
  914. if (!actor)
  915. return -EOPNOTSUPP;
  916. edata.data = actor(dev);
  917. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  918. return -EFAULT;
  919. return 0;
  920. }
  921. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  922. void (*actor)(struct net_device *, u32))
  923. {
  924. struct ethtool_value edata;
  925. if (!actor)
  926. return -EOPNOTSUPP;
  927. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  928. return -EFAULT;
  929. actor(dev, edata.data);
  930. return 0;
  931. }
  932. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  933. int (*actor)(struct net_device *, u32))
  934. {
  935. struct ethtool_value edata;
  936. if (!actor)
  937. return -EOPNOTSUPP;
  938. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  939. return -EFAULT;
  940. return actor(dev, edata.data);
  941. }
  942. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  943. char __user *useraddr)
  944. {
  945. struct ethtool_flash efl;
  946. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  947. return -EFAULT;
  948. if (!dev->ethtool_ops->flash_device)
  949. return -EOPNOTSUPP;
  950. efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  951. return dev->ethtool_ops->flash_device(dev, &efl);
  952. }
  953. static int ethtool_set_dump(struct net_device *dev,
  954. void __user *useraddr)
  955. {
  956. struct ethtool_dump dump;
  957. if (!dev->ethtool_ops->set_dump)
  958. return -EOPNOTSUPP;
  959. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  960. return -EFAULT;
  961. return dev->ethtool_ops->set_dump(dev, &dump);
  962. }
  963. static int ethtool_get_dump_flag(struct net_device *dev,
  964. void __user *useraddr)
  965. {
  966. int ret;
  967. struct ethtool_dump dump;
  968. const struct ethtool_ops *ops = dev->ethtool_ops;
  969. if (!dev->ethtool_ops->get_dump_flag)
  970. return -EOPNOTSUPP;
  971. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  972. return -EFAULT;
  973. ret = ops->get_dump_flag(dev, &dump);
  974. if (ret)
  975. return ret;
  976. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  977. return -EFAULT;
  978. return 0;
  979. }
  980. static int ethtool_get_dump_data(struct net_device *dev,
  981. void __user *useraddr)
  982. {
  983. int ret;
  984. __u32 len;
  985. struct ethtool_dump dump, tmp;
  986. const struct ethtool_ops *ops = dev->ethtool_ops;
  987. void *data = NULL;
  988. if (!dev->ethtool_ops->get_dump_data ||
  989. !dev->ethtool_ops->get_dump_flag)
  990. return -EOPNOTSUPP;
  991. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  992. return -EFAULT;
  993. memset(&tmp, 0, sizeof(tmp));
  994. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  995. ret = ops->get_dump_flag(dev, &tmp);
  996. if (ret)
  997. return ret;
  998. len = (tmp.len > dump.len) ? dump.len : tmp.len;
  999. if (!len)
  1000. return -EFAULT;
  1001. data = vzalloc(tmp.len);
  1002. if (!data)
  1003. return -ENOMEM;
  1004. ret = ops->get_dump_data(dev, &dump, data);
  1005. if (ret)
  1006. goto out;
  1007. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1008. ret = -EFAULT;
  1009. goto out;
  1010. }
  1011. useraddr += offsetof(struct ethtool_dump, data);
  1012. if (copy_to_user(useraddr, data, len))
  1013. ret = -EFAULT;
  1014. out:
  1015. vfree(data);
  1016. return ret;
  1017. }
  1018. /* The main entry point in this file. Called from net/core/dev.c */
  1019. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1020. {
  1021. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1022. void __user *useraddr = ifr->ifr_data;
  1023. u32 ethcmd;
  1024. int rc;
  1025. netdev_features_t old_features;
  1026. if (!dev || !netif_device_present(dev))
  1027. return -ENODEV;
  1028. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1029. return -EFAULT;
  1030. if (!dev->ethtool_ops) {
  1031. /* ETHTOOL_GDRVINFO does not require any driver support.
  1032. * It is also unprivileged and does not change anything,
  1033. * so we can take a shortcut to it. */
  1034. if (ethcmd == ETHTOOL_GDRVINFO)
  1035. return ethtool_get_drvinfo(dev, useraddr);
  1036. else
  1037. return -EOPNOTSUPP;
  1038. }
  1039. /* Allow some commands to be done by anyone */
  1040. switch (ethcmd) {
  1041. case ETHTOOL_GSET:
  1042. case ETHTOOL_GDRVINFO:
  1043. case ETHTOOL_GMSGLVL:
  1044. case ETHTOOL_GCOALESCE:
  1045. case ETHTOOL_GRINGPARAM:
  1046. case ETHTOOL_GPAUSEPARAM:
  1047. case ETHTOOL_GRXCSUM:
  1048. case ETHTOOL_GTXCSUM:
  1049. case ETHTOOL_GSG:
  1050. case ETHTOOL_GSSET_INFO:
  1051. case ETHTOOL_GSTRINGS:
  1052. case ETHTOOL_GTSO:
  1053. case ETHTOOL_GPERMADDR:
  1054. case ETHTOOL_GUFO:
  1055. case ETHTOOL_GGSO:
  1056. case ETHTOOL_GGRO:
  1057. case ETHTOOL_GFLAGS:
  1058. case ETHTOOL_GPFLAGS:
  1059. case ETHTOOL_GRXFH:
  1060. case ETHTOOL_GRXRINGS:
  1061. case ETHTOOL_GRXCLSRLCNT:
  1062. case ETHTOOL_GRXCLSRULE:
  1063. case ETHTOOL_GRXCLSRLALL:
  1064. case ETHTOOL_GFEATURES:
  1065. break;
  1066. default:
  1067. if (!capable(CAP_NET_ADMIN))
  1068. return -EPERM;
  1069. }
  1070. if (dev->ethtool_ops->begin) {
  1071. rc = dev->ethtool_ops->begin(dev);
  1072. if (rc < 0)
  1073. return rc;
  1074. }
  1075. old_features = dev->features;
  1076. switch (ethcmd) {
  1077. case ETHTOOL_GSET:
  1078. rc = ethtool_get_settings(dev, useraddr);
  1079. break;
  1080. case ETHTOOL_SSET:
  1081. rc = ethtool_set_settings(dev, useraddr);
  1082. break;
  1083. case ETHTOOL_GDRVINFO:
  1084. rc = ethtool_get_drvinfo(dev, useraddr);
  1085. break;
  1086. case ETHTOOL_GREGS:
  1087. rc = ethtool_get_regs(dev, useraddr);
  1088. break;
  1089. case ETHTOOL_GWOL:
  1090. rc = ethtool_get_wol(dev, useraddr);
  1091. break;
  1092. case ETHTOOL_SWOL:
  1093. rc = ethtool_set_wol(dev, useraddr);
  1094. break;
  1095. case ETHTOOL_GMSGLVL:
  1096. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1097. dev->ethtool_ops->get_msglevel);
  1098. break;
  1099. case ETHTOOL_SMSGLVL:
  1100. rc = ethtool_set_value_void(dev, useraddr,
  1101. dev->ethtool_ops->set_msglevel);
  1102. break;
  1103. case ETHTOOL_NWAY_RST:
  1104. rc = ethtool_nway_reset(dev);
  1105. break;
  1106. case ETHTOOL_GLINK:
  1107. rc = ethtool_get_link(dev, useraddr);
  1108. break;
  1109. case ETHTOOL_GEEPROM:
  1110. rc = ethtool_get_eeprom(dev, useraddr);
  1111. break;
  1112. case ETHTOOL_SEEPROM:
  1113. rc = ethtool_set_eeprom(dev, useraddr);
  1114. break;
  1115. case ETHTOOL_GCOALESCE:
  1116. rc = ethtool_get_coalesce(dev, useraddr);
  1117. break;
  1118. case ETHTOOL_SCOALESCE:
  1119. rc = ethtool_set_coalesce(dev, useraddr);
  1120. break;
  1121. case ETHTOOL_GRINGPARAM:
  1122. rc = ethtool_get_ringparam(dev, useraddr);
  1123. break;
  1124. case ETHTOOL_SRINGPARAM:
  1125. rc = ethtool_set_ringparam(dev, useraddr);
  1126. break;
  1127. case ETHTOOL_GPAUSEPARAM:
  1128. rc = ethtool_get_pauseparam(dev, useraddr);
  1129. break;
  1130. case ETHTOOL_SPAUSEPARAM:
  1131. rc = ethtool_set_pauseparam(dev, useraddr);
  1132. break;
  1133. case ETHTOOL_TEST:
  1134. rc = ethtool_self_test(dev, useraddr);
  1135. break;
  1136. case ETHTOOL_GSTRINGS:
  1137. rc = ethtool_get_strings(dev, useraddr);
  1138. break;
  1139. case ETHTOOL_PHYS_ID:
  1140. rc = ethtool_phys_id(dev, useraddr);
  1141. break;
  1142. case ETHTOOL_GSTATS:
  1143. rc = ethtool_get_stats(dev, useraddr);
  1144. break;
  1145. case ETHTOOL_GPERMADDR:
  1146. rc = ethtool_get_perm_addr(dev, useraddr);
  1147. break;
  1148. case ETHTOOL_GFLAGS:
  1149. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1150. __ethtool_get_flags);
  1151. break;
  1152. case ETHTOOL_SFLAGS:
  1153. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1154. break;
  1155. case ETHTOOL_GPFLAGS:
  1156. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1157. dev->ethtool_ops->get_priv_flags);
  1158. break;
  1159. case ETHTOOL_SPFLAGS:
  1160. rc = ethtool_set_value(dev, useraddr,
  1161. dev->ethtool_ops->set_priv_flags);
  1162. break;
  1163. case ETHTOOL_GRXFH:
  1164. case ETHTOOL_GRXRINGS:
  1165. case ETHTOOL_GRXCLSRLCNT:
  1166. case ETHTOOL_GRXCLSRULE:
  1167. case ETHTOOL_GRXCLSRLALL:
  1168. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1169. break;
  1170. case ETHTOOL_SRXFH:
  1171. case ETHTOOL_SRXCLSRLDEL:
  1172. case ETHTOOL_SRXCLSRLINS:
  1173. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1174. break;
  1175. case ETHTOOL_FLASHDEV:
  1176. rc = ethtool_flash_device(dev, useraddr);
  1177. break;
  1178. case ETHTOOL_RESET:
  1179. rc = ethtool_reset(dev, useraddr);
  1180. break;
  1181. case ETHTOOL_GSSET_INFO:
  1182. rc = ethtool_get_sset_info(dev, useraddr);
  1183. break;
  1184. case ETHTOOL_GRXFHINDIR:
  1185. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1186. break;
  1187. case ETHTOOL_SRXFHINDIR:
  1188. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1189. break;
  1190. case ETHTOOL_GFEATURES:
  1191. rc = ethtool_get_features(dev, useraddr);
  1192. break;
  1193. case ETHTOOL_SFEATURES:
  1194. rc = ethtool_set_features(dev, useraddr);
  1195. break;
  1196. case ETHTOOL_GTXCSUM:
  1197. case ETHTOOL_GRXCSUM:
  1198. case ETHTOOL_GSG:
  1199. case ETHTOOL_GTSO:
  1200. case ETHTOOL_GUFO:
  1201. case ETHTOOL_GGSO:
  1202. case ETHTOOL_GGRO:
  1203. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1204. break;
  1205. case ETHTOOL_STXCSUM:
  1206. case ETHTOOL_SRXCSUM:
  1207. case ETHTOOL_SSG:
  1208. case ETHTOOL_STSO:
  1209. case ETHTOOL_SUFO:
  1210. case ETHTOOL_SGSO:
  1211. case ETHTOOL_SGRO:
  1212. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1213. break;
  1214. case ETHTOOL_GCHANNELS:
  1215. rc = ethtool_get_channels(dev, useraddr);
  1216. break;
  1217. case ETHTOOL_SCHANNELS:
  1218. rc = ethtool_set_channels(dev, useraddr);
  1219. break;
  1220. case ETHTOOL_SET_DUMP:
  1221. rc = ethtool_set_dump(dev, useraddr);
  1222. break;
  1223. case ETHTOOL_GET_DUMP_FLAG:
  1224. rc = ethtool_get_dump_flag(dev, useraddr);
  1225. break;
  1226. case ETHTOOL_GET_DUMP_DATA:
  1227. rc = ethtool_get_dump_data(dev, useraddr);
  1228. break;
  1229. default:
  1230. rc = -EOPNOTSUPP;
  1231. }
  1232. if (dev->ethtool_ops->complete)
  1233. dev->ethtool_ops->complete(dev);
  1234. if (old_features != dev->features)
  1235. netdev_features_change(dev);
  1236. return rc;
  1237. }