ethtool.c 39 KB

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