ethtool.c 40 KB

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