ethtool.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  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. [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
  92. };
  93. static const char
  94. rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
  95. [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
  96. [ETH_RSS_HASH_XOR_BIT] = "xor",
  97. };
  98. static const char
  99. tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
  100. [ETHTOOL_ID_UNSPEC] = "Unspec",
  101. [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
  102. [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
  103. };
  104. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  105. {
  106. struct ethtool_gfeatures cmd = {
  107. .cmd = ETHTOOL_GFEATURES,
  108. .size = ETHTOOL_DEV_FEATURE_WORDS,
  109. };
  110. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  111. u32 __user *sizeaddr;
  112. u32 copy_size;
  113. int i;
  114. /* in case feature bits run out again */
  115. BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
  116. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  117. features[i].available = (u32)(dev->hw_features >> (32 * i));
  118. features[i].requested = (u32)(dev->wanted_features >> (32 * i));
  119. features[i].active = (u32)(dev->features >> (32 * i));
  120. features[i].never_changed =
  121. (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
  122. }
  123. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  124. if (get_user(copy_size, sizeaddr))
  125. return -EFAULT;
  126. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  127. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  128. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  129. return -EFAULT;
  130. useraddr += sizeof(cmd);
  131. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  132. return -EFAULT;
  133. return 0;
  134. }
  135. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  136. {
  137. struct ethtool_sfeatures cmd;
  138. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  139. netdev_features_t wanted = 0, valid = 0;
  140. int i, ret = 0;
  141. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  142. return -EFAULT;
  143. useraddr += sizeof(cmd);
  144. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  145. return -EINVAL;
  146. if (copy_from_user(features, useraddr, sizeof(features)))
  147. return -EFAULT;
  148. for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
  149. valid |= (netdev_features_t)features[i].valid << (32 * i);
  150. wanted |= (netdev_features_t)features[i].requested << (32 * i);
  151. }
  152. if (valid & ~NETIF_F_ETHTOOL_BITS)
  153. return -EINVAL;
  154. if (valid & ~dev->hw_features) {
  155. valid &= dev->hw_features;
  156. ret |= ETHTOOL_F_UNSUPPORTED;
  157. }
  158. dev->wanted_features &= ~valid;
  159. dev->wanted_features |= wanted & valid;
  160. __netdev_update_features(dev);
  161. if ((dev->wanted_features ^ dev->features) & valid)
  162. ret |= ETHTOOL_F_WISH;
  163. return ret;
  164. }
  165. static int phy_get_sset_count(struct phy_device *phydev)
  166. {
  167. int ret;
  168. if (phydev->drv->get_sset_count &&
  169. phydev->drv->get_strings &&
  170. phydev->drv->get_stats) {
  171. mutex_lock(&phydev->lock);
  172. ret = phydev->drv->get_sset_count(phydev);
  173. mutex_unlock(&phydev->lock);
  174. return ret;
  175. }
  176. return -EOPNOTSUPP;
  177. }
  178. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  179. {
  180. const struct ethtool_ops *ops = dev->ethtool_ops;
  181. if (sset == ETH_SS_FEATURES)
  182. return ARRAY_SIZE(netdev_features_strings);
  183. if (sset == ETH_SS_RSS_HASH_FUNCS)
  184. return ARRAY_SIZE(rss_hash_func_strings);
  185. if (sset == ETH_SS_TUNABLES)
  186. return ARRAY_SIZE(tunable_strings);
  187. if (sset == ETH_SS_PHY_STATS) {
  188. if (dev->phydev)
  189. return phy_get_sset_count(dev->phydev);
  190. else
  191. return -EOPNOTSUPP;
  192. }
  193. if (ops->get_sset_count && ops->get_strings)
  194. return ops->get_sset_count(dev, sset);
  195. else
  196. return -EOPNOTSUPP;
  197. }
  198. static void __ethtool_get_strings(struct net_device *dev,
  199. u32 stringset, u8 *data)
  200. {
  201. const struct ethtool_ops *ops = dev->ethtool_ops;
  202. if (stringset == ETH_SS_FEATURES)
  203. memcpy(data, netdev_features_strings,
  204. sizeof(netdev_features_strings));
  205. else if (stringset == ETH_SS_RSS_HASH_FUNCS)
  206. memcpy(data, rss_hash_func_strings,
  207. sizeof(rss_hash_func_strings));
  208. else if (stringset == ETH_SS_TUNABLES)
  209. memcpy(data, tunable_strings, sizeof(tunable_strings));
  210. else if (stringset == ETH_SS_PHY_STATS) {
  211. struct phy_device *phydev = dev->phydev;
  212. if (phydev) {
  213. mutex_lock(&phydev->lock);
  214. phydev->drv->get_strings(phydev, data);
  215. mutex_unlock(&phydev->lock);
  216. } else {
  217. return;
  218. }
  219. } else
  220. /* ops->get_strings is valid because checked earlier */
  221. ops->get_strings(dev, stringset, data);
  222. }
  223. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  224. {
  225. /* feature masks of legacy discrete ethtool ops */
  226. switch (eth_cmd) {
  227. case ETHTOOL_GTXCSUM:
  228. case ETHTOOL_STXCSUM:
  229. return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
  230. case ETHTOOL_GRXCSUM:
  231. case ETHTOOL_SRXCSUM:
  232. return NETIF_F_RXCSUM;
  233. case ETHTOOL_GSG:
  234. case ETHTOOL_SSG:
  235. return NETIF_F_SG;
  236. case ETHTOOL_GTSO:
  237. case ETHTOOL_STSO:
  238. return NETIF_F_ALL_TSO;
  239. case ETHTOOL_GUFO:
  240. case ETHTOOL_SUFO:
  241. return NETIF_F_UFO;
  242. case ETHTOOL_GGSO:
  243. case ETHTOOL_SGSO:
  244. return NETIF_F_GSO;
  245. case ETHTOOL_GGRO:
  246. case ETHTOOL_SGRO:
  247. return NETIF_F_GRO;
  248. default:
  249. BUG();
  250. }
  251. }
  252. static int ethtool_get_one_feature(struct net_device *dev,
  253. char __user *useraddr, u32 ethcmd)
  254. {
  255. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  256. struct ethtool_value edata = {
  257. .cmd = ethcmd,
  258. .data = !!(dev->features & mask),
  259. };
  260. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  261. return -EFAULT;
  262. return 0;
  263. }
  264. static int ethtool_set_one_feature(struct net_device *dev,
  265. void __user *useraddr, u32 ethcmd)
  266. {
  267. struct ethtool_value edata;
  268. netdev_features_t mask;
  269. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  270. return -EFAULT;
  271. mask = ethtool_get_feature_mask(ethcmd);
  272. mask &= dev->hw_features;
  273. if (!mask)
  274. return -EOPNOTSUPP;
  275. if (edata.data)
  276. dev->wanted_features |= mask;
  277. else
  278. dev->wanted_features &= ~mask;
  279. __netdev_update_features(dev);
  280. return 0;
  281. }
  282. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  283. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  284. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
  285. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
  286. NETIF_F_RXHASH)
  287. static u32 __ethtool_get_flags(struct net_device *dev)
  288. {
  289. u32 flags = 0;
  290. if (dev->features & NETIF_F_LRO)
  291. flags |= ETH_FLAG_LRO;
  292. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  293. flags |= ETH_FLAG_RXVLAN;
  294. if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
  295. flags |= ETH_FLAG_TXVLAN;
  296. if (dev->features & NETIF_F_NTUPLE)
  297. flags |= ETH_FLAG_NTUPLE;
  298. if (dev->features & NETIF_F_RXHASH)
  299. flags |= ETH_FLAG_RXHASH;
  300. return flags;
  301. }
  302. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  303. {
  304. netdev_features_t features = 0, changed;
  305. if (data & ~ETH_ALL_FLAGS)
  306. return -EINVAL;
  307. if (data & ETH_FLAG_LRO)
  308. features |= NETIF_F_LRO;
  309. if (data & ETH_FLAG_RXVLAN)
  310. features |= NETIF_F_HW_VLAN_CTAG_RX;
  311. if (data & ETH_FLAG_TXVLAN)
  312. features |= NETIF_F_HW_VLAN_CTAG_TX;
  313. if (data & ETH_FLAG_NTUPLE)
  314. features |= NETIF_F_NTUPLE;
  315. if (data & ETH_FLAG_RXHASH)
  316. features |= NETIF_F_RXHASH;
  317. /* allow changing only bits set in hw_features */
  318. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  319. if (changed & ~dev->hw_features)
  320. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  321. dev->wanted_features =
  322. (dev->wanted_features & ~changed) | (features & changed);
  323. __netdev_update_features(dev);
  324. return 0;
  325. }
  326. static void convert_legacy_u32_to_link_mode(unsigned long *dst, u32 legacy_u32)
  327. {
  328. bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
  329. dst[0] = legacy_u32;
  330. }
  331. /* return false if src had higher bits set. lower bits always updated. */
  332. static bool convert_link_mode_to_legacy_u32(u32 *legacy_u32,
  333. const unsigned long *src)
  334. {
  335. bool retval = true;
  336. /* TODO: following test will soon always be true */
  337. if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
  338. __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
  339. bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
  340. bitmap_fill(ext, 32);
  341. bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
  342. if (bitmap_intersects(ext, src,
  343. __ETHTOOL_LINK_MODE_MASK_NBITS)) {
  344. /* src mask goes beyond bit 31 */
  345. retval = false;
  346. }
  347. }
  348. *legacy_u32 = src[0];
  349. return retval;
  350. }
  351. /* return false if legacy contained non-0 deprecated fields
  352. * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated
  353. */
  354. static bool
  355. convert_legacy_settings_to_link_ksettings(
  356. struct ethtool_link_ksettings *link_ksettings,
  357. const struct ethtool_cmd *legacy_settings)
  358. {
  359. bool retval = true;
  360. memset(link_ksettings, 0, sizeof(*link_ksettings));
  361. /* This is used to tell users that driver is still using these
  362. * deprecated legacy fields, and they should not use
  363. * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
  364. */
  365. if (legacy_settings->transceiver ||
  366. legacy_settings->maxtxpkt ||
  367. legacy_settings->maxrxpkt)
  368. retval = false;
  369. convert_legacy_u32_to_link_mode(
  370. link_ksettings->link_modes.supported,
  371. legacy_settings->supported);
  372. convert_legacy_u32_to_link_mode(
  373. link_ksettings->link_modes.advertising,
  374. legacy_settings->advertising);
  375. convert_legacy_u32_to_link_mode(
  376. link_ksettings->link_modes.lp_advertising,
  377. legacy_settings->lp_advertising);
  378. link_ksettings->base.speed
  379. = ethtool_cmd_speed(legacy_settings);
  380. link_ksettings->base.duplex
  381. = legacy_settings->duplex;
  382. link_ksettings->base.port
  383. = legacy_settings->port;
  384. link_ksettings->base.phy_address
  385. = legacy_settings->phy_address;
  386. link_ksettings->base.autoneg
  387. = legacy_settings->autoneg;
  388. link_ksettings->base.mdio_support
  389. = legacy_settings->mdio_support;
  390. link_ksettings->base.eth_tp_mdix
  391. = legacy_settings->eth_tp_mdix;
  392. link_ksettings->base.eth_tp_mdix_ctrl
  393. = legacy_settings->eth_tp_mdix_ctrl;
  394. return retval;
  395. }
  396. /* return false if ksettings link modes had higher bits
  397. * set. legacy_settings always updated (best effort)
  398. */
  399. static bool
  400. convert_link_ksettings_to_legacy_settings(
  401. struct ethtool_cmd *legacy_settings,
  402. const struct ethtool_link_ksettings *link_ksettings)
  403. {
  404. bool retval = true;
  405. memset(legacy_settings, 0, sizeof(*legacy_settings));
  406. /* this also clears the deprecated fields in legacy structure:
  407. * __u8 transceiver;
  408. * __u32 maxtxpkt;
  409. * __u32 maxrxpkt;
  410. */
  411. retval &= convert_link_mode_to_legacy_u32(
  412. &legacy_settings->supported,
  413. link_ksettings->link_modes.supported);
  414. retval &= convert_link_mode_to_legacy_u32(
  415. &legacy_settings->advertising,
  416. link_ksettings->link_modes.advertising);
  417. retval &= convert_link_mode_to_legacy_u32(
  418. &legacy_settings->lp_advertising,
  419. link_ksettings->link_modes.lp_advertising);
  420. ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
  421. legacy_settings->duplex
  422. = link_ksettings->base.duplex;
  423. legacy_settings->port
  424. = link_ksettings->base.port;
  425. legacy_settings->phy_address
  426. = link_ksettings->base.phy_address;
  427. legacy_settings->autoneg
  428. = link_ksettings->base.autoneg;
  429. legacy_settings->mdio_support
  430. = link_ksettings->base.mdio_support;
  431. legacy_settings->eth_tp_mdix
  432. = link_ksettings->base.eth_tp_mdix;
  433. legacy_settings->eth_tp_mdix_ctrl
  434. = link_ksettings->base.eth_tp_mdix_ctrl;
  435. return retval;
  436. }
  437. /* number of 32-bit words to store the user's link mode bitmaps */
  438. #define __ETHTOOL_LINK_MODE_MASK_NU32 \
  439. DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
  440. /* layout of the struct passed from/to userland */
  441. struct ethtool_link_usettings {
  442. struct ethtool_link_settings base;
  443. struct {
  444. __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
  445. __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  446. __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
  447. } link_modes;
  448. };
  449. /* Internal kernel helper to query a device ethtool_link_settings.
  450. *
  451. * Backward compatibility note: for compatibility with legacy drivers
  452. * that implement only the ethtool_cmd API, this has to work with both
  453. * drivers implementing get_link_ksettings API and drivers
  454. * implementing get_settings API. When drivers implement get_settings
  455. * and report ethtool_cmd deprecated fields
  456. * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
  457. * because the resulting struct ethtool_link_settings does not report them.
  458. */
  459. int __ethtool_get_link_ksettings(struct net_device *dev,
  460. struct ethtool_link_ksettings *link_ksettings)
  461. {
  462. int err;
  463. struct ethtool_cmd cmd;
  464. ASSERT_RTNL();
  465. if (dev->ethtool_ops->get_link_ksettings) {
  466. memset(link_ksettings, 0, sizeof(*link_ksettings));
  467. return dev->ethtool_ops->get_link_ksettings(dev,
  468. link_ksettings);
  469. }
  470. /* driver doesn't support %ethtool_link_ksettings API. revert to
  471. * legacy %ethtool_cmd API, unless it's not supported either.
  472. * TODO: remove when ethtool_ops::get_settings disappears internally
  473. */
  474. if (!dev->ethtool_ops->get_settings)
  475. return -EOPNOTSUPP;
  476. memset(&cmd, 0, sizeof(cmd));
  477. cmd.cmd = ETHTOOL_GSET;
  478. err = dev->ethtool_ops->get_settings(dev, &cmd);
  479. if (err < 0)
  480. return err;
  481. /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
  482. */
  483. convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
  484. return err;
  485. }
  486. EXPORT_SYMBOL(__ethtool_get_link_ksettings);
  487. /* convert ethtool_link_usettings in user space to a kernel internal
  488. * ethtool_link_ksettings. return 0 on success, errno on error.
  489. */
  490. static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
  491. const void __user *from)
  492. {
  493. struct ethtool_link_usettings link_usettings;
  494. if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
  495. return -EFAULT;
  496. memcpy(&to->base, &link_usettings.base, sizeof(to->base));
  497. bitmap_from_u32array(to->link_modes.supported,
  498. __ETHTOOL_LINK_MODE_MASK_NBITS,
  499. link_usettings.link_modes.supported,
  500. __ETHTOOL_LINK_MODE_MASK_NU32);
  501. bitmap_from_u32array(to->link_modes.advertising,
  502. __ETHTOOL_LINK_MODE_MASK_NBITS,
  503. link_usettings.link_modes.advertising,
  504. __ETHTOOL_LINK_MODE_MASK_NU32);
  505. bitmap_from_u32array(to->link_modes.lp_advertising,
  506. __ETHTOOL_LINK_MODE_MASK_NBITS,
  507. link_usettings.link_modes.lp_advertising,
  508. __ETHTOOL_LINK_MODE_MASK_NU32);
  509. return 0;
  510. }
  511. /* convert a kernel internal ethtool_link_ksettings to
  512. * ethtool_link_usettings in user space. return 0 on success, errno on
  513. * error.
  514. */
  515. static int
  516. store_link_ksettings_for_user(void __user *to,
  517. const struct ethtool_link_ksettings *from)
  518. {
  519. struct ethtool_link_usettings link_usettings;
  520. memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
  521. bitmap_to_u32array(link_usettings.link_modes.supported,
  522. __ETHTOOL_LINK_MODE_MASK_NU32,
  523. from->link_modes.supported,
  524. __ETHTOOL_LINK_MODE_MASK_NBITS);
  525. bitmap_to_u32array(link_usettings.link_modes.advertising,
  526. __ETHTOOL_LINK_MODE_MASK_NU32,
  527. from->link_modes.advertising,
  528. __ETHTOOL_LINK_MODE_MASK_NBITS);
  529. bitmap_to_u32array(link_usettings.link_modes.lp_advertising,
  530. __ETHTOOL_LINK_MODE_MASK_NU32,
  531. from->link_modes.lp_advertising,
  532. __ETHTOOL_LINK_MODE_MASK_NBITS);
  533. if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
  534. return -EFAULT;
  535. return 0;
  536. }
  537. /* Query device for its ethtool_link_settings.
  538. *
  539. * Backward compatibility note: this function must fail when driver
  540. * does not implement ethtool::get_link_ksettings, even if legacy
  541. * ethtool_ops::get_settings is implemented. This tells new versions
  542. * of ethtool that they should use the legacy API %ETHTOOL_GSET for
  543. * this driver, so that they can correctly access the ethtool_cmd
  544. * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
  545. * implements ethtool_ops::get_settings anymore.
  546. */
  547. static int ethtool_get_link_ksettings(struct net_device *dev,
  548. void __user *useraddr)
  549. {
  550. int err = 0;
  551. struct ethtool_link_ksettings link_ksettings;
  552. ASSERT_RTNL();
  553. if (!dev->ethtool_ops->get_link_ksettings)
  554. return -EOPNOTSUPP;
  555. /* handle bitmap nbits handshake */
  556. if (copy_from_user(&link_ksettings.base, useraddr,
  557. sizeof(link_ksettings.base)))
  558. return -EFAULT;
  559. if (__ETHTOOL_LINK_MODE_MASK_NU32
  560. != link_ksettings.base.link_mode_masks_nwords) {
  561. /* wrong link mode nbits requested */
  562. memset(&link_ksettings, 0, sizeof(link_ksettings));
  563. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  564. /* send back number of words required as negative val */
  565. compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
  566. "need too many bits for link modes!");
  567. link_ksettings.base.link_mode_masks_nwords
  568. = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
  569. /* copy the base fields back to user, not the link
  570. * mode bitmaps
  571. */
  572. if (copy_to_user(useraddr, &link_ksettings.base,
  573. sizeof(link_ksettings.base)))
  574. return -EFAULT;
  575. return 0;
  576. }
  577. /* handshake successful: user/kernel agree on
  578. * link_mode_masks_nwords
  579. */
  580. memset(&link_ksettings, 0, sizeof(link_ksettings));
  581. err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
  582. if (err < 0)
  583. return err;
  584. /* make sure we tell the right values to user */
  585. link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
  586. link_ksettings.base.link_mode_masks_nwords
  587. = __ETHTOOL_LINK_MODE_MASK_NU32;
  588. return store_link_ksettings_for_user(useraddr, &link_ksettings);
  589. }
  590. /* Update device ethtool_link_settings.
  591. *
  592. * Backward compatibility note: this function must fail when driver
  593. * does not implement ethtool::set_link_ksettings, even if legacy
  594. * ethtool_ops::set_settings is implemented. This tells new versions
  595. * of ethtool that they should use the legacy API %ETHTOOL_SSET for
  596. * this driver, so that they can correctly update the ethtool_cmd
  597. * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
  598. * implements ethtool_ops::get_settings anymore.
  599. */
  600. static int ethtool_set_link_ksettings(struct net_device *dev,
  601. void __user *useraddr)
  602. {
  603. int err;
  604. struct ethtool_link_ksettings link_ksettings;
  605. ASSERT_RTNL();
  606. if (!dev->ethtool_ops->set_link_ksettings)
  607. return -EOPNOTSUPP;
  608. /* make sure nbits field has expected value */
  609. if (copy_from_user(&link_ksettings.base, useraddr,
  610. sizeof(link_ksettings.base)))
  611. return -EFAULT;
  612. if (__ETHTOOL_LINK_MODE_MASK_NU32
  613. != link_ksettings.base.link_mode_masks_nwords)
  614. return -EINVAL;
  615. /* copy the whole structure, now that we know it has expected
  616. * format
  617. */
  618. err = load_link_ksettings_from_user(&link_ksettings, useraddr);
  619. if (err)
  620. return err;
  621. /* re-check nwords field, just in case */
  622. if (__ETHTOOL_LINK_MODE_MASK_NU32
  623. != link_ksettings.base.link_mode_masks_nwords)
  624. return -EINVAL;
  625. return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
  626. }
  627. static void
  628. warn_incomplete_ethtool_legacy_settings_conversion(const char *details)
  629. {
  630. char name[sizeof(current->comm)];
  631. pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n",
  632. get_task_comm(name, current), details);
  633. }
  634. /* Query device for its ethtool_cmd settings.
  635. *
  636. * Backward compatibility note: for compatibility with legacy ethtool,
  637. * this has to work with both drivers implementing get_link_ksettings
  638. * API and drivers implementing get_settings API. When drivers
  639. * implement get_link_ksettings and report higher link mode bits, a
  640. * kernel warning is logged once (with name of 1st driver/device) to
  641. * recommend user to upgrade ethtool, but the command is successful
  642. * (only the lower link mode bits reported back to user).
  643. */
  644. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  645. {
  646. struct ethtool_cmd cmd;
  647. ASSERT_RTNL();
  648. if (dev->ethtool_ops->get_link_ksettings) {
  649. /* First, use link_ksettings API if it is supported */
  650. int err;
  651. struct ethtool_link_ksettings link_ksettings;
  652. memset(&link_ksettings, 0, sizeof(link_ksettings));
  653. err = dev->ethtool_ops->get_link_ksettings(dev,
  654. &link_ksettings);
  655. if (err < 0)
  656. return err;
  657. if (!convert_link_ksettings_to_legacy_settings(&cmd,
  658. &link_ksettings))
  659. warn_incomplete_ethtool_legacy_settings_conversion(
  660. "link modes are only partially reported");
  661. /* send a sensible cmd tag back to user */
  662. cmd.cmd = ETHTOOL_GSET;
  663. } else {
  664. /* driver doesn't support %ethtool_link_ksettings
  665. * API. revert to legacy %ethtool_cmd API, unless it's
  666. * not supported either.
  667. */
  668. int err;
  669. if (!dev->ethtool_ops->get_settings)
  670. return -EOPNOTSUPP;
  671. memset(&cmd, 0, sizeof(cmd));
  672. cmd.cmd = ETHTOOL_GSET;
  673. err = dev->ethtool_ops->get_settings(dev, &cmd);
  674. if (err < 0)
  675. return err;
  676. }
  677. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  678. return -EFAULT;
  679. return 0;
  680. }
  681. /* Update device link settings with given ethtool_cmd.
  682. *
  683. * Backward compatibility note: for compatibility with legacy ethtool,
  684. * this has to work with both drivers implementing set_link_ksettings
  685. * API and drivers implementing set_settings API. When drivers
  686. * implement set_link_ksettings and user's request updates deprecated
  687. * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
  688. * warning is logged once (with name of 1st driver/device) to
  689. * recommend user to upgrade ethtool, and the request is rejected.
  690. */
  691. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  692. {
  693. struct ethtool_cmd cmd;
  694. ASSERT_RTNL();
  695. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  696. return -EFAULT;
  697. /* first, try new %ethtool_link_ksettings API. */
  698. if (dev->ethtool_ops->set_link_ksettings) {
  699. struct ethtool_link_ksettings link_ksettings;
  700. if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
  701. &cmd))
  702. return -EINVAL;
  703. link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
  704. link_ksettings.base.link_mode_masks_nwords
  705. = __ETHTOOL_LINK_MODE_MASK_NU32;
  706. return dev->ethtool_ops->set_link_ksettings(dev,
  707. &link_ksettings);
  708. }
  709. /* legacy %ethtool_cmd API */
  710. /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
  711. * disappears internally
  712. */
  713. if (!dev->ethtool_ops->set_settings)
  714. return -EOPNOTSUPP;
  715. return dev->ethtool_ops->set_settings(dev, &cmd);
  716. }
  717. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  718. void __user *useraddr)
  719. {
  720. struct ethtool_drvinfo info;
  721. const struct ethtool_ops *ops = dev->ethtool_ops;
  722. memset(&info, 0, sizeof(info));
  723. info.cmd = ETHTOOL_GDRVINFO;
  724. if (ops->get_drvinfo) {
  725. ops->get_drvinfo(dev, &info);
  726. } else if (dev->dev.parent && dev->dev.parent->driver) {
  727. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  728. sizeof(info.bus_info));
  729. strlcpy(info.driver, dev->dev.parent->driver->name,
  730. sizeof(info.driver));
  731. } else {
  732. return -EOPNOTSUPP;
  733. }
  734. /*
  735. * this method of obtaining string set info is deprecated;
  736. * Use ETHTOOL_GSSET_INFO instead.
  737. */
  738. if (ops->get_sset_count) {
  739. int rc;
  740. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  741. if (rc >= 0)
  742. info.testinfo_len = rc;
  743. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  744. if (rc >= 0)
  745. info.n_stats = rc;
  746. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  747. if (rc >= 0)
  748. info.n_priv_flags = rc;
  749. }
  750. if (ops->get_regs_len)
  751. info.regdump_len = ops->get_regs_len(dev);
  752. if (ops->get_eeprom_len)
  753. info.eedump_len = ops->get_eeprom_len(dev);
  754. if (copy_to_user(useraddr, &info, sizeof(info)))
  755. return -EFAULT;
  756. return 0;
  757. }
  758. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  759. void __user *useraddr)
  760. {
  761. struct ethtool_sset_info info;
  762. u64 sset_mask;
  763. int i, idx = 0, n_bits = 0, ret, rc;
  764. u32 *info_buf = NULL;
  765. if (copy_from_user(&info, useraddr, sizeof(info)))
  766. return -EFAULT;
  767. /* store copy of mask, because we zero struct later on */
  768. sset_mask = info.sset_mask;
  769. if (!sset_mask)
  770. return 0;
  771. /* calculate size of return buffer */
  772. n_bits = hweight64(sset_mask);
  773. memset(&info, 0, sizeof(info));
  774. info.cmd = ETHTOOL_GSSET_INFO;
  775. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  776. if (!info_buf)
  777. return -ENOMEM;
  778. /*
  779. * fill return buffer based on input bitmask and successful
  780. * get_sset_count return
  781. */
  782. for (i = 0; i < 64; i++) {
  783. if (!(sset_mask & (1ULL << i)))
  784. continue;
  785. rc = __ethtool_get_sset_count(dev, i);
  786. if (rc >= 0) {
  787. info.sset_mask |= (1ULL << i);
  788. info_buf[idx++] = rc;
  789. }
  790. }
  791. ret = -EFAULT;
  792. if (copy_to_user(useraddr, &info, sizeof(info)))
  793. goto out;
  794. useraddr += offsetof(struct ethtool_sset_info, data);
  795. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  796. goto out;
  797. ret = 0;
  798. out:
  799. kfree(info_buf);
  800. return ret;
  801. }
  802. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  803. u32 cmd, void __user *useraddr)
  804. {
  805. struct ethtool_rxnfc info;
  806. size_t info_size = sizeof(info);
  807. int rc;
  808. if (!dev->ethtool_ops->set_rxnfc)
  809. return -EOPNOTSUPP;
  810. /* struct ethtool_rxnfc was originally defined for
  811. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  812. * members. User-space might still be using that
  813. * definition. */
  814. if (cmd == ETHTOOL_SRXFH)
  815. info_size = (offsetof(struct ethtool_rxnfc, data) +
  816. sizeof(info.data));
  817. if (copy_from_user(&info, useraddr, info_size))
  818. return -EFAULT;
  819. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  820. if (rc)
  821. return rc;
  822. if (cmd == ETHTOOL_SRXCLSRLINS &&
  823. copy_to_user(useraddr, &info, info_size))
  824. return -EFAULT;
  825. return 0;
  826. }
  827. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  828. u32 cmd, void __user *useraddr)
  829. {
  830. struct ethtool_rxnfc info;
  831. size_t info_size = sizeof(info);
  832. const struct ethtool_ops *ops = dev->ethtool_ops;
  833. int ret;
  834. void *rule_buf = NULL;
  835. if (!ops->get_rxnfc)
  836. return -EOPNOTSUPP;
  837. /* struct ethtool_rxnfc was originally defined for
  838. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  839. * members. User-space might still be using that
  840. * definition. */
  841. if (cmd == ETHTOOL_GRXFH)
  842. info_size = (offsetof(struct ethtool_rxnfc, data) +
  843. sizeof(info.data));
  844. if (copy_from_user(&info, useraddr, info_size))
  845. return -EFAULT;
  846. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  847. if (info.rule_cnt > 0) {
  848. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  849. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  850. GFP_USER);
  851. if (!rule_buf)
  852. return -ENOMEM;
  853. }
  854. }
  855. ret = ops->get_rxnfc(dev, &info, rule_buf);
  856. if (ret < 0)
  857. goto err_out;
  858. ret = -EFAULT;
  859. if (copy_to_user(useraddr, &info, info_size))
  860. goto err_out;
  861. if (rule_buf) {
  862. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  863. if (copy_to_user(useraddr, rule_buf,
  864. info.rule_cnt * sizeof(u32)))
  865. goto err_out;
  866. }
  867. ret = 0;
  868. err_out:
  869. kfree(rule_buf);
  870. return ret;
  871. }
  872. static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
  873. struct ethtool_rxnfc *rx_rings,
  874. u32 size)
  875. {
  876. int i;
  877. if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
  878. return -EFAULT;
  879. /* Validate ring indices */
  880. for (i = 0; i < size; i++)
  881. if (indir[i] >= rx_rings->data)
  882. return -EINVAL;
  883. return 0;
  884. }
  885. u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
  886. void netdev_rss_key_fill(void *buffer, size_t len)
  887. {
  888. BUG_ON(len > sizeof(netdev_rss_key));
  889. net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
  890. memcpy(buffer, netdev_rss_key, len);
  891. }
  892. EXPORT_SYMBOL(netdev_rss_key_fill);
  893. static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
  894. {
  895. u32 dev_size, current_max = 0;
  896. u32 *indir;
  897. int ret;
  898. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  899. !dev->ethtool_ops->get_rxfh)
  900. return -EOPNOTSUPP;
  901. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  902. if (dev_size == 0)
  903. return -EOPNOTSUPP;
  904. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  905. if (!indir)
  906. return -ENOMEM;
  907. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  908. if (ret)
  909. goto out;
  910. while (dev_size--)
  911. current_max = max(current_max, indir[dev_size]);
  912. *max = current_max;
  913. out:
  914. kfree(indir);
  915. return ret;
  916. }
  917. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  918. void __user *useraddr)
  919. {
  920. u32 user_size, dev_size;
  921. u32 *indir;
  922. int ret;
  923. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  924. !dev->ethtool_ops->get_rxfh)
  925. return -EOPNOTSUPP;
  926. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  927. if (dev_size == 0)
  928. return -EOPNOTSUPP;
  929. if (copy_from_user(&user_size,
  930. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  931. sizeof(user_size)))
  932. return -EFAULT;
  933. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  934. &dev_size, sizeof(dev_size)))
  935. return -EFAULT;
  936. /* If the user buffer size is 0, this is just a query for the
  937. * device table size. Otherwise, if it's smaller than the
  938. * device table size it's an error.
  939. */
  940. if (user_size < dev_size)
  941. return user_size == 0 ? 0 : -EINVAL;
  942. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  943. if (!indir)
  944. return -ENOMEM;
  945. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  946. if (ret)
  947. goto out;
  948. if (copy_to_user(useraddr +
  949. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  950. indir, dev_size * sizeof(indir[0])))
  951. ret = -EFAULT;
  952. out:
  953. kfree(indir);
  954. return ret;
  955. }
  956. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  957. void __user *useraddr)
  958. {
  959. struct ethtool_rxnfc rx_rings;
  960. u32 user_size, dev_size, i;
  961. u32 *indir;
  962. const struct ethtool_ops *ops = dev->ethtool_ops;
  963. int ret;
  964. u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
  965. if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
  966. !ops->get_rxnfc)
  967. return -EOPNOTSUPP;
  968. dev_size = ops->get_rxfh_indir_size(dev);
  969. if (dev_size == 0)
  970. return -EOPNOTSUPP;
  971. if (copy_from_user(&user_size,
  972. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  973. sizeof(user_size)))
  974. return -EFAULT;
  975. if (user_size != 0 && user_size != dev_size)
  976. return -EINVAL;
  977. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  978. if (!indir)
  979. return -ENOMEM;
  980. rx_rings.cmd = ETHTOOL_GRXRINGS;
  981. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  982. if (ret)
  983. goto out;
  984. if (user_size == 0) {
  985. for (i = 0; i < dev_size; i++)
  986. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  987. } else {
  988. ret = ethtool_copy_validate_indir(indir,
  989. useraddr + ringidx_offset,
  990. &rx_rings,
  991. dev_size);
  992. if (ret)
  993. goto out;
  994. }
  995. ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
  996. if (ret)
  997. goto out;
  998. /* indicate whether rxfh was set to default */
  999. if (user_size == 0)
  1000. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1001. else
  1002. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1003. out:
  1004. kfree(indir);
  1005. return ret;
  1006. }
  1007. static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
  1008. void __user *useraddr)
  1009. {
  1010. int ret;
  1011. const struct ethtool_ops *ops = dev->ethtool_ops;
  1012. u32 user_indir_size, user_key_size;
  1013. u32 dev_indir_size = 0, dev_key_size = 0;
  1014. struct ethtool_rxfh rxfh;
  1015. u32 total_size;
  1016. u32 indir_bytes;
  1017. u32 *indir = NULL;
  1018. u8 dev_hfunc = 0;
  1019. u8 *hkey = NULL;
  1020. u8 *rss_config;
  1021. if (!ops->get_rxfh)
  1022. return -EOPNOTSUPP;
  1023. if (ops->get_rxfh_indir_size)
  1024. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1025. if (ops->get_rxfh_key_size)
  1026. dev_key_size = ops->get_rxfh_key_size(dev);
  1027. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1028. return -EFAULT;
  1029. user_indir_size = rxfh.indir_size;
  1030. user_key_size = rxfh.key_size;
  1031. /* Check that reserved fields are 0 for now */
  1032. if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
  1033. rxfh.rsvd8[2] || rxfh.rsvd32)
  1034. return -EINVAL;
  1035. rxfh.indir_size = dev_indir_size;
  1036. rxfh.key_size = dev_key_size;
  1037. if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
  1038. return -EFAULT;
  1039. if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
  1040. (user_key_size && (user_key_size != dev_key_size)))
  1041. return -EINVAL;
  1042. indir_bytes = user_indir_size * sizeof(indir[0]);
  1043. total_size = indir_bytes + user_key_size;
  1044. rss_config = kzalloc(total_size, GFP_USER);
  1045. if (!rss_config)
  1046. return -ENOMEM;
  1047. if (user_indir_size)
  1048. indir = (u32 *)rss_config;
  1049. if (user_key_size)
  1050. hkey = rss_config + indir_bytes;
  1051. ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
  1052. if (ret)
  1053. goto out;
  1054. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
  1055. &dev_hfunc, sizeof(rxfh.hfunc))) {
  1056. ret = -EFAULT;
  1057. } else if (copy_to_user(useraddr +
  1058. offsetof(struct ethtool_rxfh, rss_config[0]),
  1059. rss_config, total_size)) {
  1060. ret = -EFAULT;
  1061. }
  1062. out:
  1063. kfree(rss_config);
  1064. return ret;
  1065. }
  1066. static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
  1067. void __user *useraddr)
  1068. {
  1069. int ret;
  1070. const struct ethtool_ops *ops = dev->ethtool_ops;
  1071. struct ethtool_rxnfc rx_rings;
  1072. struct ethtool_rxfh rxfh;
  1073. u32 dev_indir_size = 0, dev_key_size = 0, i;
  1074. u32 *indir = NULL, indir_bytes = 0;
  1075. u8 *hkey = NULL;
  1076. u8 *rss_config;
  1077. u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
  1078. if (!ops->get_rxnfc || !ops->set_rxfh)
  1079. return -EOPNOTSUPP;
  1080. if (ops->get_rxfh_indir_size)
  1081. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1082. if (ops->get_rxfh_key_size)
  1083. dev_key_size = ops->get_rxfh_key_size(dev);
  1084. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1085. return -EFAULT;
  1086. /* Check that reserved fields are 0 for now */
  1087. if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
  1088. rxfh.rsvd8[2] || rxfh.rsvd32)
  1089. return -EINVAL;
  1090. /* If either indir, hash key or function is valid, proceed further.
  1091. * Must request at least one change: indir size, hash key or function.
  1092. */
  1093. if ((rxfh.indir_size &&
  1094. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
  1095. rxfh.indir_size != dev_indir_size) ||
  1096. (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
  1097. (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
  1098. rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
  1099. return -EINVAL;
  1100. if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1101. indir_bytes = dev_indir_size * sizeof(indir[0]);
  1102. rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
  1103. if (!rss_config)
  1104. return -ENOMEM;
  1105. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1106. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1107. if (ret)
  1108. goto out;
  1109. /* rxfh.indir_size == 0 means reset the indir table to default.
  1110. * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
  1111. */
  1112. if (rxfh.indir_size &&
  1113. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
  1114. indir = (u32 *)rss_config;
  1115. ret = ethtool_copy_validate_indir(indir,
  1116. useraddr + rss_cfg_offset,
  1117. &rx_rings,
  1118. rxfh.indir_size);
  1119. if (ret)
  1120. goto out;
  1121. } else if (rxfh.indir_size == 0) {
  1122. indir = (u32 *)rss_config;
  1123. for (i = 0; i < dev_indir_size; i++)
  1124. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1125. }
  1126. if (rxfh.key_size) {
  1127. hkey = rss_config + indir_bytes;
  1128. if (copy_from_user(hkey,
  1129. useraddr + rss_cfg_offset + indir_bytes,
  1130. rxfh.key_size)) {
  1131. ret = -EFAULT;
  1132. goto out;
  1133. }
  1134. }
  1135. ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
  1136. if (ret)
  1137. goto out;
  1138. /* indicate whether rxfh was set to default */
  1139. if (rxfh.indir_size == 0)
  1140. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1141. else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1142. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1143. out:
  1144. kfree(rss_config);
  1145. return ret;
  1146. }
  1147. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1148. {
  1149. struct ethtool_regs regs;
  1150. const struct ethtool_ops *ops = dev->ethtool_ops;
  1151. void *regbuf;
  1152. int reglen, ret;
  1153. if (!ops->get_regs || !ops->get_regs_len)
  1154. return -EOPNOTSUPP;
  1155. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1156. return -EFAULT;
  1157. reglen = ops->get_regs_len(dev);
  1158. if (regs.len > reglen)
  1159. regs.len = reglen;
  1160. regbuf = vzalloc(reglen);
  1161. if (reglen && !regbuf)
  1162. return -ENOMEM;
  1163. ops->get_regs(dev, &regs, regbuf);
  1164. ret = -EFAULT;
  1165. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1166. goto out;
  1167. useraddr += offsetof(struct ethtool_regs, data);
  1168. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  1169. goto out;
  1170. ret = 0;
  1171. out:
  1172. vfree(regbuf);
  1173. return ret;
  1174. }
  1175. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1176. {
  1177. struct ethtool_value reset;
  1178. int ret;
  1179. if (!dev->ethtool_ops->reset)
  1180. return -EOPNOTSUPP;
  1181. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1182. return -EFAULT;
  1183. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1184. if (ret)
  1185. return ret;
  1186. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1187. return -EFAULT;
  1188. return 0;
  1189. }
  1190. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1191. {
  1192. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1193. if (!dev->ethtool_ops->get_wol)
  1194. return -EOPNOTSUPP;
  1195. dev->ethtool_ops->get_wol(dev, &wol);
  1196. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1197. return -EFAULT;
  1198. return 0;
  1199. }
  1200. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1201. {
  1202. struct ethtool_wolinfo wol;
  1203. if (!dev->ethtool_ops->set_wol)
  1204. return -EOPNOTSUPP;
  1205. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1206. return -EFAULT;
  1207. return dev->ethtool_ops->set_wol(dev, &wol);
  1208. }
  1209. static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
  1210. {
  1211. struct ethtool_eee edata;
  1212. int rc;
  1213. if (!dev->ethtool_ops->get_eee)
  1214. return -EOPNOTSUPP;
  1215. memset(&edata, 0, sizeof(struct ethtool_eee));
  1216. edata.cmd = ETHTOOL_GEEE;
  1217. rc = dev->ethtool_ops->get_eee(dev, &edata);
  1218. if (rc)
  1219. return rc;
  1220. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1221. return -EFAULT;
  1222. return 0;
  1223. }
  1224. static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
  1225. {
  1226. struct ethtool_eee edata;
  1227. if (!dev->ethtool_ops->set_eee)
  1228. return -EOPNOTSUPP;
  1229. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1230. return -EFAULT;
  1231. return dev->ethtool_ops->set_eee(dev, &edata);
  1232. }
  1233. static int ethtool_nway_reset(struct net_device *dev)
  1234. {
  1235. if (!dev->ethtool_ops->nway_reset)
  1236. return -EOPNOTSUPP;
  1237. return dev->ethtool_ops->nway_reset(dev);
  1238. }
  1239. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1240. {
  1241. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1242. if (!dev->ethtool_ops->get_link)
  1243. return -EOPNOTSUPP;
  1244. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  1245. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1246. return -EFAULT;
  1247. return 0;
  1248. }
  1249. static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
  1250. int (*getter)(struct net_device *,
  1251. struct ethtool_eeprom *, u8 *),
  1252. u32 total_len)
  1253. {
  1254. struct ethtool_eeprom eeprom;
  1255. void __user *userbuf = useraddr + sizeof(eeprom);
  1256. u32 bytes_remaining;
  1257. u8 *data;
  1258. int ret = 0;
  1259. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1260. return -EFAULT;
  1261. /* Check for wrap and zero */
  1262. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1263. return -EINVAL;
  1264. /* Check for exceeding total eeprom len */
  1265. if (eeprom.offset + eeprom.len > total_len)
  1266. return -EINVAL;
  1267. data = kmalloc(PAGE_SIZE, GFP_USER);
  1268. if (!data)
  1269. return -ENOMEM;
  1270. bytes_remaining = eeprom.len;
  1271. while (bytes_remaining > 0) {
  1272. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1273. ret = getter(dev, &eeprom, data);
  1274. if (ret)
  1275. break;
  1276. if (copy_to_user(userbuf, data, eeprom.len)) {
  1277. ret = -EFAULT;
  1278. break;
  1279. }
  1280. userbuf += eeprom.len;
  1281. eeprom.offset += eeprom.len;
  1282. bytes_remaining -= eeprom.len;
  1283. }
  1284. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1285. eeprom.offset -= eeprom.len;
  1286. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1287. ret = -EFAULT;
  1288. kfree(data);
  1289. return ret;
  1290. }
  1291. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1292. {
  1293. const struct ethtool_ops *ops = dev->ethtool_ops;
  1294. if (!ops->get_eeprom || !ops->get_eeprom_len ||
  1295. !ops->get_eeprom_len(dev))
  1296. return -EOPNOTSUPP;
  1297. return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
  1298. ops->get_eeprom_len(dev));
  1299. }
  1300. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1301. {
  1302. struct ethtool_eeprom eeprom;
  1303. const struct ethtool_ops *ops = dev->ethtool_ops;
  1304. void __user *userbuf = useraddr + sizeof(eeprom);
  1305. u32 bytes_remaining;
  1306. u8 *data;
  1307. int ret = 0;
  1308. if (!ops->set_eeprom || !ops->get_eeprom_len ||
  1309. !ops->get_eeprom_len(dev))
  1310. return -EOPNOTSUPP;
  1311. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1312. return -EFAULT;
  1313. /* Check for wrap and zero */
  1314. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1315. return -EINVAL;
  1316. /* Check for exceeding total eeprom len */
  1317. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1318. return -EINVAL;
  1319. data = kmalloc(PAGE_SIZE, GFP_USER);
  1320. if (!data)
  1321. return -ENOMEM;
  1322. bytes_remaining = eeprom.len;
  1323. while (bytes_remaining > 0) {
  1324. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1325. if (copy_from_user(data, userbuf, eeprom.len)) {
  1326. ret = -EFAULT;
  1327. break;
  1328. }
  1329. ret = ops->set_eeprom(dev, &eeprom, data);
  1330. if (ret)
  1331. break;
  1332. userbuf += eeprom.len;
  1333. eeprom.offset += eeprom.len;
  1334. bytes_remaining -= eeprom.len;
  1335. }
  1336. kfree(data);
  1337. return ret;
  1338. }
  1339. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1340. void __user *useraddr)
  1341. {
  1342. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1343. if (!dev->ethtool_ops->get_coalesce)
  1344. return -EOPNOTSUPP;
  1345. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  1346. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1347. return -EFAULT;
  1348. return 0;
  1349. }
  1350. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1351. void __user *useraddr)
  1352. {
  1353. struct ethtool_coalesce coalesce;
  1354. if (!dev->ethtool_ops->set_coalesce)
  1355. return -EOPNOTSUPP;
  1356. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1357. return -EFAULT;
  1358. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  1359. }
  1360. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1361. {
  1362. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1363. if (!dev->ethtool_ops->get_ringparam)
  1364. return -EOPNOTSUPP;
  1365. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  1366. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1367. return -EFAULT;
  1368. return 0;
  1369. }
  1370. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1371. {
  1372. struct ethtool_ringparam ringparam;
  1373. if (!dev->ethtool_ops->set_ringparam)
  1374. return -EOPNOTSUPP;
  1375. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1376. return -EFAULT;
  1377. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  1378. }
  1379. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1380. void __user *useraddr)
  1381. {
  1382. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1383. if (!dev->ethtool_ops->get_channels)
  1384. return -EOPNOTSUPP;
  1385. dev->ethtool_ops->get_channels(dev, &channels);
  1386. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1387. return -EFAULT;
  1388. return 0;
  1389. }
  1390. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1391. void __user *useraddr)
  1392. {
  1393. struct ethtool_channels channels, max;
  1394. u32 max_rx_in_use = 0;
  1395. if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
  1396. return -EOPNOTSUPP;
  1397. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1398. return -EFAULT;
  1399. dev->ethtool_ops->get_channels(dev, &max);
  1400. /* ensure new counts are within the maximums */
  1401. if ((channels.rx_count > max.max_rx) ||
  1402. (channels.tx_count > max.max_tx) ||
  1403. (channels.combined_count > max.max_combined) ||
  1404. (channels.other_count > max.max_other))
  1405. return -EINVAL;
  1406. /* ensure the new Rx count fits within the configured Rx flow
  1407. * indirection table settings */
  1408. if (netif_is_rxfh_configured(dev) &&
  1409. !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
  1410. (channels.combined_count + channels.rx_count) <= max_rx_in_use)
  1411. return -EINVAL;
  1412. return dev->ethtool_ops->set_channels(dev, &channels);
  1413. }
  1414. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1415. {
  1416. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  1417. if (!dev->ethtool_ops->get_pauseparam)
  1418. return -EOPNOTSUPP;
  1419. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1420. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1421. return -EFAULT;
  1422. return 0;
  1423. }
  1424. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1425. {
  1426. struct ethtool_pauseparam pauseparam;
  1427. if (!dev->ethtool_ops->set_pauseparam)
  1428. return -EOPNOTSUPP;
  1429. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1430. return -EFAULT;
  1431. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1432. }
  1433. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1434. {
  1435. struct ethtool_test test;
  1436. const struct ethtool_ops *ops = dev->ethtool_ops;
  1437. u64 *data;
  1438. int ret, test_len;
  1439. if (!ops->self_test || !ops->get_sset_count)
  1440. return -EOPNOTSUPP;
  1441. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1442. if (test_len < 0)
  1443. return test_len;
  1444. WARN_ON(test_len == 0);
  1445. if (copy_from_user(&test, useraddr, sizeof(test)))
  1446. return -EFAULT;
  1447. test.len = test_len;
  1448. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1449. if (!data)
  1450. return -ENOMEM;
  1451. ops->self_test(dev, &test, data);
  1452. ret = -EFAULT;
  1453. if (copy_to_user(useraddr, &test, sizeof(test)))
  1454. goto out;
  1455. useraddr += sizeof(test);
  1456. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1457. goto out;
  1458. ret = 0;
  1459. out:
  1460. kfree(data);
  1461. return ret;
  1462. }
  1463. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1464. {
  1465. struct ethtool_gstrings gstrings;
  1466. u8 *data;
  1467. int ret;
  1468. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1469. return -EFAULT;
  1470. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1471. if (ret < 0)
  1472. return ret;
  1473. gstrings.len = ret;
  1474. data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
  1475. if (!data)
  1476. return -ENOMEM;
  1477. __ethtool_get_strings(dev, gstrings.string_set, data);
  1478. ret = -EFAULT;
  1479. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1480. goto out;
  1481. useraddr += sizeof(gstrings);
  1482. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1483. goto out;
  1484. ret = 0;
  1485. out:
  1486. kfree(data);
  1487. return ret;
  1488. }
  1489. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1490. {
  1491. struct ethtool_value id;
  1492. static bool busy;
  1493. const struct ethtool_ops *ops = dev->ethtool_ops;
  1494. int rc;
  1495. if (!ops->set_phys_id)
  1496. return -EOPNOTSUPP;
  1497. if (busy)
  1498. return -EBUSY;
  1499. if (copy_from_user(&id, useraddr, sizeof(id)))
  1500. return -EFAULT;
  1501. rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1502. if (rc < 0)
  1503. return rc;
  1504. /* Drop the RTNL lock while waiting, but prevent reentry or
  1505. * removal of the device.
  1506. */
  1507. busy = true;
  1508. dev_hold(dev);
  1509. rtnl_unlock();
  1510. if (rc == 0) {
  1511. /* Driver will handle this itself */
  1512. schedule_timeout_interruptible(
  1513. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1514. } else {
  1515. /* Driver expects to be called at twice the frequency in rc */
  1516. int n = rc * 2, i, interval = HZ / n;
  1517. /* Count down seconds */
  1518. do {
  1519. /* Count down iterations per second */
  1520. i = n;
  1521. do {
  1522. rtnl_lock();
  1523. rc = ops->set_phys_id(dev,
  1524. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1525. rtnl_unlock();
  1526. if (rc)
  1527. break;
  1528. schedule_timeout_interruptible(interval);
  1529. } while (!signal_pending(current) && --i != 0);
  1530. } while (!signal_pending(current) &&
  1531. (id.data == 0 || --id.data != 0));
  1532. }
  1533. rtnl_lock();
  1534. dev_put(dev);
  1535. busy = false;
  1536. (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1537. return rc;
  1538. }
  1539. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1540. {
  1541. struct ethtool_stats stats;
  1542. const struct ethtool_ops *ops = dev->ethtool_ops;
  1543. u64 *data;
  1544. int ret, n_stats;
  1545. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1546. return -EOPNOTSUPP;
  1547. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1548. if (n_stats < 0)
  1549. return n_stats;
  1550. WARN_ON(n_stats == 0);
  1551. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1552. return -EFAULT;
  1553. stats.n_stats = n_stats;
  1554. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  1555. if (!data)
  1556. return -ENOMEM;
  1557. ops->get_ethtool_stats(dev, &stats, data);
  1558. ret = -EFAULT;
  1559. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1560. goto out;
  1561. useraddr += sizeof(stats);
  1562. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1563. goto out;
  1564. ret = 0;
  1565. out:
  1566. kfree(data);
  1567. return ret;
  1568. }
  1569. static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
  1570. {
  1571. struct ethtool_stats stats;
  1572. struct phy_device *phydev = dev->phydev;
  1573. u64 *data;
  1574. int ret, n_stats;
  1575. if (!phydev)
  1576. return -EOPNOTSUPP;
  1577. n_stats = phy_get_sset_count(phydev);
  1578. if (n_stats < 0)
  1579. return n_stats;
  1580. WARN_ON(n_stats == 0);
  1581. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1582. return -EFAULT;
  1583. stats.n_stats = n_stats;
  1584. data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
  1585. if (!data)
  1586. return -ENOMEM;
  1587. mutex_lock(&phydev->lock);
  1588. phydev->drv->get_stats(phydev, &stats, data);
  1589. mutex_unlock(&phydev->lock);
  1590. ret = -EFAULT;
  1591. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1592. goto out;
  1593. useraddr += sizeof(stats);
  1594. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1595. goto out;
  1596. ret = 0;
  1597. out:
  1598. kfree(data);
  1599. return ret;
  1600. }
  1601. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1602. {
  1603. struct ethtool_perm_addr epaddr;
  1604. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1605. return -EFAULT;
  1606. if (epaddr.size < dev->addr_len)
  1607. return -ETOOSMALL;
  1608. epaddr.size = dev->addr_len;
  1609. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1610. return -EFAULT;
  1611. useraddr += sizeof(epaddr);
  1612. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1613. return -EFAULT;
  1614. return 0;
  1615. }
  1616. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1617. u32 cmd, u32 (*actor)(struct net_device *))
  1618. {
  1619. struct ethtool_value edata = { .cmd = cmd };
  1620. if (!actor)
  1621. return -EOPNOTSUPP;
  1622. edata.data = actor(dev);
  1623. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1624. return -EFAULT;
  1625. return 0;
  1626. }
  1627. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1628. void (*actor)(struct net_device *, u32))
  1629. {
  1630. struct ethtool_value edata;
  1631. if (!actor)
  1632. return -EOPNOTSUPP;
  1633. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1634. return -EFAULT;
  1635. actor(dev, edata.data);
  1636. return 0;
  1637. }
  1638. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1639. int (*actor)(struct net_device *, u32))
  1640. {
  1641. struct ethtool_value edata;
  1642. if (!actor)
  1643. return -EOPNOTSUPP;
  1644. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1645. return -EFAULT;
  1646. return actor(dev, edata.data);
  1647. }
  1648. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1649. char __user *useraddr)
  1650. {
  1651. struct ethtool_flash efl;
  1652. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1653. return -EFAULT;
  1654. if (!dev->ethtool_ops->flash_device)
  1655. return -EOPNOTSUPP;
  1656. efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  1657. return dev->ethtool_ops->flash_device(dev, &efl);
  1658. }
  1659. static int ethtool_set_dump(struct net_device *dev,
  1660. void __user *useraddr)
  1661. {
  1662. struct ethtool_dump dump;
  1663. if (!dev->ethtool_ops->set_dump)
  1664. return -EOPNOTSUPP;
  1665. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1666. return -EFAULT;
  1667. return dev->ethtool_ops->set_dump(dev, &dump);
  1668. }
  1669. static int ethtool_get_dump_flag(struct net_device *dev,
  1670. void __user *useraddr)
  1671. {
  1672. int ret;
  1673. struct ethtool_dump dump;
  1674. const struct ethtool_ops *ops = dev->ethtool_ops;
  1675. if (!ops->get_dump_flag)
  1676. return -EOPNOTSUPP;
  1677. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1678. return -EFAULT;
  1679. ret = ops->get_dump_flag(dev, &dump);
  1680. if (ret)
  1681. return ret;
  1682. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1683. return -EFAULT;
  1684. return 0;
  1685. }
  1686. static int ethtool_get_dump_data(struct net_device *dev,
  1687. void __user *useraddr)
  1688. {
  1689. int ret;
  1690. __u32 len;
  1691. struct ethtool_dump dump, tmp;
  1692. const struct ethtool_ops *ops = dev->ethtool_ops;
  1693. void *data = NULL;
  1694. if (!ops->get_dump_data || !ops->get_dump_flag)
  1695. return -EOPNOTSUPP;
  1696. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1697. return -EFAULT;
  1698. memset(&tmp, 0, sizeof(tmp));
  1699. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1700. ret = ops->get_dump_flag(dev, &tmp);
  1701. if (ret)
  1702. return ret;
  1703. len = min(tmp.len, dump.len);
  1704. if (!len)
  1705. return -EFAULT;
  1706. /* Don't ever let the driver think there's more space available
  1707. * than it requested with .get_dump_flag().
  1708. */
  1709. dump.len = len;
  1710. /* Always allocate enough space to hold the whole thing so that the
  1711. * driver does not need to check the length and bother with partial
  1712. * dumping.
  1713. */
  1714. data = vzalloc(tmp.len);
  1715. if (!data)
  1716. return -ENOMEM;
  1717. ret = ops->get_dump_data(dev, &dump, data);
  1718. if (ret)
  1719. goto out;
  1720. /* There are two sane possibilities:
  1721. * 1. The driver's .get_dump_data() does not touch dump.len.
  1722. * 2. Or it may set dump.len to how much it really writes, which
  1723. * should be tmp.len (or len if it can do a partial dump).
  1724. * In any case respond to userspace with the actual length of data
  1725. * it's receiving.
  1726. */
  1727. WARN_ON(dump.len != len && dump.len != tmp.len);
  1728. dump.len = len;
  1729. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1730. ret = -EFAULT;
  1731. goto out;
  1732. }
  1733. useraddr += offsetof(struct ethtool_dump, data);
  1734. if (copy_to_user(useraddr, data, len))
  1735. ret = -EFAULT;
  1736. out:
  1737. vfree(data);
  1738. return ret;
  1739. }
  1740. static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
  1741. {
  1742. int err = 0;
  1743. struct ethtool_ts_info info;
  1744. const struct ethtool_ops *ops = dev->ethtool_ops;
  1745. struct phy_device *phydev = dev->phydev;
  1746. memset(&info, 0, sizeof(info));
  1747. info.cmd = ETHTOOL_GET_TS_INFO;
  1748. if (phydev && phydev->drv && phydev->drv->ts_info) {
  1749. err = phydev->drv->ts_info(phydev, &info);
  1750. } else if (ops->get_ts_info) {
  1751. err = ops->get_ts_info(dev, &info);
  1752. } else {
  1753. info.so_timestamping =
  1754. SOF_TIMESTAMPING_RX_SOFTWARE |
  1755. SOF_TIMESTAMPING_SOFTWARE;
  1756. info.phc_index = -1;
  1757. }
  1758. if (err)
  1759. return err;
  1760. if (copy_to_user(useraddr, &info, sizeof(info)))
  1761. err = -EFAULT;
  1762. return err;
  1763. }
  1764. static int __ethtool_get_module_info(struct net_device *dev,
  1765. struct ethtool_modinfo *modinfo)
  1766. {
  1767. const struct ethtool_ops *ops = dev->ethtool_ops;
  1768. struct phy_device *phydev = dev->phydev;
  1769. if (phydev && phydev->drv && phydev->drv->module_info)
  1770. return phydev->drv->module_info(phydev, modinfo);
  1771. if (ops->get_module_info)
  1772. return ops->get_module_info(dev, modinfo);
  1773. return -EOPNOTSUPP;
  1774. }
  1775. static int ethtool_get_module_info(struct net_device *dev,
  1776. void __user *useraddr)
  1777. {
  1778. int ret;
  1779. struct ethtool_modinfo modinfo;
  1780. if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
  1781. return -EFAULT;
  1782. ret = __ethtool_get_module_info(dev, &modinfo);
  1783. if (ret)
  1784. return ret;
  1785. if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
  1786. return -EFAULT;
  1787. return 0;
  1788. }
  1789. static int __ethtool_get_module_eeprom(struct net_device *dev,
  1790. struct ethtool_eeprom *ee, u8 *data)
  1791. {
  1792. const struct ethtool_ops *ops = dev->ethtool_ops;
  1793. struct phy_device *phydev = dev->phydev;
  1794. if (phydev && phydev->drv && phydev->drv->module_eeprom)
  1795. return phydev->drv->module_eeprom(phydev, ee, data);
  1796. if (ops->get_module_eeprom)
  1797. return ops->get_module_eeprom(dev, ee, data);
  1798. return -EOPNOTSUPP;
  1799. }
  1800. static int ethtool_get_module_eeprom(struct net_device *dev,
  1801. void __user *useraddr)
  1802. {
  1803. int ret;
  1804. struct ethtool_modinfo modinfo;
  1805. ret = __ethtool_get_module_info(dev, &modinfo);
  1806. if (ret)
  1807. return ret;
  1808. return ethtool_get_any_eeprom(dev, useraddr,
  1809. __ethtool_get_module_eeprom,
  1810. modinfo.eeprom_len);
  1811. }
  1812. static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
  1813. {
  1814. switch (tuna->id) {
  1815. case ETHTOOL_RX_COPYBREAK:
  1816. case ETHTOOL_TX_COPYBREAK:
  1817. if (tuna->len != sizeof(u32) ||
  1818. tuna->type_id != ETHTOOL_TUNABLE_U32)
  1819. return -EINVAL;
  1820. break;
  1821. default:
  1822. return -EINVAL;
  1823. }
  1824. return 0;
  1825. }
  1826. static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
  1827. {
  1828. int ret;
  1829. struct ethtool_tunable tuna;
  1830. const struct ethtool_ops *ops = dev->ethtool_ops;
  1831. void *data;
  1832. if (!ops->get_tunable)
  1833. return -EOPNOTSUPP;
  1834. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1835. return -EFAULT;
  1836. ret = ethtool_tunable_valid(&tuna);
  1837. if (ret)
  1838. return ret;
  1839. data = kmalloc(tuna.len, GFP_USER);
  1840. if (!data)
  1841. return -ENOMEM;
  1842. ret = ops->get_tunable(dev, &tuna, data);
  1843. if (ret)
  1844. goto out;
  1845. useraddr += sizeof(tuna);
  1846. ret = -EFAULT;
  1847. if (copy_to_user(useraddr, data, tuna.len))
  1848. goto out;
  1849. ret = 0;
  1850. out:
  1851. kfree(data);
  1852. return ret;
  1853. }
  1854. static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
  1855. {
  1856. int ret;
  1857. struct ethtool_tunable tuna;
  1858. const struct ethtool_ops *ops = dev->ethtool_ops;
  1859. void *data;
  1860. if (!ops->set_tunable)
  1861. return -EOPNOTSUPP;
  1862. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1863. return -EFAULT;
  1864. ret = ethtool_tunable_valid(&tuna);
  1865. if (ret)
  1866. return ret;
  1867. data = kmalloc(tuna.len, GFP_USER);
  1868. if (!data)
  1869. return -ENOMEM;
  1870. useraddr += sizeof(tuna);
  1871. ret = -EFAULT;
  1872. if (copy_from_user(data, useraddr, tuna.len))
  1873. goto out;
  1874. ret = ops->set_tunable(dev, &tuna, data);
  1875. out:
  1876. kfree(data);
  1877. return ret;
  1878. }
  1879. static int ethtool_get_per_queue_coalesce(struct net_device *dev,
  1880. void __user *useraddr,
  1881. struct ethtool_per_queue_op *per_queue_opt)
  1882. {
  1883. u32 bit;
  1884. int ret;
  1885. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1886. if (!dev->ethtool_ops->get_per_queue_coalesce)
  1887. return -EOPNOTSUPP;
  1888. useraddr += sizeof(*per_queue_opt);
  1889. bitmap_from_u32array(queue_mask,
  1890. MAX_NUM_QUEUE,
  1891. per_queue_opt->queue_mask,
  1892. DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
  1893. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  1894. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1895. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
  1896. if (ret != 0)
  1897. return ret;
  1898. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1899. return -EFAULT;
  1900. useraddr += sizeof(coalesce);
  1901. }
  1902. return 0;
  1903. }
  1904. static int ethtool_set_per_queue_coalesce(struct net_device *dev,
  1905. void __user *useraddr,
  1906. struct ethtool_per_queue_op *per_queue_opt)
  1907. {
  1908. u32 bit;
  1909. int i, ret = 0;
  1910. int n_queue;
  1911. struct ethtool_coalesce *backup = NULL, *tmp = NULL;
  1912. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1913. if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
  1914. (!dev->ethtool_ops->get_per_queue_coalesce))
  1915. return -EOPNOTSUPP;
  1916. useraddr += sizeof(*per_queue_opt);
  1917. bitmap_from_u32array(queue_mask,
  1918. MAX_NUM_QUEUE,
  1919. per_queue_opt->queue_mask,
  1920. DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
  1921. n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
  1922. tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
  1923. if (!backup)
  1924. return -ENOMEM;
  1925. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  1926. struct ethtool_coalesce coalesce;
  1927. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
  1928. if (ret != 0)
  1929. goto roll_back;
  1930. tmp++;
  1931. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
  1932. ret = -EFAULT;
  1933. goto roll_back;
  1934. }
  1935. ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
  1936. if (ret != 0)
  1937. goto roll_back;
  1938. useraddr += sizeof(coalesce);
  1939. }
  1940. roll_back:
  1941. if (ret != 0) {
  1942. tmp = backup;
  1943. for_each_set_bit(i, queue_mask, bit) {
  1944. dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
  1945. tmp++;
  1946. }
  1947. }
  1948. kfree(backup);
  1949. return ret;
  1950. }
  1951. static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
  1952. {
  1953. struct ethtool_per_queue_op per_queue_opt;
  1954. if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
  1955. return -EFAULT;
  1956. switch (per_queue_opt.sub_command) {
  1957. case ETHTOOL_GCOALESCE:
  1958. return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  1959. case ETHTOOL_SCOALESCE:
  1960. return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  1961. default:
  1962. return -EOPNOTSUPP;
  1963. };
  1964. }
  1965. /* The main entry point in this file. Called from net/core/dev_ioctl.c */
  1966. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1967. {
  1968. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1969. void __user *useraddr = ifr->ifr_data;
  1970. u32 ethcmd, sub_cmd;
  1971. int rc;
  1972. netdev_features_t old_features;
  1973. if (!dev || !netif_device_present(dev))
  1974. return -ENODEV;
  1975. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1976. return -EFAULT;
  1977. if (ethcmd == ETHTOOL_PERQUEUE) {
  1978. if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
  1979. return -EFAULT;
  1980. } else {
  1981. sub_cmd = ethcmd;
  1982. }
  1983. /* Allow some commands to be done by anyone */
  1984. switch (sub_cmd) {
  1985. case ETHTOOL_GSET:
  1986. case ETHTOOL_GDRVINFO:
  1987. case ETHTOOL_GMSGLVL:
  1988. case ETHTOOL_GLINK:
  1989. case ETHTOOL_GCOALESCE:
  1990. case ETHTOOL_GRINGPARAM:
  1991. case ETHTOOL_GPAUSEPARAM:
  1992. case ETHTOOL_GRXCSUM:
  1993. case ETHTOOL_GTXCSUM:
  1994. case ETHTOOL_GSG:
  1995. case ETHTOOL_GSSET_INFO:
  1996. case ETHTOOL_GSTRINGS:
  1997. case ETHTOOL_GSTATS:
  1998. case ETHTOOL_GPHYSTATS:
  1999. case ETHTOOL_GTSO:
  2000. case ETHTOOL_GPERMADDR:
  2001. case ETHTOOL_GUFO:
  2002. case ETHTOOL_GGSO:
  2003. case ETHTOOL_GGRO:
  2004. case ETHTOOL_GFLAGS:
  2005. case ETHTOOL_GPFLAGS:
  2006. case ETHTOOL_GRXFH:
  2007. case ETHTOOL_GRXRINGS:
  2008. case ETHTOOL_GRXCLSRLCNT:
  2009. case ETHTOOL_GRXCLSRULE:
  2010. case ETHTOOL_GRXCLSRLALL:
  2011. case ETHTOOL_GRXFHINDIR:
  2012. case ETHTOOL_GRSSH:
  2013. case ETHTOOL_GFEATURES:
  2014. case ETHTOOL_GCHANNELS:
  2015. case ETHTOOL_GET_TS_INFO:
  2016. case ETHTOOL_GEEE:
  2017. case ETHTOOL_GTUNABLE:
  2018. break;
  2019. default:
  2020. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2021. return -EPERM;
  2022. }
  2023. if (dev->ethtool_ops->begin) {
  2024. rc = dev->ethtool_ops->begin(dev);
  2025. if (rc < 0)
  2026. return rc;
  2027. }
  2028. old_features = dev->features;
  2029. switch (ethcmd) {
  2030. case ETHTOOL_GSET:
  2031. rc = ethtool_get_settings(dev, useraddr);
  2032. break;
  2033. case ETHTOOL_SSET:
  2034. rc = ethtool_set_settings(dev, useraddr);
  2035. break;
  2036. case ETHTOOL_GDRVINFO:
  2037. rc = ethtool_get_drvinfo(dev, useraddr);
  2038. break;
  2039. case ETHTOOL_GREGS:
  2040. rc = ethtool_get_regs(dev, useraddr);
  2041. break;
  2042. case ETHTOOL_GWOL:
  2043. rc = ethtool_get_wol(dev, useraddr);
  2044. break;
  2045. case ETHTOOL_SWOL:
  2046. rc = ethtool_set_wol(dev, useraddr);
  2047. break;
  2048. case ETHTOOL_GMSGLVL:
  2049. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2050. dev->ethtool_ops->get_msglevel);
  2051. break;
  2052. case ETHTOOL_SMSGLVL:
  2053. rc = ethtool_set_value_void(dev, useraddr,
  2054. dev->ethtool_ops->set_msglevel);
  2055. break;
  2056. case ETHTOOL_GEEE:
  2057. rc = ethtool_get_eee(dev, useraddr);
  2058. break;
  2059. case ETHTOOL_SEEE:
  2060. rc = ethtool_set_eee(dev, useraddr);
  2061. break;
  2062. case ETHTOOL_NWAY_RST:
  2063. rc = ethtool_nway_reset(dev);
  2064. break;
  2065. case ETHTOOL_GLINK:
  2066. rc = ethtool_get_link(dev, useraddr);
  2067. break;
  2068. case ETHTOOL_GEEPROM:
  2069. rc = ethtool_get_eeprom(dev, useraddr);
  2070. break;
  2071. case ETHTOOL_SEEPROM:
  2072. rc = ethtool_set_eeprom(dev, useraddr);
  2073. break;
  2074. case ETHTOOL_GCOALESCE:
  2075. rc = ethtool_get_coalesce(dev, useraddr);
  2076. break;
  2077. case ETHTOOL_SCOALESCE:
  2078. rc = ethtool_set_coalesce(dev, useraddr);
  2079. break;
  2080. case ETHTOOL_GRINGPARAM:
  2081. rc = ethtool_get_ringparam(dev, useraddr);
  2082. break;
  2083. case ETHTOOL_SRINGPARAM:
  2084. rc = ethtool_set_ringparam(dev, useraddr);
  2085. break;
  2086. case ETHTOOL_GPAUSEPARAM:
  2087. rc = ethtool_get_pauseparam(dev, useraddr);
  2088. break;
  2089. case ETHTOOL_SPAUSEPARAM:
  2090. rc = ethtool_set_pauseparam(dev, useraddr);
  2091. break;
  2092. case ETHTOOL_TEST:
  2093. rc = ethtool_self_test(dev, useraddr);
  2094. break;
  2095. case ETHTOOL_GSTRINGS:
  2096. rc = ethtool_get_strings(dev, useraddr);
  2097. break;
  2098. case ETHTOOL_PHYS_ID:
  2099. rc = ethtool_phys_id(dev, useraddr);
  2100. break;
  2101. case ETHTOOL_GSTATS:
  2102. rc = ethtool_get_stats(dev, useraddr);
  2103. break;
  2104. case ETHTOOL_GPERMADDR:
  2105. rc = ethtool_get_perm_addr(dev, useraddr);
  2106. break;
  2107. case ETHTOOL_GFLAGS:
  2108. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2109. __ethtool_get_flags);
  2110. break;
  2111. case ETHTOOL_SFLAGS:
  2112. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  2113. break;
  2114. case ETHTOOL_GPFLAGS:
  2115. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2116. dev->ethtool_ops->get_priv_flags);
  2117. break;
  2118. case ETHTOOL_SPFLAGS:
  2119. rc = ethtool_set_value(dev, useraddr,
  2120. dev->ethtool_ops->set_priv_flags);
  2121. break;
  2122. case ETHTOOL_GRXFH:
  2123. case ETHTOOL_GRXRINGS:
  2124. case ETHTOOL_GRXCLSRLCNT:
  2125. case ETHTOOL_GRXCLSRULE:
  2126. case ETHTOOL_GRXCLSRLALL:
  2127. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  2128. break;
  2129. case ETHTOOL_SRXFH:
  2130. case ETHTOOL_SRXCLSRLDEL:
  2131. case ETHTOOL_SRXCLSRLINS:
  2132. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  2133. break;
  2134. case ETHTOOL_FLASHDEV:
  2135. rc = ethtool_flash_device(dev, useraddr);
  2136. break;
  2137. case ETHTOOL_RESET:
  2138. rc = ethtool_reset(dev, useraddr);
  2139. break;
  2140. case ETHTOOL_GSSET_INFO:
  2141. rc = ethtool_get_sset_info(dev, useraddr);
  2142. break;
  2143. case ETHTOOL_GRXFHINDIR:
  2144. rc = ethtool_get_rxfh_indir(dev, useraddr);
  2145. break;
  2146. case ETHTOOL_SRXFHINDIR:
  2147. rc = ethtool_set_rxfh_indir(dev, useraddr);
  2148. break;
  2149. case ETHTOOL_GRSSH:
  2150. rc = ethtool_get_rxfh(dev, useraddr);
  2151. break;
  2152. case ETHTOOL_SRSSH:
  2153. rc = ethtool_set_rxfh(dev, useraddr);
  2154. break;
  2155. case ETHTOOL_GFEATURES:
  2156. rc = ethtool_get_features(dev, useraddr);
  2157. break;
  2158. case ETHTOOL_SFEATURES:
  2159. rc = ethtool_set_features(dev, useraddr);
  2160. break;
  2161. case ETHTOOL_GTXCSUM:
  2162. case ETHTOOL_GRXCSUM:
  2163. case ETHTOOL_GSG:
  2164. case ETHTOOL_GTSO:
  2165. case ETHTOOL_GUFO:
  2166. case ETHTOOL_GGSO:
  2167. case ETHTOOL_GGRO:
  2168. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  2169. break;
  2170. case ETHTOOL_STXCSUM:
  2171. case ETHTOOL_SRXCSUM:
  2172. case ETHTOOL_SSG:
  2173. case ETHTOOL_STSO:
  2174. case ETHTOOL_SUFO:
  2175. case ETHTOOL_SGSO:
  2176. case ETHTOOL_SGRO:
  2177. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  2178. break;
  2179. case ETHTOOL_GCHANNELS:
  2180. rc = ethtool_get_channels(dev, useraddr);
  2181. break;
  2182. case ETHTOOL_SCHANNELS:
  2183. rc = ethtool_set_channels(dev, useraddr);
  2184. break;
  2185. case ETHTOOL_SET_DUMP:
  2186. rc = ethtool_set_dump(dev, useraddr);
  2187. break;
  2188. case ETHTOOL_GET_DUMP_FLAG:
  2189. rc = ethtool_get_dump_flag(dev, useraddr);
  2190. break;
  2191. case ETHTOOL_GET_DUMP_DATA:
  2192. rc = ethtool_get_dump_data(dev, useraddr);
  2193. break;
  2194. case ETHTOOL_GET_TS_INFO:
  2195. rc = ethtool_get_ts_info(dev, useraddr);
  2196. break;
  2197. case ETHTOOL_GMODULEINFO:
  2198. rc = ethtool_get_module_info(dev, useraddr);
  2199. break;
  2200. case ETHTOOL_GMODULEEEPROM:
  2201. rc = ethtool_get_module_eeprom(dev, useraddr);
  2202. break;
  2203. case ETHTOOL_GTUNABLE:
  2204. rc = ethtool_get_tunable(dev, useraddr);
  2205. break;
  2206. case ETHTOOL_STUNABLE:
  2207. rc = ethtool_set_tunable(dev, useraddr);
  2208. break;
  2209. case ETHTOOL_GPHYSTATS:
  2210. rc = ethtool_get_phy_stats(dev, useraddr);
  2211. break;
  2212. case ETHTOOL_PERQUEUE:
  2213. rc = ethtool_set_per_queue(dev, useraddr);
  2214. break;
  2215. case ETHTOOL_GLINKSETTINGS:
  2216. rc = ethtool_get_link_ksettings(dev, useraddr);
  2217. break;
  2218. case ETHTOOL_SLINKSETTINGS:
  2219. rc = ethtool_set_link_ksettings(dev, useraddr);
  2220. break;
  2221. default:
  2222. rc = -EOPNOTSUPP;
  2223. }
  2224. if (dev->ethtool_ops->complete)
  2225. dev->ethtool_ops->complete(dev);
  2226. if (old_features != dev->features)
  2227. netdev_features_change(dev);
  2228. return rc;
  2229. }