ethtool.c 40 KB

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