ethtool.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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. if (!dev->ethtool_ops->set_rxnfc)
  363. return -EOPNOTSUPP;
  364. /* struct ethtool_rxnfc was originally defined for
  365. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  366. * members. User-space might still be using that
  367. * definition. */
  368. if (cmd == ETHTOOL_SRXFH)
  369. info_size = (offsetof(struct ethtool_rxnfc, data) +
  370. sizeof(info.data));
  371. if (copy_from_user(&info, useraddr, info_size))
  372. return -EFAULT;
  373. return dev->ethtool_ops->set_rxnfc(dev, &info);
  374. }
  375. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  376. u32 cmd, void __user *useraddr)
  377. {
  378. struct ethtool_rxnfc info;
  379. size_t info_size = sizeof(info);
  380. const struct ethtool_ops *ops = dev->ethtool_ops;
  381. int ret;
  382. void *rule_buf = NULL;
  383. if (!ops->get_rxnfc)
  384. return -EOPNOTSUPP;
  385. /* struct ethtool_rxnfc was originally defined for
  386. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  387. * members. User-space might still be using that
  388. * definition. */
  389. if (cmd == ETHTOOL_GRXFH)
  390. info_size = (offsetof(struct ethtool_rxnfc, data) +
  391. sizeof(info.data));
  392. if (copy_from_user(&info, useraddr, info_size))
  393. return -EFAULT;
  394. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  395. if (info.rule_cnt > 0) {
  396. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  397. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  398. GFP_USER);
  399. if (!rule_buf)
  400. return -ENOMEM;
  401. }
  402. }
  403. ret = ops->get_rxnfc(dev, &info, rule_buf);
  404. if (ret < 0)
  405. goto err_out;
  406. ret = -EFAULT;
  407. if (copy_to_user(useraddr, &info, info_size))
  408. goto err_out;
  409. if (rule_buf) {
  410. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  411. if (copy_to_user(useraddr, rule_buf,
  412. info.rule_cnt * sizeof(u32)))
  413. goto err_out;
  414. }
  415. ret = 0;
  416. err_out:
  417. kfree(rule_buf);
  418. return ret;
  419. }
  420. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  421. void __user *useraddr)
  422. {
  423. u32 user_size, dev_size;
  424. u32 *indir;
  425. int ret;
  426. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  427. !dev->ethtool_ops->get_rxfh_indir)
  428. return -EOPNOTSUPP;
  429. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  430. if (dev_size == 0)
  431. return -EOPNOTSUPP;
  432. if (copy_from_user(&user_size,
  433. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  434. sizeof(user_size)))
  435. return -EFAULT;
  436. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  437. &dev_size, sizeof(dev_size)))
  438. return -EFAULT;
  439. /* If the user buffer size is 0, this is just a query for the
  440. * device table size. Otherwise, if it's smaller than the
  441. * device table size it's an error.
  442. */
  443. if (user_size < dev_size)
  444. return user_size == 0 ? 0 : -EINVAL;
  445. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  446. if (!indir)
  447. return -ENOMEM;
  448. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  449. if (ret)
  450. goto out;
  451. if (copy_to_user(useraddr +
  452. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  453. indir, dev_size * sizeof(indir[0])))
  454. ret = -EFAULT;
  455. out:
  456. kfree(indir);
  457. return ret;
  458. }
  459. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  460. void __user *useraddr)
  461. {
  462. struct ethtool_rxnfc rx_rings;
  463. u32 user_size, dev_size, i;
  464. u32 *indir;
  465. int ret;
  466. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  467. !dev->ethtool_ops->set_rxfh_indir ||
  468. !dev->ethtool_ops->get_rxnfc)
  469. return -EOPNOTSUPP;
  470. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  471. if (dev_size == 0)
  472. return -EOPNOTSUPP;
  473. if (copy_from_user(&user_size,
  474. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  475. sizeof(user_size)))
  476. return -EFAULT;
  477. if (user_size != 0 && user_size != dev_size)
  478. return -EINVAL;
  479. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  480. if (!indir)
  481. return -ENOMEM;
  482. rx_rings.cmd = ETHTOOL_GRXRINGS;
  483. ret = dev->ethtool_ops->get_rxnfc(dev, &rx_rings, NULL);
  484. if (ret)
  485. goto out;
  486. if (user_size == 0) {
  487. for (i = 0; i < dev_size; i++)
  488. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  489. } else {
  490. if (copy_from_user(indir,
  491. useraddr +
  492. offsetof(struct ethtool_rxfh_indir,
  493. ring_index[0]),
  494. dev_size * sizeof(indir[0]))) {
  495. ret = -EFAULT;
  496. goto out;
  497. }
  498. /* Validate ring indices */
  499. for (i = 0; i < dev_size; i++) {
  500. if (indir[i] >= rx_rings.data) {
  501. ret = -EINVAL;
  502. goto out;
  503. }
  504. }
  505. }
  506. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  507. out:
  508. kfree(indir);
  509. return ret;
  510. }
  511. /*
  512. * ethtool does not (or did not) set masks for flow parameters that are
  513. * not specified, so if both value and mask are 0 then this must be
  514. * treated as equivalent to a mask with all bits set. Implement that
  515. * here rather than in drivers.
  516. */
  517. static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
  518. {
  519. struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
  520. struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
  521. if (fs->flow_type != TCP_V4_FLOW &&
  522. fs->flow_type != UDP_V4_FLOW &&
  523. fs->flow_type != SCTP_V4_FLOW)
  524. return;
  525. if (!(entry->ip4src | mask->ip4src))
  526. mask->ip4src = htonl(0xffffffff);
  527. if (!(entry->ip4dst | mask->ip4dst))
  528. mask->ip4dst = htonl(0xffffffff);
  529. if (!(entry->psrc | mask->psrc))
  530. mask->psrc = htons(0xffff);
  531. if (!(entry->pdst | mask->pdst))
  532. mask->pdst = htons(0xffff);
  533. if (!(entry->tos | mask->tos))
  534. mask->tos = 0xff;
  535. if (!(fs->vlan_tag | fs->vlan_tag_mask))
  536. fs->vlan_tag_mask = 0xffff;
  537. if (!(fs->data | fs->data_mask))
  538. fs->data_mask = 0xffffffffffffffffULL;
  539. }
  540. static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
  541. void __user *useraddr)
  542. {
  543. struct ethtool_rx_ntuple cmd;
  544. const struct ethtool_ops *ops = dev->ethtool_ops;
  545. if (!ops->set_rx_ntuple)
  546. return -EOPNOTSUPP;
  547. if (!(dev->features & NETIF_F_NTUPLE))
  548. return -EINVAL;
  549. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  550. return -EFAULT;
  551. rx_ntuple_fix_masks(&cmd.fs);
  552. return ops->set_rx_ntuple(dev, &cmd);
  553. }
  554. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  555. {
  556. struct ethtool_regs regs;
  557. const struct ethtool_ops *ops = dev->ethtool_ops;
  558. void *regbuf;
  559. int reglen, ret;
  560. if (!ops->get_regs || !ops->get_regs_len)
  561. return -EOPNOTSUPP;
  562. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  563. return -EFAULT;
  564. reglen = ops->get_regs_len(dev);
  565. if (regs.len > reglen)
  566. regs.len = reglen;
  567. regbuf = vzalloc(reglen);
  568. if (reglen && !regbuf)
  569. return -ENOMEM;
  570. ops->get_regs(dev, &regs, regbuf);
  571. ret = -EFAULT;
  572. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  573. goto out;
  574. useraddr += offsetof(struct ethtool_regs, data);
  575. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  576. goto out;
  577. ret = 0;
  578. out:
  579. vfree(regbuf);
  580. return ret;
  581. }
  582. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  583. {
  584. struct ethtool_value reset;
  585. int ret;
  586. if (!dev->ethtool_ops->reset)
  587. return -EOPNOTSUPP;
  588. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  589. return -EFAULT;
  590. ret = dev->ethtool_ops->reset(dev, &reset.data);
  591. if (ret)
  592. return ret;
  593. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  594. return -EFAULT;
  595. return 0;
  596. }
  597. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  598. {
  599. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  600. if (!dev->ethtool_ops->get_wol)
  601. return -EOPNOTSUPP;
  602. dev->ethtool_ops->get_wol(dev, &wol);
  603. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  604. return -EFAULT;
  605. return 0;
  606. }
  607. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  608. {
  609. struct ethtool_wolinfo wol;
  610. if (!dev->ethtool_ops->set_wol)
  611. return -EOPNOTSUPP;
  612. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  613. return -EFAULT;
  614. return dev->ethtool_ops->set_wol(dev, &wol);
  615. }
  616. static int ethtool_nway_reset(struct net_device *dev)
  617. {
  618. if (!dev->ethtool_ops->nway_reset)
  619. return -EOPNOTSUPP;
  620. return dev->ethtool_ops->nway_reset(dev);
  621. }
  622. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  623. {
  624. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  625. if (!dev->ethtool_ops->get_link)
  626. return -EOPNOTSUPP;
  627. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  628. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  629. return -EFAULT;
  630. return 0;
  631. }
  632. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  633. {
  634. struct ethtool_eeprom eeprom;
  635. const struct ethtool_ops *ops = dev->ethtool_ops;
  636. void __user *userbuf = useraddr + sizeof(eeprom);
  637. u32 bytes_remaining;
  638. u8 *data;
  639. int ret = 0;
  640. if (!ops->get_eeprom || !ops->get_eeprom_len)
  641. return -EOPNOTSUPP;
  642. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  643. return -EFAULT;
  644. /* Check for wrap and zero */
  645. if (eeprom.offset + eeprom.len <= eeprom.offset)
  646. return -EINVAL;
  647. /* Check for exceeding total eeprom len */
  648. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  649. return -EINVAL;
  650. data = kmalloc(PAGE_SIZE, GFP_USER);
  651. if (!data)
  652. return -ENOMEM;
  653. bytes_remaining = eeprom.len;
  654. while (bytes_remaining > 0) {
  655. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  656. ret = ops->get_eeprom(dev, &eeprom, data);
  657. if (ret)
  658. break;
  659. if (copy_to_user(userbuf, data, eeprom.len)) {
  660. ret = -EFAULT;
  661. break;
  662. }
  663. userbuf += eeprom.len;
  664. eeprom.offset += eeprom.len;
  665. bytes_remaining -= eeprom.len;
  666. }
  667. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  668. eeprom.offset -= eeprom.len;
  669. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  670. ret = -EFAULT;
  671. kfree(data);
  672. return ret;
  673. }
  674. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  675. {
  676. struct ethtool_eeprom eeprom;
  677. const struct ethtool_ops *ops = dev->ethtool_ops;
  678. void __user *userbuf = useraddr + sizeof(eeprom);
  679. u32 bytes_remaining;
  680. u8 *data;
  681. int ret = 0;
  682. if (!ops->set_eeprom || !ops->get_eeprom_len)
  683. return -EOPNOTSUPP;
  684. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  685. return -EFAULT;
  686. /* Check for wrap and zero */
  687. if (eeprom.offset + eeprom.len <= eeprom.offset)
  688. return -EINVAL;
  689. /* Check for exceeding total eeprom len */
  690. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  691. return -EINVAL;
  692. data = kmalloc(PAGE_SIZE, GFP_USER);
  693. if (!data)
  694. return -ENOMEM;
  695. bytes_remaining = eeprom.len;
  696. while (bytes_remaining > 0) {
  697. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  698. if (copy_from_user(data, userbuf, eeprom.len)) {
  699. ret = -EFAULT;
  700. break;
  701. }
  702. ret = ops->set_eeprom(dev, &eeprom, data);
  703. if (ret)
  704. break;
  705. userbuf += eeprom.len;
  706. eeprom.offset += eeprom.len;
  707. bytes_remaining -= eeprom.len;
  708. }
  709. kfree(data);
  710. return ret;
  711. }
  712. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  713. void __user *useraddr)
  714. {
  715. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  716. if (!dev->ethtool_ops->get_coalesce)
  717. return -EOPNOTSUPP;
  718. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  719. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  720. return -EFAULT;
  721. return 0;
  722. }
  723. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  724. void __user *useraddr)
  725. {
  726. struct ethtool_coalesce coalesce;
  727. if (!dev->ethtool_ops->set_coalesce)
  728. return -EOPNOTSUPP;
  729. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  730. return -EFAULT;
  731. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  732. }
  733. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  734. {
  735. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  736. if (!dev->ethtool_ops->get_ringparam)
  737. return -EOPNOTSUPP;
  738. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  739. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  740. return -EFAULT;
  741. return 0;
  742. }
  743. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  744. {
  745. struct ethtool_ringparam ringparam;
  746. if (!dev->ethtool_ops->set_ringparam)
  747. return -EOPNOTSUPP;
  748. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  749. return -EFAULT;
  750. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  751. }
  752. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  753. void __user *useraddr)
  754. {
  755. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  756. if (!dev->ethtool_ops->get_channels)
  757. return -EOPNOTSUPP;
  758. dev->ethtool_ops->get_channels(dev, &channels);
  759. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  760. return -EFAULT;
  761. return 0;
  762. }
  763. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  764. void __user *useraddr)
  765. {
  766. struct ethtool_channels channels;
  767. if (!dev->ethtool_ops->set_channels)
  768. return -EOPNOTSUPP;
  769. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  770. return -EFAULT;
  771. return dev->ethtool_ops->set_channels(dev, &channels);
  772. }
  773. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  774. {
  775. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  776. if (!dev->ethtool_ops->get_pauseparam)
  777. return -EOPNOTSUPP;
  778. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  779. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  780. return -EFAULT;
  781. return 0;
  782. }
  783. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  784. {
  785. struct ethtool_pauseparam pauseparam;
  786. if (!dev->ethtool_ops->set_pauseparam)
  787. return -EOPNOTSUPP;
  788. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  789. return -EFAULT;
  790. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  791. }
  792. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  793. {
  794. struct ethtool_test test;
  795. const struct ethtool_ops *ops = dev->ethtool_ops;
  796. u64 *data;
  797. int ret, test_len;
  798. if (!ops->self_test || !ops->get_sset_count)
  799. return -EOPNOTSUPP;
  800. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  801. if (test_len < 0)
  802. return test_len;
  803. WARN_ON(test_len == 0);
  804. if (copy_from_user(&test, useraddr, sizeof(test)))
  805. return -EFAULT;
  806. test.len = test_len;
  807. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  808. if (!data)
  809. return -ENOMEM;
  810. ops->self_test(dev, &test, data);
  811. ret = -EFAULT;
  812. if (copy_to_user(useraddr, &test, sizeof(test)))
  813. goto out;
  814. useraddr += sizeof(test);
  815. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  816. goto out;
  817. ret = 0;
  818. out:
  819. kfree(data);
  820. return ret;
  821. }
  822. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  823. {
  824. struct ethtool_gstrings gstrings;
  825. u8 *data;
  826. int ret;
  827. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  828. return -EFAULT;
  829. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  830. if (ret < 0)
  831. return ret;
  832. gstrings.len = ret;
  833. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  834. if (!data)
  835. return -ENOMEM;
  836. __ethtool_get_strings(dev, gstrings.string_set, data);
  837. ret = -EFAULT;
  838. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  839. goto out;
  840. useraddr += sizeof(gstrings);
  841. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  842. goto out;
  843. ret = 0;
  844. out:
  845. kfree(data);
  846. return ret;
  847. }
  848. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  849. {
  850. struct ethtool_value id;
  851. static bool busy;
  852. int rc;
  853. if (!dev->ethtool_ops->set_phys_id)
  854. return -EOPNOTSUPP;
  855. if (busy)
  856. return -EBUSY;
  857. if (copy_from_user(&id, useraddr, sizeof(id)))
  858. return -EFAULT;
  859. rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  860. if (rc < 0)
  861. return rc;
  862. /* Drop the RTNL lock while waiting, but prevent reentry or
  863. * removal of the device.
  864. */
  865. busy = true;
  866. dev_hold(dev);
  867. rtnl_unlock();
  868. if (rc == 0) {
  869. /* Driver will handle this itself */
  870. schedule_timeout_interruptible(
  871. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  872. } else {
  873. /* Driver expects to be called at twice the frequency in rc */
  874. int n = rc * 2, i, interval = HZ / n;
  875. /* Count down seconds */
  876. do {
  877. /* Count down iterations per second */
  878. i = n;
  879. do {
  880. rtnl_lock();
  881. rc = dev->ethtool_ops->set_phys_id(dev,
  882. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  883. rtnl_unlock();
  884. if (rc)
  885. break;
  886. schedule_timeout_interruptible(interval);
  887. } while (!signal_pending(current) && --i != 0);
  888. } while (!signal_pending(current) &&
  889. (id.data == 0 || --id.data != 0));
  890. }
  891. rtnl_lock();
  892. dev_put(dev);
  893. busy = false;
  894. (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  895. return rc;
  896. }
  897. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  898. {
  899. struct ethtool_stats stats;
  900. const struct ethtool_ops *ops = dev->ethtool_ops;
  901. u64 *data;
  902. int ret, n_stats;
  903. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  904. return -EOPNOTSUPP;
  905. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  906. if (n_stats < 0)
  907. return n_stats;
  908. WARN_ON(n_stats == 0);
  909. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  910. return -EFAULT;
  911. stats.n_stats = n_stats;
  912. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  913. if (!data)
  914. return -ENOMEM;
  915. ops->get_ethtool_stats(dev, &stats, data);
  916. ret = -EFAULT;
  917. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  918. goto out;
  919. useraddr += sizeof(stats);
  920. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  921. goto out;
  922. ret = 0;
  923. out:
  924. kfree(data);
  925. return ret;
  926. }
  927. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  928. {
  929. struct ethtool_perm_addr epaddr;
  930. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  931. return -EFAULT;
  932. if (epaddr.size < dev->addr_len)
  933. return -ETOOSMALL;
  934. epaddr.size = dev->addr_len;
  935. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  936. return -EFAULT;
  937. useraddr += sizeof(epaddr);
  938. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  939. return -EFAULT;
  940. return 0;
  941. }
  942. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  943. u32 cmd, u32 (*actor)(struct net_device *))
  944. {
  945. struct ethtool_value edata = { .cmd = cmd };
  946. if (!actor)
  947. return -EOPNOTSUPP;
  948. edata.data = actor(dev);
  949. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  950. return -EFAULT;
  951. return 0;
  952. }
  953. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  954. void (*actor)(struct net_device *, u32))
  955. {
  956. struct ethtool_value edata;
  957. if (!actor)
  958. return -EOPNOTSUPP;
  959. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  960. return -EFAULT;
  961. actor(dev, edata.data);
  962. return 0;
  963. }
  964. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  965. int (*actor)(struct net_device *, u32))
  966. {
  967. struct ethtool_value edata;
  968. if (!actor)
  969. return -EOPNOTSUPP;
  970. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  971. return -EFAULT;
  972. return actor(dev, edata.data);
  973. }
  974. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  975. char __user *useraddr)
  976. {
  977. struct ethtool_flash efl;
  978. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  979. return -EFAULT;
  980. if (!dev->ethtool_ops->flash_device)
  981. return -EOPNOTSUPP;
  982. return dev->ethtool_ops->flash_device(dev, &efl);
  983. }
  984. static int ethtool_set_dump(struct net_device *dev,
  985. void __user *useraddr)
  986. {
  987. struct ethtool_dump dump;
  988. if (!dev->ethtool_ops->set_dump)
  989. return -EOPNOTSUPP;
  990. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  991. return -EFAULT;
  992. return dev->ethtool_ops->set_dump(dev, &dump);
  993. }
  994. static int ethtool_get_dump_flag(struct net_device *dev,
  995. void __user *useraddr)
  996. {
  997. int ret;
  998. struct ethtool_dump dump;
  999. const struct ethtool_ops *ops = dev->ethtool_ops;
  1000. if (!dev->ethtool_ops->get_dump_flag)
  1001. return -EOPNOTSUPP;
  1002. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1003. return -EFAULT;
  1004. ret = ops->get_dump_flag(dev, &dump);
  1005. if (ret)
  1006. return ret;
  1007. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1008. return -EFAULT;
  1009. return 0;
  1010. }
  1011. static int ethtool_get_dump_data(struct net_device *dev,
  1012. void __user *useraddr)
  1013. {
  1014. int ret;
  1015. __u32 len;
  1016. struct ethtool_dump dump, tmp;
  1017. const struct ethtool_ops *ops = dev->ethtool_ops;
  1018. void *data = NULL;
  1019. if (!dev->ethtool_ops->get_dump_data ||
  1020. !dev->ethtool_ops->get_dump_flag)
  1021. return -EOPNOTSUPP;
  1022. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1023. return -EFAULT;
  1024. memset(&tmp, 0, sizeof(tmp));
  1025. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1026. ret = ops->get_dump_flag(dev, &tmp);
  1027. if (ret)
  1028. return ret;
  1029. len = (tmp.len > dump.len) ? dump.len : tmp.len;
  1030. if (!len)
  1031. return -EFAULT;
  1032. data = vzalloc(tmp.len);
  1033. if (!data)
  1034. return -ENOMEM;
  1035. ret = ops->get_dump_data(dev, &dump, data);
  1036. if (ret)
  1037. goto out;
  1038. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1039. ret = -EFAULT;
  1040. goto out;
  1041. }
  1042. useraddr += offsetof(struct ethtool_dump, data);
  1043. if (copy_to_user(useraddr, data, len))
  1044. ret = -EFAULT;
  1045. out:
  1046. vfree(data);
  1047. return ret;
  1048. }
  1049. /* The main entry point in this file. Called from net/core/dev.c */
  1050. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1051. {
  1052. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1053. void __user *useraddr = ifr->ifr_data;
  1054. u32 ethcmd;
  1055. int rc;
  1056. u32 old_features;
  1057. if (!dev || !netif_device_present(dev))
  1058. return -ENODEV;
  1059. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1060. return -EFAULT;
  1061. if (!dev->ethtool_ops) {
  1062. /* ETHTOOL_GDRVINFO does not require any driver support.
  1063. * It is also unprivileged and does not change anything,
  1064. * so we can take a shortcut to it. */
  1065. if (ethcmd == ETHTOOL_GDRVINFO)
  1066. return ethtool_get_drvinfo(dev, useraddr);
  1067. else
  1068. return -EOPNOTSUPP;
  1069. }
  1070. /* Allow some commands to be done by anyone */
  1071. switch (ethcmd) {
  1072. case ETHTOOL_GSET:
  1073. case ETHTOOL_GDRVINFO:
  1074. case ETHTOOL_GMSGLVL:
  1075. case ETHTOOL_GCOALESCE:
  1076. case ETHTOOL_GRINGPARAM:
  1077. case ETHTOOL_GPAUSEPARAM:
  1078. case ETHTOOL_GRXCSUM:
  1079. case ETHTOOL_GTXCSUM:
  1080. case ETHTOOL_GSG:
  1081. case ETHTOOL_GSTRINGS:
  1082. case ETHTOOL_GTSO:
  1083. case ETHTOOL_GPERMADDR:
  1084. case ETHTOOL_GUFO:
  1085. case ETHTOOL_GGSO:
  1086. case ETHTOOL_GGRO:
  1087. case ETHTOOL_GFLAGS:
  1088. case ETHTOOL_GPFLAGS:
  1089. case ETHTOOL_GRXFH:
  1090. case ETHTOOL_GRXRINGS:
  1091. case ETHTOOL_GRXCLSRLCNT:
  1092. case ETHTOOL_GRXCLSRULE:
  1093. case ETHTOOL_GRXCLSRLALL:
  1094. case ETHTOOL_GFEATURES:
  1095. break;
  1096. default:
  1097. if (!capable(CAP_NET_ADMIN))
  1098. return -EPERM;
  1099. }
  1100. if (dev->ethtool_ops->begin) {
  1101. rc = dev->ethtool_ops->begin(dev);
  1102. if (rc < 0)
  1103. return rc;
  1104. }
  1105. old_features = dev->features;
  1106. switch (ethcmd) {
  1107. case ETHTOOL_GSET:
  1108. rc = ethtool_get_settings(dev, useraddr);
  1109. break;
  1110. case ETHTOOL_SSET:
  1111. rc = ethtool_set_settings(dev, useraddr);
  1112. break;
  1113. case ETHTOOL_GDRVINFO:
  1114. rc = ethtool_get_drvinfo(dev, useraddr);
  1115. break;
  1116. case ETHTOOL_GREGS:
  1117. rc = ethtool_get_regs(dev, useraddr);
  1118. break;
  1119. case ETHTOOL_GWOL:
  1120. rc = ethtool_get_wol(dev, useraddr);
  1121. break;
  1122. case ETHTOOL_SWOL:
  1123. rc = ethtool_set_wol(dev, useraddr);
  1124. break;
  1125. case ETHTOOL_GMSGLVL:
  1126. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1127. dev->ethtool_ops->get_msglevel);
  1128. break;
  1129. case ETHTOOL_SMSGLVL:
  1130. rc = ethtool_set_value_void(dev, useraddr,
  1131. dev->ethtool_ops->set_msglevel);
  1132. break;
  1133. case ETHTOOL_NWAY_RST:
  1134. rc = ethtool_nway_reset(dev);
  1135. break;
  1136. case ETHTOOL_GLINK:
  1137. rc = ethtool_get_link(dev, useraddr);
  1138. break;
  1139. case ETHTOOL_GEEPROM:
  1140. rc = ethtool_get_eeprom(dev, useraddr);
  1141. break;
  1142. case ETHTOOL_SEEPROM:
  1143. rc = ethtool_set_eeprom(dev, useraddr);
  1144. break;
  1145. case ETHTOOL_GCOALESCE:
  1146. rc = ethtool_get_coalesce(dev, useraddr);
  1147. break;
  1148. case ETHTOOL_SCOALESCE:
  1149. rc = ethtool_set_coalesce(dev, useraddr);
  1150. break;
  1151. case ETHTOOL_GRINGPARAM:
  1152. rc = ethtool_get_ringparam(dev, useraddr);
  1153. break;
  1154. case ETHTOOL_SRINGPARAM:
  1155. rc = ethtool_set_ringparam(dev, useraddr);
  1156. break;
  1157. case ETHTOOL_GPAUSEPARAM:
  1158. rc = ethtool_get_pauseparam(dev, useraddr);
  1159. break;
  1160. case ETHTOOL_SPAUSEPARAM:
  1161. rc = ethtool_set_pauseparam(dev, useraddr);
  1162. break;
  1163. case ETHTOOL_TEST:
  1164. rc = ethtool_self_test(dev, useraddr);
  1165. break;
  1166. case ETHTOOL_GSTRINGS:
  1167. rc = ethtool_get_strings(dev, useraddr);
  1168. break;
  1169. case ETHTOOL_PHYS_ID:
  1170. rc = ethtool_phys_id(dev, useraddr);
  1171. break;
  1172. case ETHTOOL_GSTATS:
  1173. rc = ethtool_get_stats(dev, useraddr);
  1174. break;
  1175. case ETHTOOL_GPERMADDR:
  1176. rc = ethtool_get_perm_addr(dev, useraddr);
  1177. break;
  1178. case ETHTOOL_GFLAGS:
  1179. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1180. __ethtool_get_flags);
  1181. break;
  1182. case ETHTOOL_SFLAGS:
  1183. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1184. break;
  1185. case ETHTOOL_GPFLAGS:
  1186. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1187. dev->ethtool_ops->get_priv_flags);
  1188. break;
  1189. case ETHTOOL_SPFLAGS:
  1190. rc = ethtool_set_value(dev, useraddr,
  1191. dev->ethtool_ops->set_priv_flags);
  1192. break;
  1193. case ETHTOOL_GRXFH:
  1194. case ETHTOOL_GRXRINGS:
  1195. case ETHTOOL_GRXCLSRLCNT:
  1196. case ETHTOOL_GRXCLSRULE:
  1197. case ETHTOOL_GRXCLSRLALL:
  1198. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1199. break;
  1200. case ETHTOOL_SRXFH:
  1201. case ETHTOOL_SRXCLSRLDEL:
  1202. case ETHTOOL_SRXCLSRLINS:
  1203. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1204. break;
  1205. case ETHTOOL_FLASHDEV:
  1206. rc = ethtool_flash_device(dev, useraddr);
  1207. break;
  1208. case ETHTOOL_RESET:
  1209. rc = ethtool_reset(dev, useraddr);
  1210. break;
  1211. case ETHTOOL_SRXNTUPLE:
  1212. rc = ethtool_set_rx_ntuple(dev, useraddr);
  1213. break;
  1214. case ETHTOOL_GSSET_INFO:
  1215. rc = ethtool_get_sset_info(dev, useraddr);
  1216. break;
  1217. case ETHTOOL_GRXFHINDIR:
  1218. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1219. break;
  1220. case ETHTOOL_SRXFHINDIR:
  1221. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1222. break;
  1223. case ETHTOOL_GFEATURES:
  1224. rc = ethtool_get_features(dev, useraddr);
  1225. break;
  1226. case ETHTOOL_SFEATURES:
  1227. rc = ethtool_set_features(dev, useraddr);
  1228. break;
  1229. case ETHTOOL_GTXCSUM:
  1230. case ETHTOOL_GRXCSUM:
  1231. case ETHTOOL_GSG:
  1232. case ETHTOOL_GTSO:
  1233. case ETHTOOL_GUFO:
  1234. case ETHTOOL_GGSO:
  1235. case ETHTOOL_GGRO:
  1236. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1237. break;
  1238. case ETHTOOL_STXCSUM:
  1239. case ETHTOOL_SRXCSUM:
  1240. case ETHTOOL_SSG:
  1241. case ETHTOOL_STSO:
  1242. case ETHTOOL_SUFO:
  1243. case ETHTOOL_SGSO:
  1244. case ETHTOOL_SGRO:
  1245. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1246. break;
  1247. case ETHTOOL_GCHANNELS:
  1248. rc = ethtool_get_channels(dev, useraddr);
  1249. break;
  1250. case ETHTOOL_SCHANNELS:
  1251. rc = ethtool_set_channels(dev, useraddr);
  1252. break;
  1253. case ETHTOOL_SET_DUMP:
  1254. rc = ethtool_set_dump(dev, useraddr);
  1255. break;
  1256. case ETHTOOL_GET_DUMP_FLAG:
  1257. rc = ethtool_get_dump_flag(dev, useraddr);
  1258. break;
  1259. case ETHTOOL_GET_DUMP_DATA:
  1260. rc = ethtool_get_dump_data(dev, useraddr);
  1261. break;
  1262. default:
  1263. rc = -EOPNOTSUPP;
  1264. }
  1265. if (dev->ethtool_ops->complete)
  1266. dev->ethtool_ops->complete(dev);
  1267. if (old_features != dev->features)
  1268. netdev_features_change(dev);
  1269. return rc;
  1270. }