ethtool.c 50 KB

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