ethtool.c 54 KB

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