ethtool.c 40 KB

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