ethtool.c 39 KB

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