ethtool.c 37 KB

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