ethtool.c 40 KB

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