ethtool.c 40 KB

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