ethtool.c 39 KB

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