ethtool.c 36 KB

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