ethtool.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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. /* Query device for its ethtool_cmd settings.
  636. *
  637. * Backward compatibility note: for compatibility with legacy ethtool,
  638. * this has to work with both drivers implementing get_link_ksettings
  639. * API and drivers implementing get_settings API. When drivers
  640. * implement get_link_ksettings and report higher link mode bits, a
  641. * kernel warning is logged once (with name of 1st driver/device) to
  642. * recommend user to upgrade ethtool, but the command is successful
  643. * (only the lower link mode bits reported back to user).
  644. */
  645. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  646. {
  647. struct ethtool_cmd cmd;
  648. ASSERT_RTNL();
  649. if (dev->ethtool_ops->get_link_ksettings) {
  650. /* First, use link_ksettings API if it is supported */
  651. int err;
  652. struct ethtool_link_ksettings link_ksettings;
  653. memset(&link_ksettings, 0, sizeof(link_ksettings));
  654. err = dev->ethtool_ops->get_link_ksettings(dev,
  655. &link_ksettings);
  656. if (err < 0)
  657. return err;
  658. convert_link_ksettings_to_legacy_settings(&cmd,
  659. &link_ksettings);
  660. /* send a sensible cmd tag back to user */
  661. cmd.cmd = ETHTOOL_GSET;
  662. } else {
  663. /* driver doesn't support %ethtool_link_ksettings
  664. * API. revert to legacy %ethtool_cmd API, unless it's
  665. * not supported either.
  666. */
  667. int err;
  668. if (!dev->ethtool_ops->get_settings)
  669. return -EOPNOTSUPP;
  670. memset(&cmd, 0, sizeof(cmd));
  671. cmd.cmd = ETHTOOL_GSET;
  672. err = dev->ethtool_ops->get_settings(dev, &cmd);
  673. if (err < 0)
  674. return err;
  675. }
  676. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  677. return -EFAULT;
  678. return 0;
  679. }
  680. /* Update device link settings with given ethtool_cmd.
  681. *
  682. * Backward compatibility note: for compatibility with legacy ethtool,
  683. * this has to work with both drivers implementing set_link_ksettings
  684. * API and drivers implementing set_settings API. When drivers
  685. * implement set_link_ksettings and user's request updates deprecated
  686. * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
  687. * warning is logged once (with name of 1st driver/device) to
  688. * recommend user to upgrade ethtool, and the request is rejected.
  689. */
  690. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  691. {
  692. struct ethtool_cmd cmd;
  693. ASSERT_RTNL();
  694. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  695. return -EFAULT;
  696. /* first, try new %ethtool_link_ksettings API. */
  697. if (dev->ethtool_ops->set_link_ksettings) {
  698. struct ethtool_link_ksettings link_ksettings;
  699. if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
  700. &cmd))
  701. return -EINVAL;
  702. link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
  703. link_ksettings.base.link_mode_masks_nwords
  704. = __ETHTOOL_LINK_MODE_MASK_NU32;
  705. return dev->ethtool_ops->set_link_ksettings(dev,
  706. &link_ksettings);
  707. }
  708. /* legacy %ethtool_cmd API */
  709. /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
  710. * disappears internally
  711. */
  712. if (!dev->ethtool_ops->set_settings)
  713. return -EOPNOTSUPP;
  714. return dev->ethtool_ops->set_settings(dev, &cmd);
  715. }
  716. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  717. void __user *useraddr)
  718. {
  719. struct ethtool_drvinfo info;
  720. const struct ethtool_ops *ops = dev->ethtool_ops;
  721. memset(&info, 0, sizeof(info));
  722. info.cmd = ETHTOOL_GDRVINFO;
  723. if (ops->get_drvinfo) {
  724. ops->get_drvinfo(dev, &info);
  725. } else if (dev->dev.parent && dev->dev.parent->driver) {
  726. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  727. sizeof(info.bus_info));
  728. strlcpy(info.driver, dev->dev.parent->driver->name,
  729. sizeof(info.driver));
  730. } else {
  731. return -EOPNOTSUPP;
  732. }
  733. /*
  734. * this method of obtaining string set info is deprecated;
  735. * Use ETHTOOL_GSSET_INFO instead.
  736. */
  737. if (ops->get_sset_count) {
  738. int rc;
  739. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  740. if (rc >= 0)
  741. info.testinfo_len = rc;
  742. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  743. if (rc >= 0)
  744. info.n_stats = rc;
  745. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  746. if (rc >= 0)
  747. info.n_priv_flags = rc;
  748. }
  749. if (ops->get_regs_len)
  750. info.regdump_len = ops->get_regs_len(dev);
  751. if (ops->get_eeprom_len)
  752. info.eedump_len = ops->get_eeprom_len(dev);
  753. if (copy_to_user(useraddr, &info, sizeof(info)))
  754. return -EFAULT;
  755. return 0;
  756. }
  757. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  758. void __user *useraddr)
  759. {
  760. struct ethtool_sset_info info;
  761. u64 sset_mask;
  762. int i, idx = 0, n_bits = 0, ret, rc;
  763. u32 *info_buf = NULL;
  764. if (copy_from_user(&info, useraddr, sizeof(info)))
  765. return -EFAULT;
  766. /* store copy of mask, because we zero struct later on */
  767. sset_mask = info.sset_mask;
  768. if (!sset_mask)
  769. return 0;
  770. /* calculate size of return buffer */
  771. n_bits = hweight64(sset_mask);
  772. memset(&info, 0, sizeof(info));
  773. info.cmd = ETHTOOL_GSSET_INFO;
  774. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  775. if (!info_buf)
  776. return -ENOMEM;
  777. /*
  778. * fill return buffer based on input bitmask and successful
  779. * get_sset_count return
  780. */
  781. for (i = 0; i < 64; i++) {
  782. if (!(sset_mask & (1ULL << i)))
  783. continue;
  784. rc = __ethtool_get_sset_count(dev, i);
  785. if (rc >= 0) {
  786. info.sset_mask |= (1ULL << i);
  787. info_buf[idx++] = rc;
  788. }
  789. }
  790. ret = -EFAULT;
  791. if (copy_to_user(useraddr, &info, sizeof(info)))
  792. goto out;
  793. useraddr += offsetof(struct ethtool_sset_info, data);
  794. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  795. goto out;
  796. ret = 0;
  797. out:
  798. kfree(info_buf);
  799. return ret;
  800. }
  801. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  802. u32 cmd, void __user *useraddr)
  803. {
  804. struct ethtool_rxnfc info;
  805. size_t info_size = sizeof(info);
  806. int rc;
  807. if (!dev->ethtool_ops->set_rxnfc)
  808. return -EOPNOTSUPP;
  809. /* struct ethtool_rxnfc was originally defined for
  810. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  811. * members. User-space might still be using that
  812. * definition. */
  813. if (cmd == ETHTOOL_SRXFH)
  814. info_size = (offsetof(struct ethtool_rxnfc, data) +
  815. sizeof(info.data));
  816. if (copy_from_user(&info, useraddr, info_size))
  817. return -EFAULT;
  818. rc = dev->ethtool_ops->set_rxnfc(dev, &info);
  819. if (rc)
  820. return rc;
  821. if (cmd == ETHTOOL_SRXCLSRLINS &&
  822. copy_to_user(useraddr, &info, info_size))
  823. return -EFAULT;
  824. return 0;
  825. }
  826. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  827. u32 cmd, void __user *useraddr)
  828. {
  829. struct ethtool_rxnfc info;
  830. size_t info_size = sizeof(info);
  831. const struct ethtool_ops *ops = dev->ethtool_ops;
  832. int ret;
  833. void *rule_buf = NULL;
  834. if (!ops->get_rxnfc)
  835. return -EOPNOTSUPP;
  836. /* struct ethtool_rxnfc was originally defined for
  837. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  838. * members. User-space might still be using that
  839. * definition. */
  840. if (cmd == ETHTOOL_GRXFH)
  841. info_size = (offsetof(struct ethtool_rxnfc, data) +
  842. sizeof(info.data));
  843. if (copy_from_user(&info, useraddr, info_size))
  844. return -EFAULT;
  845. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  846. if (info.rule_cnt > 0) {
  847. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  848. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  849. GFP_USER);
  850. if (!rule_buf)
  851. return -ENOMEM;
  852. }
  853. }
  854. ret = ops->get_rxnfc(dev, &info, rule_buf);
  855. if (ret < 0)
  856. goto err_out;
  857. ret = -EFAULT;
  858. if (copy_to_user(useraddr, &info, info_size))
  859. goto err_out;
  860. if (rule_buf) {
  861. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  862. if (copy_to_user(useraddr, rule_buf,
  863. info.rule_cnt * sizeof(u32)))
  864. goto err_out;
  865. }
  866. ret = 0;
  867. err_out:
  868. kfree(rule_buf);
  869. return ret;
  870. }
  871. static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
  872. struct ethtool_rxnfc *rx_rings,
  873. u32 size)
  874. {
  875. int i;
  876. if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
  877. return -EFAULT;
  878. /* Validate ring indices */
  879. for (i = 0; i < size; i++)
  880. if (indir[i] >= rx_rings->data)
  881. return -EINVAL;
  882. return 0;
  883. }
  884. u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
  885. void netdev_rss_key_fill(void *buffer, size_t len)
  886. {
  887. BUG_ON(len > sizeof(netdev_rss_key));
  888. net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
  889. memcpy(buffer, netdev_rss_key, len);
  890. }
  891. EXPORT_SYMBOL(netdev_rss_key_fill);
  892. static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
  893. {
  894. u32 dev_size, current_max = 0;
  895. u32 *indir;
  896. int ret;
  897. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  898. !dev->ethtool_ops->get_rxfh)
  899. return -EOPNOTSUPP;
  900. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  901. if (dev_size == 0)
  902. return -EOPNOTSUPP;
  903. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  904. if (!indir)
  905. return -ENOMEM;
  906. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  907. if (ret)
  908. goto out;
  909. while (dev_size--)
  910. current_max = max(current_max, indir[dev_size]);
  911. *max = current_max;
  912. out:
  913. kfree(indir);
  914. return ret;
  915. }
  916. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  917. void __user *useraddr)
  918. {
  919. u32 user_size, dev_size;
  920. u32 *indir;
  921. int ret;
  922. if (!dev->ethtool_ops->get_rxfh_indir_size ||
  923. !dev->ethtool_ops->get_rxfh)
  924. return -EOPNOTSUPP;
  925. dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
  926. if (dev_size == 0)
  927. return -EOPNOTSUPP;
  928. if (copy_from_user(&user_size,
  929. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  930. sizeof(user_size)))
  931. return -EFAULT;
  932. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
  933. &dev_size, sizeof(dev_size)))
  934. return -EFAULT;
  935. /* If the user buffer size is 0, this is just a query for the
  936. * device table size. Otherwise, if it's smaller than the
  937. * device table size it's an error.
  938. */
  939. if (user_size < dev_size)
  940. return user_size == 0 ? 0 : -EINVAL;
  941. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  942. if (!indir)
  943. return -ENOMEM;
  944. ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
  945. if (ret)
  946. goto out;
  947. if (copy_to_user(useraddr +
  948. offsetof(struct ethtool_rxfh_indir, ring_index[0]),
  949. indir, dev_size * sizeof(indir[0])))
  950. ret = -EFAULT;
  951. out:
  952. kfree(indir);
  953. return ret;
  954. }
  955. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  956. void __user *useraddr)
  957. {
  958. struct ethtool_rxnfc rx_rings;
  959. u32 user_size, dev_size, i;
  960. u32 *indir;
  961. const struct ethtool_ops *ops = dev->ethtool_ops;
  962. int ret;
  963. u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
  964. if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
  965. !ops->get_rxnfc)
  966. return -EOPNOTSUPP;
  967. dev_size = ops->get_rxfh_indir_size(dev);
  968. if (dev_size == 0)
  969. return -EOPNOTSUPP;
  970. if (copy_from_user(&user_size,
  971. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  972. sizeof(user_size)))
  973. return -EFAULT;
  974. if (user_size != 0 && user_size != dev_size)
  975. return -EINVAL;
  976. indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
  977. if (!indir)
  978. return -ENOMEM;
  979. rx_rings.cmd = ETHTOOL_GRXRINGS;
  980. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  981. if (ret)
  982. goto out;
  983. if (user_size == 0) {
  984. for (i = 0; i < dev_size; i++)
  985. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  986. } else {
  987. ret = ethtool_copy_validate_indir(indir,
  988. useraddr + ringidx_offset,
  989. &rx_rings,
  990. dev_size);
  991. if (ret)
  992. goto out;
  993. }
  994. ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
  995. if (ret)
  996. goto out;
  997. /* indicate whether rxfh was set to default */
  998. if (user_size == 0)
  999. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1000. else
  1001. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1002. out:
  1003. kfree(indir);
  1004. return ret;
  1005. }
  1006. static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
  1007. void __user *useraddr)
  1008. {
  1009. int ret;
  1010. const struct ethtool_ops *ops = dev->ethtool_ops;
  1011. u32 user_indir_size, user_key_size;
  1012. u32 dev_indir_size = 0, dev_key_size = 0;
  1013. struct ethtool_rxfh rxfh;
  1014. u32 total_size;
  1015. u32 indir_bytes;
  1016. u32 *indir = NULL;
  1017. u8 dev_hfunc = 0;
  1018. u8 *hkey = NULL;
  1019. u8 *rss_config;
  1020. if (!ops->get_rxfh)
  1021. return -EOPNOTSUPP;
  1022. if (ops->get_rxfh_indir_size)
  1023. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1024. if (ops->get_rxfh_key_size)
  1025. dev_key_size = ops->get_rxfh_key_size(dev);
  1026. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1027. return -EFAULT;
  1028. user_indir_size = rxfh.indir_size;
  1029. user_key_size = rxfh.key_size;
  1030. /* Check that reserved fields are 0 for now */
  1031. if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
  1032. rxfh.rsvd8[2] || rxfh.rsvd32)
  1033. return -EINVAL;
  1034. rxfh.indir_size = dev_indir_size;
  1035. rxfh.key_size = dev_key_size;
  1036. if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
  1037. return -EFAULT;
  1038. if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
  1039. (user_key_size && (user_key_size != dev_key_size)))
  1040. return -EINVAL;
  1041. indir_bytes = user_indir_size * sizeof(indir[0]);
  1042. total_size = indir_bytes + user_key_size;
  1043. rss_config = kzalloc(total_size, GFP_USER);
  1044. if (!rss_config)
  1045. return -ENOMEM;
  1046. if (user_indir_size)
  1047. indir = (u32 *)rss_config;
  1048. if (user_key_size)
  1049. hkey = rss_config + indir_bytes;
  1050. ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
  1051. if (ret)
  1052. goto out;
  1053. if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
  1054. &dev_hfunc, sizeof(rxfh.hfunc))) {
  1055. ret = -EFAULT;
  1056. } else if (copy_to_user(useraddr +
  1057. offsetof(struct ethtool_rxfh, rss_config[0]),
  1058. rss_config, total_size)) {
  1059. ret = -EFAULT;
  1060. }
  1061. out:
  1062. kfree(rss_config);
  1063. return ret;
  1064. }
  1065. static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
  1066. void __user *useraddr)
  1067. {
  1068. int ret;
  1069. const struct ethtool_ops *ops = dev->ethtool_ops;
  1070. struct ethtool_rxnfc rx_rings;
  1071. struct ethtool_rxfh rxfh;
  1072. u32 dev_indir_size = 0, dev_key_size = 0, i;
  1073. u32 *indir = NULL, indir_bytes = 0;
  1074. u8 *hkey = NULL;
  1075. u8 *rss_config;
  1076. u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
  1077. if (!ops->get_rxnfc || !ops->set_rxfh)
  1078. return -EOPNOTSUPP;
  1079. if (ops->get_rxfh_indir_size)
  1080. dev_indir_size = ops->get_rxfh_indir_size(dev);
  1081. if (ops->get_rxfh_key_size)
  1082. dev_key_size = ops->get_rxfh_key_size(dev);
  1083. if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
  1084. return -EFAULT;
  1085. /* Check that reserved fields are 0 for now */
  1086. if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
  1087. rxfh.rsvd8[2] || rxfh.rsvd32)
  1088. return -EINVAL;
  1089. /* If either indir, hash key or function is valid, proceed further.
  1090. * Must request at least one change: indir size, hash key or function.
  1091. */
  1092. if ((rxfh.indir_size &&
  1093. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
  1094. rxfh.indir_size != dev_indir_size) ||
  1095. (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
  1096. (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
  1097. rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
  1098. return -EINVAL;
  1099. if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1100. indir_bytes = dev_indir_size * sizeof(indir[0]);
  1101. rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
  1102. if (!rss_config)
  1103. return -ENOMEM;
  1104. rx_rings.cmd = ETHTOOL_GRXRINGS;
  1105. ret = ops->get_rxnfc(dev, &rx_rings, NULL);
  1106. if (ret)
  1107. goto out;
  1108. /* rxfh.indir_size == 0 means reset the indir table to default.
  1109. * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
  1110. */
  1111. if (rxfh.indir_size &&
  1112. rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
  1113. indir = (u32 *)rss_config;
  1114. ret = ethtool_copy_validate_indir(indir,
  1115. useraddr + rss_cfg_offset,
  1116. &rx_rings,
  1117. rxfh.indir_size);
  1118. if (ret)
  1119. goto out;
  1120. } else if (rxfh.indir_size == 0) {
  1121. indir = (u32 *)rss_config;
  1122. for (i = 0; i < dev_indir_size; i++)
  1123. indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
  1124. }
  1125. if (rxfh.key_size) {
  1126. hkey = rss_config + indir_bytes;
  1127. if (copy_from_user(hkey,
  1128. useraddr + rss_cfg_offset + indir_bytes,
  1129. rxfh.key_size)) {
  1130. ret = -EFAULT;
  1131. goto out;
  1132. }
  1133. }
  1134. ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
  1135. if (ret)
  1136. goto out;
  1137. /* indicate whether rxfh was set to default */
  1138. if (rxfh.indir_size == 0)
  1139. dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
  1140. else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
  1141. dev->priv_flags |= IFF_RXFH_CONFIGURED;
  1142. out:
  1143. kfree(rss_config);
  1144. return ret;
  1145. }
  1146. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1147. {
  1148. struct ethtool_regs regs;
  1149. const struct ethtool_ops *ops = dev->ethtool_ops;
  1150. void *regbuf;
  1151. int reglen, ret;
  1152. if (!ops->get_regs || !ops->get_regs_len)
  1153. return -EOPNOTSUPP;
  1154. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1155. return -EFAULT;
  1156. reglen = ops->get_regs_len(dev);
  1157. if (regs.len > reglen)
  1158. regs.len = reglen;
  1159. regbuf = NULL;
  1160. if (reglen) {
  1161. regbuf = vzalloc(reglen);
  1162. if (!regbuf)
  1163. return -ENOMEM;
  1164. }
  1165. ops->get_regs(dev, &regs, regbuf);
  1166. ret = -EFAULT;
  1167. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1168. goto out;
  1169. useraddr += offsetof(struct ethtool_regs, data);
  1170. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  1171. goto out;
  1172. ret = 0;
  1173. out:
  1174. vfree(regbuf);
  1175. return ret;
  1176. }
  1177. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1178. {
  1179. struct ethtool_value reset;
  1180. int ret;
  1181. if (!dev->ethtool_ops->reset)
  1182. return -EOPNOTSUPP;
  1183. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1184. return -EFAULT;
  1185. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1186. if (ret)
  1187. return ret;
  1188. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1189. return -EFAULT;
  1190. return 0;
  1191. }
  1192. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1193. {
  1194. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1195. if (!dev->ethtool_ops->get_wol)
  1196. return -EOPNOTSUPP;
  1197. dev->ethtool_ops->get_wol(dev, &wol);
  1198. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1199. return -EFAULT;
  1200. return 0;
  1201. }
  1202. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1203. {
  1204. struct ethtool_wolinfo wol;
  1205. if (!dev->ethtool_ops->set_wol)
  1206. return -EOPNOTSUPP;
  1207. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1208. return -EFAULT;
  1209. return dev->ethtool_ops->set_wol(dev, &wol);
  1210. }
  1211. static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
  1212. {
  1213. struct ethtool_eee edata;
  1214. int rc;
  1215. if (!dev->ethtool_ops->get_eee)
  1216. return -EOPNOTSUPP;
  1217. memset(&edata, 0, sizeof(struct ethtool_eee));
  1218. edata.cmd = ETHTOOL_GEEE;
  1219. rc = dev->ethtool_ops->get_eee(dev, &edata);
  1220. if (rc)
  1221. return rc;
  1222. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1223. return -EFAULT;
  1224. return 0;
  1225. }
  1226. static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
  1227. {
  1228. struct ethtool_eee edata;
  1229. if (!dev->ethtool_ops->set_eee)
  1230. return -EOPNOTSUPP;
  1231. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1232. return -EFAULT;
  1233. return dev->ethtool_ops->set_eee(dev, &edata);
  1234. }
  1235. static int ethtool_nway_reset(struct net_device *dev)
  1236. {
  1237. if (!dev->ethtool_ops->nway_reset)
  1238. return -EOPNOTSUPP;
  1239. return dev->ethtool_ops->nway_reset(dev);
  1240. }
  1241. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1242. {
  1243. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1244. if (!dev->ethtool_ops->get_link)
  1245. return -EOPNOTSUPP;
  1246. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  1247. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1248. return -EFAULT;
  1249. return 0;
  1250. }
  1251. static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
  1252. int (*getter)(struct net_device *,
  1253. struct ethtool_eeprom *, u8 *),
  1254. u32 total_len)
  1255. {
  1256. struct ethtool_eeprom eeprom;
  1257. void __user *userbuf = useraddr + sizeof(eeprom);
  1258. u32 bytes_remaining;
  1259. u8 *data;
  1260. int ret = 0;
  1261. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1262. return -EFAULT;
  1263. /* Check for wrap and zero */
  1264. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1265. return -EINVAL;
  1266. /* Check for exceeding total eeprom len */
  1267. if (eeprom.offset + eeprom.len > total_len)
  1268. return -EINVAL;
  1269. data = kmalloc(PAGE_SIZE, GFP_USER);
  1270. if (!data)
  1271. return -ENOMEM;
  1272. bytes_remaining = eeprom.len;
  1273. while (bytes_remaining > 0) {
  1274. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1275. ret = getter(dev, &eeprom, data);
  1276. if (ret)
  1277. break;
  1278. if (copy_to_user(userbuf, data, eeprom.len)) {
  1279. ret = -EFAULT;
  1280. break;
  1281. }
  1282. userbuf += eeprom.len;
  1283. eeprom.offset += eeprom.len;
  1284. bytes_remaining -= eeprom.len;
  1285. }
  1286. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1287. eeprom.offset -= eeprom.len;
  1288. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1289. ret = -EFAULT;
  1290. kfree(data);
  1291. return ret;
  1292. }
  1293. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1294. {
  1295. const struct ethtool_ops *ops = dev->ethtool_ops;
  1296. if (!ops->get_eeprom || !ops->get_eeprom_len ||
  1297. !ops->get_eeprom_len(dev))
  1298. return -EOPNOTSUPP;
  1299. return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
  1300. ops->get_eeprom_len(dev));
  1301. }
  1302. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1303. {
  1304. struct ethtool_eeprom eeprom;
  1305. const struct ethtool_ops *ops = dev->ethtool_ops;
  1306. void __user *userbuf = useraddr + sizeof(eeprom);
  1307. u32 bytes_remaining;
  1308. u8 *data;
  1309. int ret = 0;
  1310. if (!ops->set_eeprom || !ops->get_eeprom_len ||
  1311. !ops->get_eeprom_len(dev))
  1312. return -EOPNOTSUPP;
  1313. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1314. return -EFAULT;
  1315. /* Check for wrap and zero */
  1316. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1317. return -EINVAL;
  1318. /* Check for exceeding total eeprom len */
  1319. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1320. return -EINVAL;
  1321. data = kmalloc(PAGE_SIZE, GFP_USER);
  1322. if (!data)
  1323. return -ENOMEM;
  1324. bytes_remaining = eeprom.len;
  1325. while (bytes_remaining > 0) {
  1326. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1327. if (copy_from_user(data, userbuf, eeprom.len)) {
  1328. ret = -EFAULT;
  1329. break;
  1330. }
  1331. ret = ops->set_eeprom(dev, &eeprom, data);
  1332. if (ret)
  1333. break;
  1334. userbuf += eeprom.len;
  1335. eeprom.offset += eeprom.len;
  1336. bytes_remaining -= eeprom.len;
  1337. }
  1338. kfree(data);
  1339. return ret;
  1340. }
  1341. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1342. void __user *useraddr)
  1343. {
  1344. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1345. if (!dev->ethtool_ops->get_coalesce)
  1346. return -EOPNOTSUPP;
  1347. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  1348. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1349. return -EFAULT;
  1350. return 0;
  1351. }
  1352. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1353. void __user *useraddr)
  1354. {
  1355. struct ethtool_coalesce coalesce;
  1356. if (!dev->ethtool_ops->set_coalesce)
  1357. return -EOPNOTSUPP;
  1358. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1359. return -EFAULT;
  1360. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  1361. }
  1362. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1363. {
  1364. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1365. if (!dev->ethtool_ops->get_ringparam)
  1366. return -EOPNOTSUPP;
  1367. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  1368. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1369. return -EFAULT;
  1370. return 0;
  1371. }
  1372. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1373. {
  1374. struct ethtool_ringparam ringparam;
  1375. if (!dev->ethtool_ops->set_ringparam)
  1376. return -EOPNOTSUPP;
  1377. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1378. return -EFAULT;
  1379. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  1380. }
  1381. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1382. void __user *useraddr)
  1383. {
  1384. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1385. if (!dev->ethtool_ops->get_channels)
  1386. return -EOPNOTSUPP;
  1387. dev->ethtool_ops->get_channels(dev, &channels);
  1388. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1389. return -EFAULT;
  1390. return 0;
  1391. }
  1392. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1393. void __user *useraddr)
  1394. {
  1395. struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
  1396. u32 max_rx_in_use = 0;
  1397. if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
  1398. return -EOPNOTSUPP;
  1399. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1400. return -EFAULT;
  1401. dev->ethtool_ops->get_channels(dev, &max);
  1402. /* ensure new counts are within the maximums */
  1403. if ((channels.rx_count > max.max_rx) ||
  1404. (channels.tx_count > max.max_tx) ||
  1405. (channels.combined_count > max.max_combined) ||
  1406. (channels.other_count > max.max_other))
  1407. return -EINVAL;
  1408. /* ensure the new Rx count fits within the configured Rx flow
  1409. * indirection table settings */
  1410. if (netif_is_rxfh_configured(dev) &&
  1411. !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
  1412. (channels.combined_count + channels.rx_count) <= max_rx_in_use)
  1413. return -EINVAL;
  1414. return dev->ethtool_ops->set_channels(dev, &channels);
  1415. }
  1416. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1417. {
  1418. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  1419. if (!dev->ethtool_ops->get_pauseparam)
  1420. return -EOPNOTSUPP;
  1421. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1422. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1423. return -EFAULT;
  1424. return 0;
  1425. }
  1426. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1427. {
  1428. struct ethtool_pauseparam pauseparam;
  1429. if (!dev->ethtool_ops->set_pauseparam)
  1430. return -EOPNOTSUPP;
  1431. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1432. return -EFAULT;
  1433. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1434. }
  1435. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1436. {
  1437. struct ethtool_test test;
  1438. const struct ethtool_ops *ops = dev->ethtool_ops;
  1439. u64 *data;
  1440. int ret, test_len;
  1441. if (!ops->self_test || !ops->get_sset_count)
  1442. return -EOPNOTSUPP;
  1443. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1444. if (test_len < 0)
  1445. return test_len;
  1446. WARN_ON(test_len == 0);
  1447. if (copy_from_user(&test, useraddr, sizeof(test)))
  1448. return -EFAULT;
  1449. test.len = test_len;
  1450. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1451. if (!data)
  1452. return -ENOMEM;
  1453. ops->self_test(dev, &test, data);
  1454. ret = -EFAULT;
  1455. if (copy_to_user(useraddr, &test, sizeof(test)))
  1456. goto out;
  1457. useraddr += sizeof(test);
  1458. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1459. goto out;
  1460. ret = 0;
  1461. out:
  1462. kfree(data);
  1463. return ret;
  1464. }
  1465. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1466. {
  1467. struct ethtool_gstrings gstrings;
  1468. u8 *data;
  1469. int ret;
  1470. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1471. return -EFAULT;
  1472. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1473. if (ret < 0)
  1474. return ret;
  1475. gstrings.len = ret;
  1476. data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
  1477. if (!data)
  1478. return -ENOMEM;
  1479. __ethtool_get_strings(dev, gstrings.string_set, data);
  1480. ret = -EFAULT;
  1481. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1482. goto out;
  1483. useraddr += sizeof(gstrings);
  1484. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1485. goto out;
  1486. ret = 0;
  1487. out:
  1488. kfree(data);
  1489. return ret;
  1490. }
  1491. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1492. {
  1493. struct ethtool_value id;
  1494. static bool busy;
  1495. const struct ethtool_ops *ops = dev->ethtool_ops;
  1496. int rc;
  1497. if (!ops->set_phys_id)
  1498. return -EOPNOTSUPP;
  1499. if (busy)
  1500. return -EBUSY;
  1501. if (copy_from_user(&id, useraddr, sizeof(id)))
  1502. return -EFAULT;
  1503. rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1504. if (rc < 0)
  1505. return rc;
  1506. /* Drop the RTNL lock while waiting, but prevent reentry or
  1507. * removal of the device.
  1508. */
  1509. busy = true;
  1510. dev_hold(dev);
  1511. rtnl_unlock();
  1512. if (rc == 0) {
  1513. /* Driver will handle this itself */
  1514. schedule_timeout_interruptible(
  1515. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1516. } else {
  1517. /* Driver expects to be called at twice the frequency in rc */
  1518. int n = rc * 2, i, interval = HZ / n;
  1519. /* Count down seconds */
  1520. do {
  1521. /* Count down iterations per second */
  1522. i = n;
  1523. do {
  1524. rtnl_lock();
  1525. rc = ops->set_phys_id(dev,
  1526. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1527. rtnl_unlock();
  1528. if (rc)
  1529. break;
  1530. schedule_timeout_interruptible(interval);
  1531. } while (!signal_pending(current) && --i != 0);
  1532. } while (!signal_pending(current) &&
  1533. (id.data == 0 || --id.data != 0));
  1534. }
  1535. rtnl_lock();
  1536. dev_put(dev);
  1537. busy = false;
  1538. (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1539. return rc;
  1540. }
  1541. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1542. {
  1543. struct ethtool_stats stats;
  1544. const struct ethtool_ops *ops = dev->ethtool_ops;
  1545. u64 *data;
  1546. int ret, n_stats;
  1547. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1548. return -EOPNOTSUPP;
  1549. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1550. if (n_stats < 0)
  1551. return n_stats;
  1552. WARN_ON(n_stats == 0);
  1553. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1554. return -EFAULT;
  1555. stats.n_stats = n_stats;
  1556. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  1557. if (!data)
  1558. return -ENOMEM;
  1559. ops->get_ethtool_stats(dev, &stats, data);
  1560. ret = -EFAULT;
  1561. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1562. goto out;
  1563. useraddr += sizeof(stats);
  1564. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1565. goto out;
  1566. ret = 0;
  1567. out:
  1568. kfree(data);
  1569. return ret;
  1570. }
  1571. static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
  1572. {
  1573. struct ethtool_stats stats;
  1574. struct phy_device *phydev = dev->phydev;
  1575. u64 *data;
  1576. int ret, n_stats;
  1577. if (!phydev)
  1578. return -EOPNOTSUPP;
  1579. n_stats = phy_get_sset_count(phydev);
  1580. if (n_stats < 0)
  1581. return n_stats;
  1582. WARN_ON(n_stats == 0);
  1583. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1584. return -EFAULT;
  1585. stats.n_stats = n_stats;
  1586. data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
  1587. if (!data)
  1588. return -ENOMEM;
  1589. mutex_lock(&phydev->lock);
  1590. phydev->drv->get_stats(phydev, &stats, data);
  1591. mutex_unlock(&phydev->lock);
  1592. ret = -EFAULT;
  1593. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1594. goto out;
  1595. useraddr += sizeof(stats);
  1596. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1597. goto out;
  1598. ret = 0;
  1599. out:
  1600. kfree(data);
  1601. return ret;
  1602. }
  1603. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1604. {
  1605. struct ethtool_perm_addr epaddr;
  1606. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1607. return -EFAULT;
  1608. if (epaddr.size < dev->addr_len)
  1609. return -ETOOSMALL;
  1610. epaddr.size = dev->addr_len;
  1611. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1612. return -EFAULT;
  1613. useraddr += sizeof(epaddr);
  1614. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1615. return -EFAULT;
  1616. return 0;
  1617. }
  1618. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1619. u32 cmd, u32 (*actor)(struct net_device *))
  1620. {
  1621. struct ethtool_value edata = { .cmd = cmd };
  1622. if (!actor)
  1623. return -EOPNOTSUPP;
  1624. edata.data = actor(dev);
  1625. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1626. return -EFAULT;
  1627. return 0;
  1628. }
  1629. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1630. void (*actor)(struct net_device *, u32))
  1631. {
  1632. struct ethtool_value edata;
  1633. if (!actor)
  1634. return -EOPNOTSUPP;
  1635. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1636. return -EFAULT;
  1637. actor(dev, edata.data);
  1638. return 0;
  1639. }
  1640. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1641. int (*actor)(struct net_device *, u32))
  1642. {
  1643. struct ethtool_value edata;
  1644. if (!actor)
  1645. return -EOPNOTSUPP;
  1646. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1647. return -EFAULT;
  1648. return actor(dev, edata.data);
  1649. }
  1650. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1651. char __user *useraddr)
  1652. {
  1653. struct ethtool_flash efl;
  1654. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1655. return -EFAULT;
  1656. if (!dev->ethtool_ops->flash_device)
  1657. return -EOPNOTSUPP;
  1658. efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
  1659. return dev->ethtool_ops->flash_device(dev, &efl);
  1660. }
  1661. static int ethtool_set_dump(struct net_device *dev,
  1662. void __user *useraddr)
  1663. {
  1664. struct ethtool_dump dump;
  1665. if (!dev->ethtool_ops->set_dump)
  1666. return -EOPNOTSUPP;
  1667. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1668. return -EFAULT;
  1669. return dev->ethtool_ops->set_dump(dev, &dump);
  1670. }
  1671. static int ethtool_get_dump_flag(struct net_device *dev,
  1672. void __user *useraddr)
  1673. {
  1674. int ret;
  1675. struct ethtool_dump dump;
  1676. const struct ethtool_ops *ops = dev->ethtool_ops;
  1677. if (!ops->get_dump_flag)
  1678. return -EOPNOTSUPP;
  1679. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1680. return -EFAULT;
  1681. ret = ops->get_dump_flag(dev, &dump);
  1682. if (ret)
  1683. return ret;
  1684. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1685. return -EFAULT;
  1686. return 0;
  1687. }
  1688. static int ethtool_get_dump_data(struct net_device *dev,
  1689. void __user *useraddr)
  1690. {
  1691. int ret;
  1692. __u32 len;
  1693. struct ethtool_dump dump, tmp;
  1694. const struct ethtool_ops *ops = dev->ethtool_ops;
  1695. void *data = NULL;
  1696. if (!ops->get_dump_data || !ops->get_dump_flag)
  1697. return -EOPNOTSUPP;
  1698. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1699. return -EFAULT;
  1700. memset(&tmp, 0, sizeof(tmp));
  1701. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1702. ret = ops->get_dump_flag(dev, &tmp);
  1703. if (ret)
  1704. return ret;
  1705. len = min(tmp.len, dump.len);
  1706. if (!len)
  1707. return -EFAULT;
  1708. /* Don't ever let the driver think there's more space available
  1709. * than it requested with .get_dump_flag().
  1710. */
  1711. dump.len = len;
  1712. /* Always allocate enough space to hold the whole thing so that the
  1713. * driver does not need to check the length and bother with partial
  1714. * dumping.
  1715. */
  1716. data = vzalloc(tmp.len);
  1717. if (!data)
  1718. return -ENOMEM;
  1719. ret = ops->get_dump_data(dev, &dump, data);
  1720. if (ret)
  1721. goto out;
  1722. /* There are two sane possibilities:
  1723. * 1. The driver's .get_dump_data() does not touch dump.len.
  1724. * 2. Or it may set dump.len to how much it really writes, which
  1725. * should be tmp.len (or len if it can do a partial dump).
  1726. * In any case respond to userspace with the actual length of data
  1727. * it's receiving.
  1728. */
  1729. WARN_ON(dump.len != len && dump.len != tmp.len);
  1730. dump.len = len;
  1731. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1732. ret = -EFAULT;
  1733. goto out;
  1734. }
  1735. useraddr += offsetof(struct ethtool_dump, data);
  1736. if (copy_to_user(useraddr, data, len))
  1737. ret = -EFAULT;
  1738. out:
  1739. vfree(data);
  1740. return ret;
  1741. }
  1742. static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
  1743. {
  1744. int err = 0;
  1745. struct ethtool_ts_info info;
  1746. const struct ethtool_ops *ops = dev->ethtool_ops;
  1747. struct phy_device *phydev = dev->phydev;
  1748. memset(&info, 0, sizeof(info));
  1749. info.cmd = ETHTOOL_GET_TS_INFO;
  1750. if (phydev && phydev->drv && phydev->drv->ts_info) {
  1751. err = phydev->drv->ts_info(phydev, &info);
  1752. } else if (ops->get_ts_info) {
  1753. err = ops->get_ts_info(dev, &info);
  1754. } else {
  1755. info.so_timestamping =
  1756. SOF_TIMESTAMPING_RX_SOFTWARE |
  1757. SOF_TIMESTAMPING_SOFTWARE;
  1758. info.phc_index = -1;
  1759. }
  1760. if (err)
  1761. return err;
  1762. if (copy_to_user(useraddr, &info, sizeof(info)))
  1763. err = -EFAULT;
  1764. return err;
  1765. }
  1766. static int __ethtool_get_module_info(struct net_device *dev,
  1767. struct ethtool_modinfo *modinfo)
  1768. {
  1769. const struct ethtool_ops *ops = dev->ethtool_ops;
  1770. struct phy_device *phydev = dev->phydev;
  1771. if (phydev && phydev->drv && phydev->drv->module_info)
  1772. return phydev->drv->module_info(phydev, modinfo);
  1773. if (ops->get_module_info)
  1774. return ops->get_module_info(dev, modinfo);
  1775. return -EOPNOTSUPP;
  1776. }
  1777. static int ethtool_get_module_info(struct net_device *dev,
  1778. void __user *useraddr)
  1779. {
  1780. int ret;
  1781. struct ethtool_modinfo modinfo;
  1782. if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
  1783. return -EFAULT;
  1784. ret = __ethtool_get_module_info(dev, &modinfo);
  1785. if (ret)
  1786. return ret;
  1787. if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
  1788. return -EFAULT;
  1789. return 0;
  1790. }
  1791. static int __ethtool_get_module_eeprom(struct net_device *dev,
  1792. struct ethtool_eeprom *ee, u8 *data)
  1793. {
  1794. const struct ethtool_ops *ops = dev->ethtool_ops;
  1795. struct phy_device *phydev = dev->phydev;
  1796. if (phydev && phydev->drv && phydev->drv->module_eeprom)
  1797. return phydev->drv->module_eeprom(phydev, ee, data);
  1798. if (ops->get_module_eeprom)
  1799. return ops->get_module_eeprom(dev, ee, data);
  1800. return -EOPNOTSUPP;
  1801. }
  1802. static int ethtool_get_module_eeprom(struct net_device *dev,
  1803. void __user *useraddr)
  1804. {
  1805. int ret;
  1806. struct ethtool_modinfo modinfo;
  1807. ret = __ethtool_get_module_info(dev, &modinfo);
  1808. if (ret)
  1809. return ret;
  1810. return ethtool_get_any_eeprom(dev, useraddr,
  1811. __ethtool_get_module_eeprom,
  1812. modinfo.eeprom_len);
  1813. }
  1814. static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
  1815. {
  1816. switch (tuna->id) {
  1817. case ETHTOOL_RX_COPYBREAK:
  1818. case ETHTOOL_TX_COPYBREAK:
  1819. if (tuna->len != sizeof(u32) ||
  1820. tuna->type_id != ETHTOOL_TUNABLE_U32)
  1821. return -EINVAL;
  1822. break;
  1823. default:
  1824. return -EINVAL;
  1825. }
  1826. return 0;
  1827. }
  1828. static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
  1829. {
  1830. int ret;
  1831. struct ethtool_tunable tuna;
  1832. const struct ethtool_ops *ops = dev->ethtool_ops;
  1833. void *data;
  1834. if (!ops->get_tunable)
  1835. return -EOPNOTSUPP;
  1836. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1837. return -EFAULT;
  1838. ret = ethtool_tunable_valid(&tuna);
  1839. if (ret)
  1840. return ret;
  1841. data = kmalloc(tuna.len, GFP_USER);
  1842. if (!data)
  1843. return -ENOMEM;
  1844. ret = ops->get_tunable(dev, &tuna, data);
  1845. if (ret)
  1846. goto out;
  1847. useraddr += sizeof(tuna);
  1848. ret = -EFAULT;
  1849. if (copy_to_user(useraddr, data, tuna.len))
  1850. goto out;
  1851. ret = 0;
  1852. out:
  1853. kfree(data);
  1854. return ret;
  1855. }
  1856. static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
  1857. {
  1858. int ret;
  1859. struct ethtool_tunable tuna;
  1860. const struct ethtool_ops *ops = dev->ethtool_ops;
  1861. void *data;
  1862. if (!ops->set_tunable)
  1863. return -EOPNOTSUPP;
  1864. if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
  1865. return -EFAULT;
  1866. ret = ethtool_tunable_valid(&tuna);
  1867. if (ret)
  1868. return ret;
  1869. data = kmalloc(tuna.len, GFP_USER);
  1870. if (!data)
  1871. return -ENOMEM;
  1872. useraddr += sizeof(tuna);
  1873. ret = -EFAULT;
  1874. if (copy_from_user(data, useraddr, tuna.len))
  1875. goto out;
  1876. ret = ops->set_tunable(dev, &tuna, data);
  1877. out:
  1878. kfree(data);
  1879. return ret;
  1880. }
  1881. static int ethtool_get_per_queue_coalesce(struct net_device *dev,
  1882. void __user *useraddr,
  1883. struct ethtool_per_queue_op *per_queue_opt)
  1884. {
  1885. u32 bit;
  1886. int ret;
  1887. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1888. if (!dev->ethtool_ops->get_per_queue_coalesce)
  1889. return -EOPNOTSUPP;
  1890. useraddr += sizeof(*per_queue_opt);
  1891. bitmap_from_u32array(queue_mask,
  1892. MAX_NUM_QUEUE,
  1893. per_queue_opt->queue_mask,
  1894. DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
  1895. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  1896. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1897. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
  1898. if (ret != 0)
  1899. return ret;
  1900. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1901. return -EFAULT;
  1902. useraddr += sizeof(coalesce);
  1903. }
  1904. return 0;
  1905. }
  1906. static int ethtool_set_per_queue_coalesce(struct net_device *dev,
  1907. void __user *useraddr,
  1908. struct ethtool_per_queue_op *per_queue_opt)
  1909. {
  1910. u32 bit;
  1911. int i, ret = 0;
  1912. int n_queue;
  1913. struct ethtool_coalesce *backup = NULL, *tmp = NULL;
  1914. DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
  1915. if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
  1916. (!dev->ethtool_ops->get_per_queue_coalesce))
  1917. return -EOPNOTSUPP;
  1918. useraddr += sizeof(*per_queue_opt);
  1919. bitmap_from_u32array(queue_mask,
  1920. MAX_NUM_QUEUE,
  1921. per_queue_opt->queue_mask,
  1922. DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
  1923. n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
  1924. tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
  1925. if (!backup)
  1926. return -ENOMEM;
  1927. for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
  1928. struct ethtool_coalesce coalesce;
  1929. ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
  1930. if (ret != 0)
  1931. goto roll_back;
  1932. tmp++;
  1933. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
  1934. ret = -EFAULT;
  1935. goto roll_back;
  1936. }
  1937. ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
  1938. if (ret != 0)
  1939. goto roll_back;
  1940. useraddr += sizeof(coalesce);
  1941. }
  1942. roll_back:
  1943. if (ret != 0) {
  1944. tmp = backup;
  1945. for_each_set_bit(i, queue_mask, bit) {
  1946. dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
  1947. tmp++;
  1948. }
  1949. }
  1950. kfree(backup);
  1951. return ret;
  1952. }
  1953. static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
  1954. {
  1955. struct ethtool_per_queue_op per_queue_opt;
  1956. if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
  1957. return -EFAULT;
  1958. switch (per_queue_opt.sub_command) {
  1959. case ETHTOOL_GCOALESCE:
  1960. return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  1961. case ETHTOOL_SCOALESCE:
  1962. return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
  1963. default:
  1964. return -EOPNOTSUPP;
  1965. };
  1966. }
  1967. /* The main entry point in this file. Called from net/core/dev_ioctl.c */
  1968. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1969. {
  1970. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1971. void __user *useraddr = ifr->ifr_data;
  1972. u32 ethcmd, sub_cmd;
  1973. int rc;
  1974. netdev_features_t old_features;
  1975. if (!dev || !netif_device_present(dev))
  1976. return -ENODEV;
  1977. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1978. return -EFAULT;
  1979. if (ethcmd == ETHTOOL_PERQUEUE) {
  1980. if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
  1981. return -EFAULT;
  1982. } else {
  1983. sub_cmd = ethcmd;
  1984. }
  1985. /* Allow some commands to be done by anyone */
  1986. switch (sub_cmd) {
  1987. case ETHTOOL_GSET:
  1988. case ETHTOOL_GDRVINFO:
  1989. case ETHTOOL_GMSGLVL:
  1990. case ETHTOOL_GLINK:
  1991. case ETHTOOL_GCOALESCE:
  1992. case ETHTOOL_GRINGPARAM:
  1993. case ETHTOOL_GPAUSEPARAM:
  1994. case ETHTOOL_GRXCSUM:
  1995. case ETHTOOL_GTXCSUM:
  1996. case ETHTOOL_GSG:
  1997. case ETHTOOL_GSSET_INFO:
  1998. case ETHTOOL_GSTRINGS:
  1999. case ETHTOOL_GSTATS:
  2000. case ETHTOOL_GPHYSTATS:
  2001. case ETHTOOL_GTSO:
  2002. case ETHTOOL_GPERMADDR:
  2003. case ETHTOOL_GUFO:
  2004. case ETHTOOL_GGSO:
  2005. case ETHTOOL_GGRO:
  2006. case ETHTOOL_GFLAGS:
  2007. case ETHTOOL_GPFLAGS:
  2008. case ETHTOOL_GRXFH:
  2009. case ETHTOOL_GRXRINGS:
  2010. case ETHTOOL_GRXCLSRLCNT:
  2011. case ETHTOOL_GRXCLSRULE:
  2012. case ETHTOOL_GRXCLSRLALL:
  2013. case ETHTOOL_GRXFHINDIR:
  2014. case ETHTOOL_GRSSH:
  2015. case ETHTOOL_GFEATURES:
  2016. case ETHTOOL_GCHANNELS:
  2017. case ETHTOOL_GET_TS_INFO:
  2018. case ETHTOOL_GEEE:
  2019. case ETHTOOL_GTUNABLE:
  2020. case ETHTOOL_GLINKSETTINGS:
  2021. break;
  2022. default:
  2023. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2024. return -EPERM;
  2025. }
  2026. if (dev->ethtool_ops->begin) {
  2027. rc = dev->ethtool_ops->begin(dev);
  2028. if (rc < 0)
  2029. return rc;
  2030. }
  2031. old_features = dev->features;
  2032. switch (ethcmd) {
  2033. case ETHTOOL_GSET:
  2034. rc = ethtool_get_settings(dev, useraddr);
  2035. break;
  2036. case ETHTOOL_SSET:
  2037. rc = ethtool_set_settings(dev, useraddr);
  2038. break;
  2039. case ETHTOOL_GDRVINFO:
  2040. rc = ethtool_get_drvinfo(dev, useraddr);
  2041. break;
  2042. case ETHTOOL_GREGS:
  2043. rc = ethtool_get_regs(dev, useraddr);
  2044. break;
  2045. case ETHTOOL_GWOL:
  2046. rc = ethtool_get_wol(dev, useraddr);
  2047. break;
  2048. case ETHTOOL_SWOL:
  2049. rc = ethtool_set_wol(dev, useraddr);
  2050. break;
  2051. case ETHTOOL_GMSGLVL:
  2052. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2053. dev->ethtool_ops->get_msglevel);
  2054. break;
  2055. case ETHTOOL_SMSGLVL:
  2056. rc = ethtool_set_value_void(dev, useraddr,
  2057. dev->ethtool_ops->set_msglevel);
  2058. break;
  2059. case ETHTOOL_GEEE:
  2060. rc = ethtool_get_eee(dev, useraddr);
  2061. break;
  2062. case ETHTOOL_SEEE:
  2063. rc = ethtool_set_eee(dev, useraddr);
  2064. break;
  2065. case ETHTOOL_NWAY_RST:
  2066. rc = ethtool_nway_reset(dev);
  2067. break;
  2068. case ETHTOOL_GLINK:
  2069. rc = ethtool_get_link(dev, useraddr);
  2070. break;
  2071. case ETHTOOL_GEEPROM:
  2072. rc = ethtool_get_eeprom(dev, useraddr);
  2073. break;
  2074. case ETHTOOL_SEEPROM:
  2075. rc = ethtool_set_eeprom(dev, useraddr);
  2076. break;
  2077. case ETHTOOL_GCOALESCE:
  2078. rc = ethtool_get_coalesce(dev, useraddr);
  2079. break;
  2080. case ETHTOOL_SCOALESCE:
  2081. rc = ethtool_set_coalesce(dev, useraddr);
  2082. break;
  2083. case ETHTOOL_GRINGPARAM:
  2084. rc = ethtool_get_ringparam(dev, useraddr);
  2085. break;
  2086. case ETHTOOL_SRINGPARAM:
  2087. rc = ethtool_set_ringparam(dev, useraddr);
  2088. break;
  2089. case ETHTOOL_GPAUSEPARAM:
  2090. rc = ethtool_get_pauseparam(dev, useraddr);
  2091. break;
  2092. case ETHTOOL_SPAUSEPARAM:
  2093. rc = ethtool_set_pauseparam(dev, useraddr);
  2094. break;
  2095. case ETHTOOL_TEST:
  2096. rc = ethtool_self_test(dev, useraddr);
  2097. break;
  2098. case ETHTOOL_GSTRINGS:
  2099. rc = ethtool_get_strings(dev, useraddr);
  2100. break;
  2101. case ETHTOOL_PHYS_ID:
  2102. rc = ethtool_phys_id(dev, useraddr);
  2103. break;
  2104. case ETHTOOL_GSTATS:
  2105. rc = ethtool_get_stats(dev, useraddr);
  2106. break;
  2107. case ETHTOOL_GPERMADDR:
  2108. rc = ethtool_get_perm_addr(dev, useraddr);
  2109. break;
  2110. case ETHTOOL_GFLAGS:
  2111. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2112. __ethtool_get_flags);
  2113. break;
  2114. case ETHTOOL_SFLAGS:
  2115. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  2116. break;
  2117. case ETHTOOL_GPFLAGS:
  2118. rc = ethtool_get_value(dev, useraddr, ethcmd,
  2119. dev->ethtool_ops->get_priv_flags);
  2120. break;
  2121. case ETHTOOL_SPFLAGS:
  2122. rc = ethtool_set_value(dev, useraddr,
  2123. dev->ethtool_ops->set_priv_flags);
  2124. break;
  2125. case ETHTOOL_GRXFH:
  2126. case ETHTOOL_GRXRINGS:
  2127. case ETHTOOL_GRXCLSRLCNT:
  2128. case ETHTOOL_GRXCLSRULE:
  2129. case ETHTOOL_GRXCLSRLALL:
  2130. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  2131. break;
  2132. case ETHTOOL_SRXFH:
  2133. case ETHTOOL_SRXCLSRLDEL:
  2134. case ETHTOOL_SRXCLSRLINS:
  2135. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  2136. break;
  2137. case ETHTOOL_FLASHDEV:
  2138. rc = ethtool_flash_device(dev, useraddr);
  2139. break;
  2140. case ETHTOOL_RESET:
  2141. rc = ethtool_reset(dev, useraddr);
  2142. break;
  2143. case ETHTOOL_GSSET_INFO:
  2144. rc = ethtool_get_sset_info(dev, useraddr);
  2145. break;
  2146. case ETHTOOL_GRXFHINDIR:
  2147. rc = ethtool_get_rxfh_indir(dev, useraddr);
  2148. break;
  2149. case ETHTOOL_SRXFHINDIR:
  2150. rc = ethtool_set_rxfh_indir(dev, useraddr);
  2151. break;
  2152. case ETHTOOL_GRSSH:
  2153. rc = ethtool_get_rxfh(dev, useraddr);
  2154. break;
  2155. case ETHTOOL_SRSSH:
  2156. rc = ethtool_set_rxfh(dev, useraddr);
  2157. break;
  2158. case ETHTOOL_GFEATURES:
  2159. rc = ethtool_get_features(dev, useraddr);
  2160. break;
  2161. case ETHTOOL_SFEATURES:
  2162. rc = ethtool_set_features(dev, useraddr);
  2163. break;
  2164. case ETHTOOL_GTXCSUM:
  2165. case ETHTOOL_GRXCSUM:
  2166. case ETHTOOL_GSG:
  2167. case ETHTOOL_GTSO:
  2168. case ETHTOOL_GUFO:
  2169. case ETHTOOL_GGSO:
  2170. case ETHTOOL_GGRO:
  2171. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  2172. break;
  2173. case ETHTOOL_STXCSUM:
  2174. case ETHTOOL_SRXCSUM:
  2175. case ETHTOOL_SSG:
  2176. case ETHTOOL_STSO:
  2177. case ETHTOOL_SUFO:
  2178. case ETHTOOL_SGSO:
  2179. case ETHTOOL_SGRO:
  2180. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  2181. break;
  2182. case ETHTOOL_GCHANNELS:
  2183. rc = ethtool_get_channels(dev, useraddr);
  2184. break;
  2185. case ETHTOOL_SCHANNELS:
  2186. rc = ethtool_set_channels(dev, useraddr);
  2187. break;
  2188. case ETHTOOL_SET_DUMP:
  2189. rc = ethtool_set_dump(dev, useraddr);
  2190. break;
  2191. case ETHTOOL_GET_DUMP_FLAG:
  2192. rc = ethtool_get_dump_flag(dev, useraddr);
  2193. break;
  2194. case ETHTOOL_GET_DUMP_DATA:
  2195. rc = ethtool_get_dump_data(dev, useraddr);
  2196. break;
  2197. case ETHTOOL_GET_TS_INFO:
  2198. rc = ethtool_get_ts_info(dev, useraddr);
  2199. break;
  2200. case ETHTOOL_GMODULEINFO:
  2201. rc = ethtool_get_module_info(dev, useraddr);
  2202. break;
  2203. case ETHTOOL_GMODULEEEPROM:
  2204. rc = ethtool_get_module_eeprom(dev, useraddr);
  2205. break;
  2206. case ETHTOOL_GTUNABLE:
  2207. rc = ethtool_get_tunable(dev, useraddr);
  2208. break;
  2209. case ETHTOOL_STUNABLE:
  2210. rc = ethtool_set_tunable(dev, useraddr);
  2211. break;
  2212. case ETHTOOL_GPHYSTATS:
  2213. rc = ethtool_get_phy_stats(dev, useraddr);
  2214. break;
  2215. case ETHTOOL_PERQUEUE:
  2216. rc = ethtool_set_per_queue(dev, useraddr);
  2217. break;
  2218. case ETHTOOL_GLINKSETTINGS:
  2219. rc = ethtool_get_link_ksettings(dev, useraddr);
  2220. break;
  2221. case ETHTOOL_SLINKSETTINGS:
  2222. rc = ethtool_set_link_ksettings(dev, useraddr);
  2223. break;
  2224. default:
  2225. rc = -EOPNOTSUPP;
  2226. }
  2227. if (dev->ethtool_ops->complete)
  2228. dev->ethtool_ops->complete(dev);
  2229. if (old_features != dev->features)
  2230. netdev_features_change(dev);
  2231. return rc;
  2232. }