ethtool.c 39 KB

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