ethtool.c 68 KB

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