ethtool.c 39 KB

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