ethtool.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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 ((NETDEV_FEATURE_COUNT + 31) / 32)
  37. static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
  38. [NETIF_F_SG_BIT] = "tx-scatter-gather",
  39. [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
  40. [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
  41. [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
  42. [NETIF_F_HIGHDMA_BIT] = "highdma",
  43. [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
  44. [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
  45. [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
  46. [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
  47. [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
  48. [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
  49. [NETIF_F_LLTX_BIT] = "tx-lockless",
  50. [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
  51. [NETIF_F_GRO_BIT] = "rx-gro",
  52. [NETIF_F_LRO_BIT] = "rx-lro",
  53. [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
  54. [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
  55. [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
  56. [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
  57. [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
  58. [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
  59. [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
  60. [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
  61. [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
  62. [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
  63. [NETIF_F_RXHASH_BIT] = "rx-hashing",
  64. [NETIF_F_RXCSUM_BIT] = "rx-checksum",
  65. [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
  66. [NETIF_F_LOOPBACK_BIT] = "loopback",
  67. };
  68. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  69. {
  70. struct ethtool_gfeatures cmd = {
  71. .cmd = ETHTOOL_GFEATURES,
  72. .size = ETHTOOL_DEV_FEATURE_WORDS,
  73. };
  74. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  75. u32 __user *sizeaddr;
  76. u32 copy_size;
  77. int i;
  78. /* in case feature bits run out again */
  79. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  80. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  81. features[i].available = (u32)(dev->hw_features >> (32 * i));
  82. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  83. features[i].active = (u32)(dev->features >> (32 * i));
  84. features[i].never_changed =
  85. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  86. }
  87. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  88. if (get_user(copy_size, sizeaddr))
  89. return -EFAULT;
  90. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  91. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  92. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  93. return -EFAULT;
  94. useraddr += sizeof(cmd);
  95. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  96. return -EFAULT;
  97. return 0;
  98. }
  99. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  100. {
  101. struct ethtool_sfeatures cmd;
  102. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  103. netdev_features_t wanted = 0, valid = 0;
  104. int i, ret = 0;
  105. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  106. return -EFAULT;
  107. useraddr += sizeof(cmd);
  108. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  109. return -EINVAL;
  110. if (copy_from_user(features, useraddr, sizeof(features)))
  111. return -EFAULT;
  112. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  113. valid |= (netdev_features_t)features[i].valid << (32 * i);
  114. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  115. }
  116. if (valid & ~NETIF_F_ETHTOOL_BITS)
  117. return -EINVAL;
  118. if (valid & ~dev->hw_features) {
  119. valid &= dev->hw_features;
  120. ret |= ETHTOOL_F_UNSUPPORTED;
  121. }
  122. dev->wanted_features &= ~valid;
  123. dev->wanted_features |= wanted & valid;
  124. __netdev_update_features(dev);
  125. if ((dev->wanted_features ^ dev->features) & valid)
  126. ret |= ETHTOOL_F_WISH;
  127. return ret;
  128. }
  129. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  130. {
  131. const struct ethtool_ops *ops = dev->ethtool_ops;
  132. if (sset == ETH_SS_FEATURES)
  133. return ARRAY_SIZE(netdev_features_strings);
  134. if (ops && ops->get_sset_count && ops->get_strings)
  135. return ops->get_sset_count(dev, sset);
  136. else
  137. return -EOPNOTSUPP;
  138. }
  139. static void __ethtool_get_strings(struct net_device *dev,
  140. u32 stringset, u8 *data)
  141. {
  142. const struct ethtool_ops *ops = dev->ethtool_ops;
  143. if (stringset == ETH_SS_FEATURES)
  144. memcpy(data, netdev_features_strings,
  145. sizeof(netdev_features_strings));
  146. else
  147. /* ops->get_strings is valid because checked earlier */
  148. ops->get_strings(dev, stringset, data);
  149. }
  150. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  151. {
  152. /* feature masks of legacy discrete ethtool ops */
  153. switch (eth_cmd) {
  154. case ETHTOOL_GTXCSUM:
  155. case ETHTOOL_STXCSUM:
  156. return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
  157. case ETHTOOL_GRXCSUM:
  158. case ETHTOOL_SRXCSUM:
  159. return NETIF_F_RXCSUM;
  160. case ETHTOOL_GSG:
  161. case ETHTOOL_SSG:
  162. return NETIF_F_SG;
  163. case ETHTOOL_GTSO:
  164. case ETHTOOL_STSO:
  165. return NETIF_F_ALL_TSO;
  166. case ETHTOOL_GUFO:
  167. case ETHTOOL_SUFO:
  168. return NETIF_F_UFO;
  169. case ETHTOOL_GGSO:
  170. case ETHTOOL_SGSO:
  171. return NETIF_F_GSO;
  172. case ETHTOOL_GGRO:
  173. case ETHTOOL_SGRO:
  174. return NETIF_F_GRO;
  175. default:
  176. BUG();
  177. }
  178. }
  179. static int ethtool_get_one_feature(struct net_device *dev,
  180. char __user *useraddr, u32 ethcmd)
  181. {
  182. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  183. struct ethtool_value edata = {
  184. .cmd = ethcmd,
  185. .data = !!(dev->features & mask),
  186. };
  187. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  188. return -EFAULT;
  189. return 0;
  190. }
  191. static int ethtool_set_one_feature(struct net_device *dev,
  192. void __user *useraddr, u32 ethcmd)
  193. {
  194. struct ethtool_value edata;
  195. netdev_features_t mask;
  196. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  197. return -EFAULT;
  198. mask = ethtool_get_feature_mask(ethcmd);
  199. mask &= dev->hw_features;
  200. if (!mask)
  201. return -EOPNOTSUPP;
  202. if (edata.data)
  203. dev->wanted_features |= mask;
  204. else
  205. dev->wanted_features &= ~mask;
  206. __netdev_update_features(dev);
  207. return 0;
  208. }
  209. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  210. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  211. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
  212. NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
  213. static u32 __ethtool_get_flags(struct net_device *dev)
  214. {
  215. u32 flags = 0;
  216. if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
  217. if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
  218. if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
  219. if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
  220. if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
  221. return flags;
  222. }
  223. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  224. {
  225. netdev_features_t features = 0, changed;
  226. if (data & ~ETH_ALL_FLAGS)
  227. return -EINVAL;
  228. if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
  229. if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
  230. if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
  231. if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
  232. if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
  233. /* allow changing only bits set in hw_features */
  234. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  235. if (changed & ~dev->hw_features)
  236. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  237. dev->wanted_features =
  238. (dev->wanted_features & ~changed) | (features & changed);
  239. __netdev_update_features(dev);
  240. return 0;
  241. }
  242. int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  243. {
  244. ASSERT_RTNL();
  245. if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
  246. return -EOPNOTSUPP;
  247. memset(cmd, 0, sizeof(struct ethtool_cmd));
  248. cmd->cmd = ETHTOOL_GSET;
  249. return dev->ethtool_ops->get_settings(dev, cmd);
  250. }
  251. EXPORT_SYMBOL(__ethtool_get_settings);
  252. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  253. {
  254. int err;
  255. struct ethtool_cmd cmd;
  256. err = __ethtool_get_settings(dev, &cmd);
  257. if (err < 0)
  258. return err;
  259. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  260. return -EFAULT;
  261. return 0;
  262. }
  263. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  264. {
  265. struct ethtool_cmd cmd;
  266. if (!dev->ethtool_ops->set_settings)
  267. return -EOPNOTSUPP;
  268. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  269. return -EFAULT;
  270. return dev->ethtool_ops->set_settings(dev, &cmd);
  271. }
  272. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  273. void __user *useraddr)
  274. {
  275. struct ethtool_drvinfo info;
  276. const struct ethtool_ops *ops = dev->ethtool_ops;
  277. memset(&info, 0, sizeof(info));
  278. info.cmd = ETHTOOL_GDRVINFO;
  279. if (ops && ops->get_drvinfo) {
  280. ops->get_drvinfo(dev, &info);
  281. } else if (dev->dev.parent && dev->dev.parent->driver) {
  282. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  283. sizeof(info.bus_info));
  284. strlcpy(info.driver, dev->dev.parent->driver->name,
  285. sizeof(info.driver));
  286. } else {
  287. return -EOPNOTSUPP;
  288. }
  289. /*
  290. * this method of obtaining string set info is deprecated;
  291. * Use ETHTOOL_GSSET_INFO instead.
  292. */
  293. if (ops && ops->get_sset_count) {
  294. int rc;
  295. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  296. if (rc >= 0)
  297. info.testinfo_len = rc;
  298. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  299. if (rc >= 0)
  300. info.n_stats = rc;
  301. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  302. if (rc >= 0)
  303. info.n_priv_flags = rc;
  304. }
  305. if (ops && ops->get_regs_len)
  306. info.regdump_len = ops->get_regs_len(dev);
  307. if (ops && ops->get_eeprom_len)
  308. info.eedump_len = ops->get_eeprom_len(dev);
  309. if (copy_to_user(useraddr, &info, sizeof(info)))
  310. return -EFAULT;
  311. return 0;
  312. }
  313. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  314. void __user *useraddr)
  315. {
  316. struct ethtool_sset_info info;
  317. u64 sset_mask;
  318. int i, idx = 0, n_bits = 0, ret, rc;
  319. u32 *info_buf = NULL;
  320. if (copy_from_user(&info, useraddr, sizeof(info)))
  321. return -EFAULT;
  322. /* store copy of mask, because we zero struct later on */
  323. sset_mask = info.sset_mask;
  324. if (!sset_mask)
  325. return 0;
  326. /* calculate size of return buffer */
  327. n_bits = hweight64(sset_mask);
  328. memset(&info, 0, sizeof(info));
  329. info.cmd = ETHTOOL_GSSET_INFO;
  330. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  331. if (!info_buf)
  332. return -ENOMEM;
  333. /*
  334. * fill return buffer based on input bitmask and successful
  335. * get_sset_count return
  336. */
  337. for (i = 0; i < 64; i++) {
  338. if (!(sset_mask & (1ULL << i)))
  339. continue;
  340. rc = __ethtool_get_sset_count(dev, i);
  341. if (rc >= 0) {
  342. info.sset_mask |= (1ULL << i);
  343. info_buf[idx++] = rc;
  344. }
  345. }
  346. ret = -EFAULT;
  347. if (copy_to_user(useraddr, &info, sizeof(info)))
  348. goto out;
  349. useraddr += offsetof(struct ethtool_sset_info, data);
  350. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  351. goto out;
  352. ret = 0;
  353. out:
  354. kfree(info_buf);
  355. return ret;
  356. }
  357. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  358. u32 cmd, void __user *useraddr)
  359. {
  360. struct ethtool_rxnfc info;
  361. size_t info_size = sizeof(info);
  362. int rc;
  363. if (!dev->ethtool_ops->set_rxnfc)
  364. return -EOPNOTSUPP;
  365. /* struct ethtool_rxnfc was originally defined for
  366. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  367. * members. User-space might still be using that
  368. * definition. */
  369. if (cmd == ETHTOOL_SRXFH)
  370. info_size = (offsetof(struct ethtool_rxnfc, data) +
  371. sizeof(info.data));
  372. if (copy_from_user(&info, useraddr, info_size))
  373. return -EFAULT;
  374. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  375. if (rc)
  376. return rc;
  377. if (cmd == ETHTOOL_SRXCLSRLINS &&
  378. copy_to_user(useraddr, &info, info_size))
  379. return -EFAULT;
  380. return 0;
  381. }
  382. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  383. u32 cmd, void __user *useraddr)
  384. {
  385. struct ethtool_rxnfc info;
  386. size_t info_size = sizeof(info);
  387. const struct ethtool_ops *ops = dev->ethtool_ops;
  388. int ret;
  389. void *rule_buf = NULL;
  390. if (!ops->get_rxnfc)
  391. return -EOPNOTSUPP;
  392. /* struct ethtool_rxnfc was originally defined for
  393. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  394. * members. User-space might still be using that
  395. * definition. */
  396. if (cmd == ETHTOOL_GRXFH)
  397. info_size = (offsetof(struct ethtool_rxnfc, data) +
  398. sizeof(info.data));
  399. if (copy_from_user(&info, useraddr, info_size))
  400. return -EFAULT;
  401. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  402. if (info.rule_cnt > 0) {
  403. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  404. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  405. GFP_USER);
  406. if (!rule_buf)
  407. return -ENOMEM;
  408. }
  409. }
  410. ret = ops->get_rxnfc(dev, &info, rule_buf);
  411. if (ret < 0)
  412. goto err_out;
  413. ret = -EFAULT;
  414. if (copy_to_user(useraddr, &info, info_size))
  415. goto err_out;
  416. if (rule_buf) {
  417. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  418. if (copy_to_user(useraddr, rule_buf,
  419. info.rule_cnt * sizeof(u32)))
  420. goto err_out;
  421. }
  422. ret = 0;
  423. err_out:
  424. kfree(rule_buf);
  425. return ret;
  426. }
  427. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  428. void __user *useraddr)
  429. {
  430. u32 user_size, dev_size;
  431. u32 *indir;
  432. int ret;
  433. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  434. !dev->ethtool_ops->get_rxfh_indir)
  435. return -EOPNOTSUPP;
  436. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  437. if (dev_size == 0)
  438. return -EOPNOTSUPP;
  439. if (copy_from_user(&user_size,
  440. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  441. sizeof(user_size)))
  442. return -EFAULT;
  443. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  444. &dev_size, sizeof(dev_size)))
  445. return -EFAULT;
  446. /* If the user buffer size is 0, this is just a query for the
  447. * device table size. Otherwise, if it's smaller than the
  448. * device table size it's an error.
  449. */
  450. if (user_size < dev_size)
  451. return user_size == 0 ? 0 : -EINVAL;
  452. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  453. if (!indir)
  454. return -ENOMEM;
  455. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  456. if (ret)
  457. goto out;
  458. if (copy_to_user(useraddr +
  459. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  460. indir, dev_size * sizeof(indir[0])))
  461. ret = -EFAULT;
  462. out:
  463. kfree(indir);
  464. return ret;
  465. }
  466. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  467. void __user *useraddr)
  468. {
  469. struct ethtool_rxnfc rx_rings;
  470. u32 user_size, dev_size, i;
  471. u32 *indir;
  472. int ret;
  473. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  474. !dev->ethtool_ops->set_rxfh_indir ||
  475. !dev->ethtool_ops->get_rxnfc)
  476. return -EOPNOTSUPP;
  477. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  478. if (dev_size == 0)
  479. return -EOPNOTSUPP;
  480. if (copy_from_user(&user_size,
  481. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  482. sizeof(user_size)))
  483. return -EFAULT;
  484. if (user_size != 0 && user_size != dev_size)
  485. return -EINVAL;
  486. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  487. if (!indir)
  488. return -ENOMEM;
  489. rx_rings.cmd = ETHTOOL_GRXRINGS;
  490. ret = dev->ethtool_ops->get_rxnfc(dev, &rx_rings, NULL);
  491. if (ret)
  492. goto out;
  493. if (user_size == 0) {
  494. for (i = 0; i < dev_size; i++)
  495. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  496. } else {
  497. if (copy_from_user(indir,
  498. useraddr +
  499. offsetof(struct ethtool_rxfh_indir,
  500. ring_index[0]),
  501. dev_size * sizeof(indir[0]))) {
  502. ret = -EFAULT;
  503. goto out;
  504. }
  505. /* Validate ring indices */
  506. for (i = 0; i < dev_size; i++) {
  507. if (indir[i] >= rx_rings.data) {
  508. ret = -EINVAL;
  509. goto out;
  510. }
  511. }
  512. }
  513. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  514. out:
  515. kfree(indir);
  516. return ret;
  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_GSSET_INFO:
  1046. case ETHTOOL_GSTRINGS:
  1047. case ETHTOOL_GTSO:
  1048. case ETHTOOL_GPERMADDR:
  1049. case ETHTOOL_GUFO:
  1050. case ETHTOOL_GGSO:
  1051. case ETHTOOL_GGRO:
  1052. case ETHTOOL_GFLAGS:
  1053. case ETHTOOL_GPFLAGS:
  1054. case ETHTOOL_GRXFH:
  1055. case ETHTOOL_GRXRINGS:
  1056. case ETHTOOL_GRXCLSRLCNT:
  1057. case ETHTOOL_GRXCLSRULE:
  1058. case ETHTOOL_GRXCLSRLALL:
  1059. case ETHTOOL_GFEATURES:
  1060. break;
  1061. default:
  1062. if (!capable(CAP_NET_ADMIN))
  1063. return -EPERM;
  1064. }
  1065. if (dev->ethtool_ops->begin) {
  1066. rc = dev->ethtool_ops->begin(dev);
  1067. if (rc < 0)
  1068. return rc;
  1069. }
  1070. old_features = dev->features;
  1071. switch (ethcmd) {
  1072. case ETHTOOL_GSET:
  1073. rc = ethtool_get_settings(dev, useraddr);
  1074. break;
  1075. case ETHTOOL_SSET:
  1076. rc = ethtool_set_settings(dev, useraddr);
  1077. break;
  1078. case ETHTOOL_GDRVINFO:
  1079. rc = ethtool_get_drvinfo(dev, useraddr);
  1080. break;
  1081. case ETHTOOL_GREGS:
  1082. rc = ethtool_get_regs(dev, useraddr);
  1083. break;
  1084. case ETHTOOL_GWOL:
  1085. rc = ethtool_get_wol(dev, useraddr);
  1086. break;
  1087. case ETHTOOL_SWOL:
  1088. rc = ethtool_set_wol(dev, useraddr);
  1089. break;
  1090. case ETHTOOL_GMSGLVL:
  1091. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1092. dev->ethtool_ops->get_msglevel);
  1093. break;
  1094. case ETHTOOL_SMSGLVL:
  1095. rc = ethtool_set_value_void(dev, useraddr,
  1096. dev->ethtool_ops->set_msglevel);
  1097. break;
  1098. case ETHTOOL_NWAY_RST:
  1099. rc = ethtool_nway_reset(dev);
  1100. break;
  1101. case ETHTOOL_GLINK:
  1102. rc = ethtool_get_link(dev, useraddr);
  1103. break;
  1104. case ETHTOOL_GEEPROM:
  1105. rc = ethtool_get_eeprom(dev, useraddr);
  1106. break;
  1107. case ETHTOOL_SEEPROM:
  1108. rc = ethtool_set_eeprom(dev, useraddr);
  1109. break;
  1110. case ETHTOOL_GCOALESCE:
  1111. rc = ethtool_get_coalesce(dev, useraddr);
  1112. break;
  1113. case ETHTOOL_SCOALESCE:
  1114. rc = ethtool_set_coalesce(dev, useraddr);
  1115. break;
  1116. case ETHTOOL_GRINGPARAM:
  1117. rc = ethtool_get_ringparam(dev, useraddr);
  1118. break;
  1119. case ETHTOOL_SRINGPARAM:
  1120. rc = ethtool_set_ringparam(dev, useraddr);
  1121. break;
  1122. case ETHTOOL_GPAUSEPARAM:
  1123. rc = ethtool_get_pauseparam(dev, useraddr);
  1124. break;
  1125. case ETHTOOL_SPAUSEPARAM:
  1126. rc = ethtool_set_pauseparam(dev, useraddr);
  1127. break;
  1128. case ETHTOOL_TEST:
  1129. rc = ethtool_self_test(dev, useraddr);
  1130. break;
  1131. case ETHTOOL_GSTRINGS:
  1132. rc = ethtool_get_strings(dev, useraddr);
  1133. break;
  1134. case ETHTOOL_PHYS_ID:
  1135. rc = ethtool_phys_id(dev, useraddr);
  1136. break;
  1137. case ETHTOOL_GSTATS:
  1138. rc = ethtool_get_stats(dev, useraddr);
  1139. break;
  1140. case ETHTOOL_GPERMADDR:
  1141. rc = ethtool_get_perm_addr(dev, useraddr);
  1142. break;
  1143. case ETHTOOL_GFLAGS:
  1144. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1145. __ethtool_get_flags);
  1146. break;
  1147. case ETHTOOL_SFLAGS:
  1148. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1149. break;
  1150. case ETHTOOL_GPFLAGS:
  1151. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1152. dev->ethtool_ops->get_priv_flags);
  1153. break;
  1154. case ETHTOOL_SPFLAGS:
  1155. rc = ethtool_set_value(dev, useraddr,
  1156. dev->ethtool_ops->set_priv_flags);
  1157. break;
  1158. case ETHTOOL_GRXFH:
  1159. case ETHTOOL_GRXRINGS:
  1160. case ETHTOOL_GRXCLSRLCNT:
  1161. case ETHTOOL_GRXCLSRULE:
  1162. case ETHTOOL_GRXCLSRLALL:
  1163. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1164. break;
  1165. case ETHTOOL_SRXFH:
  1166. case ETHTOOL_SRXCLSRLDEL:
  1167. case ETHTOOL_SRXCLSRLINS:
  1168. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1169. break;
  1170. case ETHTOOL_FLASHDEV:
  1171. rc = ethtool_flash_device(dev, useraddr);
  1172. break;
  1173. case ETHTOOL_RESET:
  1174. rc = ethtool_reset(dev, useraddr);
  1175. break;
  1176. case ETHTOOL_GSSET_INFO:
  1177. rc = ethtool_get_sset_info(dev, useraddr);
  1178. break;
  1179. case ETHTOOL_GRXFHINDIR:
  1180. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1181. break;
  1182. case ETHTOOL_SRXFHINDIR:
  1183. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1184. break;
  1185. case ETHTOOL_GFEATURES:
  1186. rc = ethtool_get_features(dev, useraddr);
  1187. break;
  1188. case ETHTOOL_SFEATURES:
  1189. rc = ethtool_set_features(dev, useraddr);
  1190. break;
  1191. case ETHTOOL_GTXCSUM:
  1192. case ETHTOOL_GRXCSUM:
  1193. case ETHTOOL_GSG:
  1194. case ETHTOOL_GTSO:
  1195. case ETHTOOL_GUFO:
  1196. case ETHTOOL_GGSO:
  1197. case ETHTOOL_GGRO:
  1198. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1199. break;
  1200. case ETHTOOL_STXCSUM:
  1201. case ETHTOOL_SRXCSUM:
  1202. case ETHTOOL_SSG:
  1203. case ETHTOOL_STSO:
  1204. case ETHTOOL_SUFO:
  1205. case ETHTOOL_SGSO:
  1206. case ETHTOOL_SGRO:
  1207. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1208. break;
  1209. case ETHTOOL_GCHANNELS:
  1210. rc = ethtool_get_channels(dev, useraddr);
  1211. break;
  1212. case ETHTOOL_SCHANNELS:
  1213. rc = ethtool_set_channels(dev, useraddr);
  1214. break;
  1215. case ETHTOOL_SET_DUMP:
  1216. rc = ethtool_set_dump(dev, useraddr);
  1217. break;
  1218. case ETHTOOL_GET_DUMP_FLAG:
  1219. rc = ethtool_get_dump_flag(dev, useraddr);
  1220. break;
  1221. case ETHTOOL_GET_DUMP_DATA:
  1222. rc = ethtool_get_dump_data(dev, useraddr);
  1223. break;
  1224. default:
  1225. rc = -EOPNOTSUPP;
  1226. }
  1227. if (dev->ethtool_ops->complete)
  1228. dev->ethtool_ops->complete(dev);
  1229. if (old_features != dev->features)
  1230. netdev_features_change(dev);
  1231. return rc;
  1232. }