ethtool.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  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/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/slab.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/sched.h>
  25. /*
  26. * Some useful ethtool_ops methods that're device independent.
  27. * If we find that all drivers want to do the same thing here,
  28. * we can turn these into dev_() function calls.
  29. */
  30. u32 ethtool_op_get_link(struct net_device *dev)
  31. {
  32. return netif_carrier_ok(dev) ? 1 : 0;
  33. }
  34. EXPORT_SYMBOL(ethtool_op_get_link);
  35. u32 ethtool_op_get_tx_csum(struct net_device *dev)
  36. {
  37. return (dev->features & NETIF_F_ALL_CSUM) != 0;
  38. }
  39. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  40. int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
  41. {
  42. if (data)
  43. dev->features |= NETIF_F_IP_CSUM;
  44. else
  45. dev->features &= ~NETIF_F_IP_CSUM;
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  49. int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
  50. {
  51. if (data)
  52. dev->features |= NETIF_F_HW_CSUM;
  53. else
  54. dev->features &= ~NETIF_F_HW_CSUM;
  55. return 0;
  56. }
  57. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  58. int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
  59. {
  60. if (data)
  61. dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  62. else
  63. dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  67. u32 ethtool_op_get_sg(struct net_device *dev)
  68. {
  69. return (dev->features & NETIF_F_SG) != 0;
  70. }
  71. EXPORT_SYMBOL(ethtool_op_get_sg);
  72. int ethtool_op_set_sg(struct net_device *dev, u32 data)
  73. {
  74. if (data)
  75. dev->features |= NETIF_F_SG;
  76. else
  77. dev->features &= ~NETIF_F_SG;
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(ethtool_op_set_sg);
  81. u32 ethtool_op_get_tso(struct net_device *dev)
  82. {
  83. return (dev->features & NETIF_F_TSO) != 0;
  84. }
  85. EXPORT_SYMBOL(ethtool_op_get_tso);
  86. int ethtool_op_set_tso(struct net_device *dev, u32 data)
  87. {
  88. if (data)
  89. dev->features |= NETIF_F_TSO;
  90. else
  91. dev->features &= ~NETIF_F_TSO;
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(ethtool_op_set_tso);
  95. u32 ethtool_op_get_ufo(struct net_device *dev)
  96. {
  97. return (dev->features & NETIF_F_UFO) != 0;
  98. }
  99. EXPORT_SYMBOL(ethtool_op_get_ufo);
  100. int ethtool_op_set_ufo(struct net_device *dev, u32 data)
  101. {
  102. if (data)
  103. dev->features |= NETIF_F_UFO;
  104. else
  105. dev->features &= ~NETIF_F_UFO;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(ethtool_op_set_ufo);
  109. /* the following list of flags are the same as their associated
  110. * NETIF_F_xxx values in include/linux/netdevice.h
  111. */
  112. static const u32 flags_dup_features =
  113. (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |
  114. ETH_FLAG_RXHASH);
  115. u32 ethtool_op_get_flags(struct net_device *dev)
  116. {
  117. /* in the future, this function will probably contain additional
  118. * handling for flags which are not so easily handled
  119. * by a simple masking operation
  120. */
  121. return dev->features & flags_dup_features;
  122. }
  123. EXPORT_SYMBOL(ethtool_op_get_flags);
  124. /* Check if device can enable (or disable) particular feature coded in "data"
  125. * argument. Flags "supported" describe features that can be toggled by device.
  126. * If feature can not be toggled, it state (enabled or disabled) must match
  127. * hardcoded device features state, otherwise flags are marked as invalid.
  128. */
  129. bool ethtool_invalid_flags(struct net_device *dev, u32 data, u32 supported)
  130. {
  131. u32 features = dev->features & flags_dup_features;
  132. /* "data" can contain only flags_dup_features bits,
  133. * see __ethtool_set_flags */
  134. return (features & ~supported) != (data & ~supported);
  135. }
  136. EXPORT_SYMBOL(ethtool_invalid_flags);
  137. int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
  138. {
  139. if (ethtool_invalid_flags(dev, data, supported))
  140. return -EINVAL;
  141. dev->features = ((dev->features & ~flags_dup_features) |
  142. (data & flags_dup_features));
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(ethtool_op_set_flags);
  146. void ethtool_ntuple_flush(struct net_device *dev)
  147. {
  148. struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
  149. list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
  150. list_del(&fsc->list);
  151. kfree(fsc);
  152. }
  153. dev->ethtool_ntuple_list.count = 0;
  154. }
  155. EXPORT_SYMBOL(ethtool_ntuple_flush);
  156. /* Handlers for each ethtool command */
  157. #define ETHTOOL_DEV_FEATURE_WORDS 1
  158. static void ethtool_get_features_compat(struct net_device *dev,
  159. struct ethtool_get_features_block *features)
  160. {
  161. if (!dev->ethtool_ops)
  162. return;
  163. /* getting RX checksum */
  164. if (dev->ethtool_ops->get_rx_csum)
  165. if (dev->ethtool_ops->get_rx_csum(dev))
  166. features[0].active |= NETIF_F_RXCSUM;
  167. /* mark legacy-changeable features */
  168. if (dev->ethtool_ops->set_sg)
  169. features[0].available |= NETIF_F_SG;
  170. if (dev->ethtool_ops->set_tx_csum)
  171. features[0].available |= NETIF_F_ALL_CSUM;
  172. if (dev->ethtool_ops->set_tso)
  173. features[0].available |= NETIF_F_ALL_TSO;
  174. if (dev->ethtool_ops->set_rx_csum)
  175. features[0].available |= NETIF_F_RXCSUM;
  176. if (dev->ethtool_ops->set_flags)
  177. features[0].available |= flags_dup_features;
  178. }
  179. static int ethtool_set_feature_compat(struct net_device *dev,
  180. int (*legacy_set)(struct net_device *, u32),
  181. struct ethtool_set_features_block *features, u32 mask)
  182. {
  183. u32 do_set;
  184. if (!legacy_set)
  185. return 0;
  186. if (!(features[0].valid & mask))
  187. return 0;
  188. features[0].valid &= ~mask;
  189. do_set = !!(features[0].requested & mask);
  190. if (legacy_set(dev, do_set) < 0)
  191. netdev_info(dev,
  192. "Legacy feature change (%s) failed for 0x%08x\n",
  193. do_set ? "set" : "clear", mask);
  194. return 1;
  195. }
  196. static int ethtool_set_features_compat(struct net_device *dev,
  197. struct ethtool_set_features_block *features)
  198. {
  199. int compat;
  200. if (!dev->ethtool_ops)
  201. return 0;
  202. compat = ethtool_set_feature_compat(dev, dev->ethtool_ops->set_sg,
  203. features, NETIF_F_SG);
  204. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tx_csum,
  205. features, NETIF_F_ALL_CSUM);
  206. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_tso,
  207. features, NETIF_F_ALL_TSO);
  208. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_rx_csum,
  209. features, NETIF_F_RXCSUM);
  210. compat |= ethtool_set_feature_compat(dev, dev->ethtool_ops->set_flags,
  211. features, flags_dup_features);
  212. return compat;
  213. }
  214. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  215. {
  216. struct ethtool_gfeatures cmd = {
  217. .cmd = ETHTOOL_GFEATURES,
  218. .size = ETHTOOL_DEV_FEATURE_WORDS,
  219. };
  220. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
  221. {
  222. .available = dev->hw_features,
  223. .requested = dev->wanted_features,
  224. .active = dev->features,
  225. .never_changed = NETIF_F_NEVER_CHANGE,
  226. },
  227. };
  228. u32 __user *sizeaddr;
  229. u32 copy_size;
  230. ethtool_get_features_compat(dev, features);
  231. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  232. if (get_user(copy_size, sizeaddr))
  233. return -EFAULT;
  234. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  235. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  236. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  237. return -EFAULT;
  238. useraddr += sizeof(cmd);
  239. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  240. return -EFAULT;
  241. return 0;
  242. }
  243. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  244. {
  245. struct ethtool_sfeatures cmd;
  246. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  247. int ret = 0;
  248. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  249. return -EFAULT;
  250. useraddr += sizeof(cmd);
  251. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  252. return -EINVAL;
  253. if (copy_from_user(features, useraddr, sizeof(features)))
  254. return -EFAULT;
  255. if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
  256. return -EINVAL;
  257. if (ethtool_set_features_compat(dev, features))
  258. ret |= ETHTOOL_F_COMPAT;
  259. if (features[0].valid & ~dev->hw_features) {
  260. features[0].valid &= dev->hw_features;
  261. ret |= ETHTOOL_F_UNSUPPORTED;
  262. }
  263. dev->wanted_features &= ~features[0].valid;
  264. dev->wanted_features |= features[0].valid & features[0].requested;
  265. __netdev_update_features(dev);
  266. if ((dev->wanted_features ^ dev->features) & features[0].valid)
  267. ret |= ETHTOOL_F_WISH;
  268. return ret;
  269. }
  270. static const char netdev_features_strings[ETHTOOL_DEV_FEATURE_WORDS * 32][ETH_GSTRING_LEN] = {
  271. /* NETIF_F_SG */ "tx-scatter-gather",
  272. /* NETIF_F_IP_CSUM */ "tx-checksum-ipv4",
  273. /* NETIF_F_NO_CSUM */ "tx-checksum-unneeded",
  274. /* NETIF_F_HW_CSUM */ "tx-checksum-ip-generic",
  275. /* NETIF_F_IPV6_CSUM */ "tx-checksum-ipv6",
  276. /* NETIF_F_HIGHDMA */ "highdma",
  277. /* NETIF_F_FRAGLIST */ "tx-scatter-gather-fraglist",
  278. /* NETIF_F_HW_VLAN_TX */ "tx-vlan-hw-insert",
  279. /* NETIF_F_HW_VLAN_RX */ "rx-vlan-hw-parse",
  280. /* NETIF_F_HW_VLAN_FILTER */ "rx-vlan-filter",
  281. /* NETIF_F_VLAN_CHALLENGED */ "vlan-challenged",
  282. /* NETIF_F_GSO */ "tx-generic-segmentation",
  283. /* NETIF_F_LLTX */ "tx-lockless",
  284. /* NETIF_F_NETNS_LOCAL */ "netns-local",
  285. /* NETIF_F_GRO */ "rx-gro",
  286. /* NETIF_F_LRO */ "rx-lro",
  287. /* NETIF_F_TSO */ "tx-tcp-segmentation",
  288. /* NETIF_F_UFO */ "tx-udp-fragmentation",
  289. /* NETIF_F_GSO_ROBUST */ "tx-gso-robust",
  290. /* NETIF_F_TSO_ECN */ "tx-tcp-ecn-segmentation",
  291. /* NETIF_F_TSO6 */ "tx-tcp6-segmentation",
  292. /* NETIF_F_FSO */ "tx-fcoe-segmentation",
  293. "",
  294. "",
  295. /* NETIF_F_FCOE_CRC */ "tx-checksum-fcoe-crc",
  296. /* NETIF_F_SCTP_CSUM */ "tx-checksum-sctp",
  297. /* NETIF_F_FCOE_MTU */ "fcoe-mtu",
  298. /* NETIF_F_NTUPLE */ "rx-ntuple-filter",
  299. /* NETIF_F_RXHASH */ "rx-hashing",
  300. /* NETIF_F_RXCSUM */ "rx-checksum",
  301. /* NETIF_F_NOCACHE_COPY */ "tx-nocache-copy",
  302. /* NETIF_F_LOOPBACK */ "loopback",
  303. };
  304. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  305. {
  306. const struct ethtool_ops *ops = dev->ethtool_ops;
  307. if (sset == ETH_SS_FEATURES)
  308. return ARRAY_SIZE(netdev_features_strings);
  309. if (ops && ops->get_sset_count && ops->get_strings)
  310. return ops->get_sset_count(dev, sset);
  311. else
  312. return -EOPNOTSUPP;
  313. }
  314. static void __ethtool_get_strings(struct net_device *dev,
  315. u32 stringset, u8 *data)
  316. {
  317. const struct ethtool_ops *ops = dev->ethtool_ops;
  318. if (stringset == ETH_SS_FEATURES)
  319. memcpy(data, netdev_features_strings,
  320. sizeof(netdev_features_strings));
  321. else
  322. /* ops->get_strings is valid because checked earlier */
  323. ops->get_strings(dev, stringset, data);
  324. }
  325. static u32 ethtool_get_feature_mask(u32 eth_cmd)
  326. {
  327. /* feature masks of legacy discrete ethtool ops */
  328. switch (eth_cmd) {
  329. case ETHTOOL_GTXCSUM:
  330. case ETHTOOL_STXCSUM:
  331. return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
  332. case ETHTOOL_GRXCSUM:
  333. case ETHTOOL_SRXCSUM:
  334. return NETIF_F_RXCSUM;
  335. case ETHTOOL_GSG:
  336. case ETHTOOL_SSG:
  337. return NETIF_F_SG;
  338. case ETHTOOL_GTSO:
  339. case ETHTOOL_STSO:
  340. return NETIF_F_ALL_TSO;
  341. case ETHTOOL_GUFO:
  342. case ETHTOOL_SUFO:
  343. return NETIF_F_UFO;
  344. case ETHTOOL_GGSO:
  345. case ETHTOOL_SGSO:
  346. return NETIF_F_GSO;
  347. case ETHTOOL_GGRO:
  348. case ETHTOOL_SGRO:
  349. return NETIF_F_GRO;
  350. default:
  351. BUG();
  352. }
  353. }
  354. static void *__ethtool_get_one_feature_actor(struct net_device *dev, u32 ethcmd)
  355. {
  356. const struct ethtool_ops *ops = dev->ethtool_ops;
  357. if (!ops)
  358. return NULL;
  359. switch (ethcmd) {
  360. case ETHTOOL_GTXCSUM:
  361. return ops->get_tx_csum;
  362. case ETHTOOL_GRXCSUM:
  363. return ops->get_rx_csum;
  364. case ETHTOOL_SSG:
  365. return ops->get_sg;
  366. case ETHTOOL_STSO:
  367. return ops->get_tso;
  368. case ETHTOOL_SUFO:
  369. return ops->get_ufo;
  370. default:
  371. return NULL;
  372. }
  373. }
  374. static u32 __ethtool_get_rx_csum_oldbug(struct net_device *dev)
  375. {
  376. return !!(dev->features & NETIF_F_ALL_CSUM);
  377. }
  378. static int ethtool_get_one_feature(struct net_device *dev,
  379. char __user *useraddr, u32 ethcmd)
  380. {
  381. u32 mask = ethtool_get_feature_mask(ethcmd);
  382. struct ethtool_value edata = {
  383. .cmd = ethcmd,
  384. .data = !!(dev->features & mask),
  385. };
  386. /* compatibility with discrete get_ ops */
  387. if (!(dev->hw_features & mask)) {
  388. u32 (*actor)(struct net_device *);
  389. actor = __ethtool_get_one_feature_actor(dev, ethcmd);
  390. /* bug compatibility with old get_rx_csum */
  391. if (ethcmd == ETHTOOL_GRXCSUM && !actor)
  392. actor = __ethtool_get_rx_csum_oldbug;
  393. if (actor)
  394. edata.data = actor(dev);
  395. }
  396. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  397. return -EFAULT;
  398. return 0;
  399. }
  400. static int __ethtool_set_tx_csum(struct net_device *dev, u32 data);
  401. static int __ethtool_set_rx_csum(struct net_device *dev, u32 data);
  402. static int __ethtool_set_sg(struct net_device *dev, u32 data);
  403. static int __ethtool_set_tso(struct net_device *dev, u32 data);
  404. static int __ethtool_set_ufo(struct net_device *dev, u32 data);
  405. static int ethtool_set_one_feature(struct net_device *dev,
  406. void __user *useraddr, u32 ethcmd)
  407. {
  408. struct ethtool_value edata;
  409. u32 mask;
  410. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  411. return -EFAULT;
  412. mask = ethtool_get_feature_mask(ethcmd);
  413. mask &= dev->hw_features;
  414. if (mask) {
  415. if (edata.data)
  416. dev->wanted_features |= mask;
  417. else
  418. dev->wanted_features &= ~mask;
  419. __netdev_update_features(dev);
  420. return 0;
  421. }
  422. /* Driver is not converted to ndo_fix_features or does not
  423. * support changing this offload. In the latter case it won't
  424. * have corresponding ethtool_ops field set.
  425. *
  426. * Following part is to be removed after all drivers advertise
  427. * their changeable features in netdev->hw_features and stop
  428. * using discrete offload setting ops.
  429. */
  430. switch (ethcmd) {
  431. case ETHTOOL_STXCSUM:
  432. return __ethtool_set_tx_csum(dev, edata.data);
  433. case ETHTOOL_SRXCSUM:
  434. return __ethtool_set_rx_csum(dev, edata.data);
  435. case ETHTOOL_SSG:
  436. return __ethtool_set_sg(dev, edata.data);
  437. case ETHTOOL_STSO:
  438. return __ethtool_set_tso(dev, edata.data);
  439. case ETHTOOL_SUFO:
  440. return __ethtool_set_ufo(dev, edata.data);
  441. default:
  442. return -EOPNOTSUPP;
  443. }
  444. }
  445. int __ethtool_set_flags(struct net_device *dev, u32 data)
  446. {
  447. u32 changed;
  448. if (data & ~flags_dup_features)
  449. return -EINVAL;
  450. /* legacy set_flags() op */
  451. if (dev->ethtool_ops->set_flags) {
  452. if (unlikely(dev->hw_features & flags_dup_features))
  453. netdev_warn(dev,
  454. "driver BUG: mixed hw_features and set_flags()\n");
  455. return dev->ethtool_ops->set_flags(dev, data);
  456. }
  457. /* allow changing only bits set in hw_features */
  458. changed = (data ^ dev->features) & flags_dup_features;
  459. if (changed & ~dev->hw_features)
  460. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  461. dev->wanted_features =
  462. (dev->wanted_features & ~changed) | (data & dev->hw_features);
  463. __netdev_update_features(dev);
  464. return 0;
  465. }
  466. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  467. {
  468. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  469. int err;
  470. if (!dev->ethtool_ops->get_settings)
  471. return -EOPNOTSUPP;
  472. err = dev->ethtool_ops->get_settings(dev, &cmd);
  473. if (err < 0)
  474. return err;
  475. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  476. return -EFAULT;
  477. return 0;
  478. }
  479. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  480. {
  481. struct ethtool_cmd cmd;
  482. if (!dev->ethtool_ops->set_settings)
  483. return -EOPNOTSUPP;
  484. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  485. return -EFAULT;
  486. return dev->ethtool_ops->set_settings(dev, &cmd);
  487. }
  488. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  489. void __user *useraddr)
  490. {
  491. struct ethtool_drvinfo info;
  492. const struct ethtool_ops *ops = dev->ethtool_ops;
  493. memset(&info, 0, sizeof(info));
  494. info.cmd = ETHTOOL_GDRVINFO;
  495. if (ops && ops->get_drvinfo) {
  496. ops->get_drvinfo(dev, &info);
  497. } else if (dev->dev.parent && dev->dev.parent->driver) {
  498. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  499. sizeof(info.bus_info));
  500. strlcpy(info.driver, dev->dev.parent->driver->name,
  501. sizeof(info.driver));
  502. } else {
  503. return -EOPNOTSUPP;
  504. }
  505. /*
  506. * this method of obtaining string set info is deprecated;
  507. * Use ETHTOOL_GSSET_INFO instead.
  508. */
  509. if (ops && ops->get_sset_count) {
  510. int rc;
  511. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  512. if (rc >= 0)
  513. info.testinfo_len = rc;
  514. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  515. if (rc >= 0)
  516. info.n_stats = rc;
  517. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  518. if (rc >= 0)
  519. info.n_priv_flags = rc;
  520. }
  521. if (ops && ops->get_regs_len)
  522. info.regdump_len = ops->get_regs_len(dev);
  523. if (ops && ops->get_eeprom_len)
  524. info.eedump_len = ops->get_eeprom_len(dev);
  525. if (copy_to_user(useraddr, &info, sizeof(info)))
  526. return -EFAULT;
  527. return 0;
  528. }
  529. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  530. void __user *useraddr)
  531. {
  532. struct ethtool_sset_info info;
  533. u64 sset_mask;
  534. int i, idx = 0, n_bits = 0, ret, rc;
  535. u32 *info_buf = NULL;
  536. if (copy_from_user(&info, useraddr, sizeof(info)))
  537. return -EFAULT;
  538. /* store copy of mask, because we zero struct later on */
  539. sset_mask = info.sset_mask;
  540. if (!sset_mask)
  541. return 0;
  542. /* calculate size of return buffer */
  543. n_bits = hweight64(sset_mask);
  544. memset(&info, 0, sizeof(info));
  545. info.cmd = ETHTOOL_GSSET_INFO;
  546. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  547. if (!info_buf)
  548. return -ENOMEM;
  549. /*
  550. * fill return buffer based on input bitmask and successful
  551. * get_sset_count return
  552. */
  553. for (i = 0; i < 64; i++) {
  554. if (!(sset_mask & (1ULL << i)))
  555. continue;
  556. rc = __ethtool_get_sset_count(dev, i);
  557. if (rc >= 0) {
  558. info.sset_mask |= (1ULL << i);
  559. info_buf[idx++] = rc;
  560. }
  561. }
  562. ret = -EFAULT;
  563. if (copy_to_user(useraddr, &info, sizeof(info)))
  564. goto out;
  565. useraddr += offsetof(struct ethtool_sset_info, data);
  566. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  567. goto out;
  568. ret = 0;
  569. out:
  570. kfree(info_buf);
  571. return ret;
  572. }
  573. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  574. u32 cmd, void __user *useraddr)
  575. {
  576. struct ethtool_rxnfc info;
  577. size_t info_size = sizeof(info);
  578. if (!dev->ethtool_ops->set_rxnfc)
  579. return -EOPNOTSUPP;
  580. /* struct ethtool_rxnfc was originally defined for
  581. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  582. * members. User-space might still be using that
  583. * definition. */
  584. if (cmd == ETHTOOL_SRXFH)
  585. info_size = (offsetof(struct ethtool_rxnfc, data) +
  586. sizeof(info.data));
  587. if (copy_from_user(&info, useraddr, info_size))
  588. return -EFAULT;
  589. return dev->ethtool_ops->set_rxnfc(dev, &info);
  590. }
  591. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  592. u32 cmd, void __user *useraddr)
  593. {
  594. struct ethtool_rxnfc info;
  595. size_t info_size = sizeof(info);
  596. const struct ethtool_ops *ops = dev->ethtool_ops;
  597. int ret;
  598. void *rule_buf = NULL;
  599. if (!ops->get_rxnfc)
  600. return -EOPNOTSUPP;
  601. /* struct ethtool_rxnfc was originally defined for
  602. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  603. * members. User-space might still be using that
  604. * definition. */
  605. if (cmd == ETHTOOL_GRXFH)
  606. info_size = (offsetof(struct ethtool_rxnfc, data) +
  607. sizeof(info.data));
  608. if (copy_from_user(&info, useraddr, info_size))
  609. return -EFAULT;
  610. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  611. if (info.rule_cnt > 0) {
  612. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  613. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  614. GFP_USER);
  615. if (!rule_buf)
  616. return -ENOMEM;
  617. }
  618. }
  619. ret = ops->get_rxnfc(dev, &info, rule_buf);
  620. if (ret < 0)
  621. goto err_out;
  622. ret = -EFAULT;
  623. if (copy_to_user(useraddr, &info, info_size))
  624. goto err_out;
  625. if (rule_buf) {
  626. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  627. if (copy_to_user(useraddr, rule_buf,
  628. info.rule_cnt * sizeof(u32)))
  629. goto err_out;
  630. }
  631. ret = 0;
  632. err_out:
  633. kfree(rule_buf);
  634. return ret;
  635. }
  636. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  637. void __user *useraddr)
  638. {
  639. struct ethtool_rxfh_indir *indir;
  640. u32 table_size;
  641. size_t full_size;
  642. int ret;
  643. if (!dev->ethtool_ops->get_rxfh_indir)
  644. return -EOPNOTSUPP;
  645. if (copy_from_user(&table_size,
  646. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  647. sizeof(table_size)))
  648. return -EFAULT;
  649. if (table_size >
  650. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  651. return -ENOMEM;
  652. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  653. indir = kzalloc(full_size, GFP_USER);
  654. if (!indir)
  655. return -ENOMEM;
  656. indir->cmd = ETHTOOL_GRXFHINDIR;
  657. indir->size = table_size;
  658. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  659. if (ret)
  660. goto out;
  661. if (copy_to_user(useraddr, indir, full_size))
  662. ret = -EFAULT;
  663. out:
  664. kfree(indir);
  665. return ret;
  666. }
  667. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  668. void __user *useraddr)
  669. {
  670. struct ethtool_rxfh_indir *indir;
  671. u32 table_size;
  672. size_t full_size;
  673. int ret;
  674. if (!dev->ethtool_ops->set_rxfh_indir)
  675. return -EOPNOTSUPP;
  676. if (copy_from_user(&table_size,
  677. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  678. sizeof(table_size)))
  679. return -EFAULT;
  680. if (table_size >
  681. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  682. return -ENOMEM;
  683. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  684. indir = kmalloc(full_size, GFP_USER);
  685. if (!indir)
  686. return -ENOMEM;
  687. if (copy_from_user(indir, useraddr, full_size)) {
  688. ret = -EFAULT;
  689. goto out;
  690. }
  691. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  692. out:
  693. kfree(indir);
  694. return ret;
  695. }
  696. static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
  697. struct ethtool_rx_ntuple_flow_spec *spec,
  698. struct ethtool_rx_ntuple_flow_spec_container *fsc)
  699. {
  700. /* don't add filters forever */
  701. if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
  702. /* free the container */
  703. kfree(fsc);
  704. return;
  705. }
  706. /* Copy the whole filter over */
  707. fsc->fs.flow_type = spec->flow_type;
  708. memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
  709. memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
  710. fsc->fs.vlan_tag = spec->vlan_tag;
  711. fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
  712. fsc->fs.data = spec->data;
  713. fsc->fs.data_mask = spec->data_mask;
  714. fsc->fs.action = spec->action;
  715. /* add to the list */
  716. list_add_tail_rcu(&fsc->list, &list->list);
  717. list->count++;
  718. }
  719. /*
  720. * ethtool does not (or did not) set masks for flow parameters that are
  721. * not specified, so if both value and mask are 0 then this must be
  722. * treated as equivalent to a mask with all bits set. Implement that
  723. * here rather than in drivers.
  724. */
  725. static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
  726. {
  727. struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
  728. struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
  729. if (fs->flow_type != TCP_V4_FLOW &&
  730. fs->flow_type != UDP_V4_FLOW &&
  731. fs->flow_type != SCTP_V4_FLOW)
  732. return;
  733. if (!(entry->ip4src | mask->ip4src))
  734. mask->ip4src = htonl(0xffffffff);
  735. if (!(entry->ip4dst | mask->ip4dst))
  736. mask->ip4dst = htonl(0xffffffff);
  737. if (!(entry->psrc | mask->psrc))
  738. mask->psrc = htons(0xffff);
  739. if (!(entry->pdst | mask->pdst))
  740. mask->pdst = htons(0xffff);
  741. if (!(entry->tos | mask->tos))
  742. mask->tos = 0xff;
  743. if (!(fs->vlan_tag | fs->vlan_tag_mask))
  744. fs->vlan_tag_mask = 0xffff;
  745. if (!(fs->data | fs->data_mask))
  746. fs->data_mask = 0xffffffffffffffffULL;
  747. }
  748. static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
  749. void __user *useraddr)
  750. {
  751. struct ethtool_rx_ntuple cmd;
  752. const struct ethtool_ops *ops = dev->ethtool_ops;
  753. struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
  754. int ret;
  755. if (!ops->set_rx_ntuple)
  756. return -EOPNOTSUPP;
  757. if (!(dev->features & NETIF_F_NTUPLE))
  758. return -EINVAL;
  759. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  760. return -EFAULT;
  761. rx_ntuple_fix_masks(&cmd.fs);
  762. /*
  763. * Cache filter in dev struct for GET operation only if
  764. * the underlying driver doesn't have its own GET operation, and
  765. * only if the filter was added successfully. First make sure we
  766. * can allocate the filter, then continue if successful.
  767. */
  768. if (!ops->get_rx_ntuple) {
  769. fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
  770. if (!fsc)
  771. return -ENOMEM;
  772. }
  773. ret = ops->set_rx_ntuple(dev, &cmd);
  774. if (ret) {
  775. kfree(fsc);
  776. return ret;
  777. }
  778. if (!ops->get_rx_ntuple)
  779. __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
  780. return ret;
  781. }
  782. static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
  783. {
  784. struct ethtool_gstrings gstrings;
  785. const struct ethtool_ops *ops = dev->ethtool_ops;
  786. struct ethtool_rx_ntuple_flow_spec_container *fsc;
  787. u8 *data;
  788. char *p;
  789. int ret, i, num_strings = 0;
  790. if (!ops->get_sset_count)
  791. return -EOPNOTSUPP;
  792. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  793. return -EFAULT;
  794. ret = ops->get_sset_count(dev, gstrings.string_set);
  795. if (ret < 0)
  796. return ret;
  797. gstrings.len = ret;
  798. data = kzalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  799. if (!data)
  800. return -ENOMEM;
  801. if (ops->get_rx_ntuple) {
  802. /* driver-specific filter grab */
  803. ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
  804. goto copy;
  805. }
  806. /* default ethtool filter grab */
  807. i = 0;
  808. p = (char *)data;
  809. list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
  810. sprintf(p, "Filter %d:\n", i);
  811. p += ETH_GSTRING_LEN;
  812. num_strings++;
  813. switch (fsc->fs.flow_type) {
  814. case TCP_V4_FLOW:
  815. sprintf(p, "\tFlow Type: TCP\n");
  816. p += ETH_GSTRING_LEN;
  817. num_strings++;
  818. break;
  819. case UDP_V4_FLOW:
  820. sprintf(p, "\tFlow Type: UDP\n");
  821. p += ETH_GSTRING_LEN;
  822. num_strings++;
  823. break;
  824. case SCTP_V4_FLOW:
  825. sprintf(p, "\tFlow Type: SCTP\n");
  826. p += ETH_GSTRING_LEN;
  827. num_strings++;
  828. break;
  829. case AH_ESP_V4_FLOW:
  830. sprintf(p, "\tFlow Type: AH ESP\n");
  831. p += ETH_GSTRING_LEN;
  832. num_strings++;
  833. break;
  834. case ESP_V4_FLOW:
  835. sprintf(p, "\tFlow Type: ESP\n");
  836. p += ETH_GSTRING_LEN;
  837. num_strings++;
  838. break;
  839. case IP_USER_FLOW:
  840. sprintf(p, "\tFlow Type: Raw IP\n");
  841. p += ETH_GSTRING_LEN;
  842. num_strings++;
  843. break;
  844. case IPV4_FLOW:
  845. sprintf(p, "\tFlow Type: IPv4\n");
  846. p += ETH_GSTRING_LEN;
  847. num_strings++;
  848. break;
  849. default:
  850. sprintf(p, "\tFlow Type: Unknown\n");
  851. p += ETH_GSTRING_LEN;
  852. num_strings++;
  853. goto unknown_filter;
  854. }
  855. /* now the rest of the filters */
  856. switch (fsc->fs.flow_type) {
  857. case TCP_V4_FLOW:
  858. case UDP_V4_FLOW:
  859. case SCTP_V4_FLOW:
  860. sprintf(p, "\tSrc IP addr: 0x%x\n",
  861. fsc->fs.h_u.tcp_ip4_spec.ip4src);
  862. p += ETH_GSTRING_LEN;
  863. num_strings++;
  864. sprintf(p, "\tSrc IP mask: 0x%x\n",
  865. fsc->fs.m_u.tcp_ip4_spec.ip4src);
  866. p += ETH_GSTRING_LEN;
  867. num_strings++;
  868. sprintf(p, "\tDest IP addr: 0x%x\n",
  869. fsc->fs.h_u.tcp_ip4_spec.ip4dst);
  870. p += ETH_GSTRING_LEN;
  871. num_strings++;
  872. sprintf(p, "\tDest IP mask: 0x%x\n",
  873. fsc->fs.m_u.tcp_ip4_spec.ip4dst);
  874. p += ETH_GSTRING_LEN;
  875. num_strings++;
  876. sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
  877. fsc->fs.h_u.tcp_ip4_spec.psrc,
  878. fsc->fs.m_u.tcp_ip4_spec.psrc);
  879. p += ETH_GSTRING_LEN;
  880. num_strings++;
  881. sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
  882. fsc->fs.h_u.tcp_ip4_spec.pdst,
  883. fsc->fs.m_u.tcp_ip4_spec.pdst);
  884. p += ETH_GSTRING_LEN;
  885. num_strings++;
  886. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  887. fsc->fs.h_u.tcp_ip4_spec.tos,
  888. fsc->fs.m_u.tcp_ip4_spec.tos);
  889. p += ETH_GSTRING_LEN;
  890. num_strings++;
  891. break;
  892. case AH_ESP_V4_FLOW:
  893. case ESP_V4_FLOW:
  894. sprintf(p, "\tSrc IP addr: 0x%x\n",
  895. fsc->fs.h_u.ah_ip4_spec.ip4src);
  896. p += ETH_GSTRING_LEN;
  897. num_strings++;
  898. sprintf(p, "\tSrc IP mask: 0x%x\n",
  899. fsc->fs.m_u.ah_ip4_spec.ip4src);
  900. p += ETH_GSTRING_LEN;
  901. num_strings++;
  902. sprintf(p, "\tDest IP addr: 0x%x\n",
  903. fsc->fs.h_u.ah_ip4_spec.ip4dst);
  904. p += ETH_GSTRING_LEN;
  905. num_strings++;
  906. sprintf(p, "\tDest IP mask: 0x%x\n",
  907. fsc->fs.m_u.ah_ip4_spec.ip4dst);
  908. p += ETH_GSTRING_LEN;
  909. num_strings++;
  910. sprintf(p, "\tSPI: %d, mask: 0x%x\n",
  911. fsc->fs.h_u.ah_ip4_spec.spi,
  912. fsc->fs.m_u.ah_ip4_spec.spi);
  913. p += ETH_GSTRING_LEN;
  914. num_strings++;
  915. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  916. fsc->fs.h_u.ah_ip4_spec.tos,
  917. fsc->fs.m_u.ah_ip4_spec.tos);
  918. p += ETH_GSTRING_LEN;
  919. num_strings++;
  920. break;
  921. case IP_USER_FLOW:
  922. sprintf(p, "\tSrc IP addr: 0x%x\n",
  923. fsc->fs.h_u.usr_ip4_spec.ip4src);
  924. p += ETH_GSTRING_LEN;
  925. num_strings++;
  926. sprintf(p, "\tSrc IP mask: 0x%x\n",
  927. fsc->fs.m_u.usr_ip4_spec.ip4src);
  928. p += ETH_GSTRING_LEN;
  929. num_strings++;
  930. sprintf(p, "\tDest IP addr: 0x%x\n",
  931. fsc->fs.h_u.usr_ip4_spec.ip4dst);
  932. p += ETH_GSTRING_LEN;
  933. num_strings++;
  934. sprintf(p, "\tDest IP mask: 0x%x\n",
  935. fsc->fs.m_u.usr_ip4_spec.ip4dst);
  936. p += ETH_GSTRING_LEN;
  937. num_strings++;
  938. break;
  939. case IPV4_FLOW:
  940. sprintf(p, "\tSrc IP addr: 0x%x\n",
  941. fsc->fs.h_u.usr_ip4_spec.ip4src);
  942. p += ETH_GSTRING_LEN;
  943. num_strings++;
  944. sprintf(p, "\tSrc IP mask: 0x%x\n",
  945. fsc->fs.m_u.usr_ip4_spec.ip4src);
  946. p += ETH_GSTRING_LEN;
  947. num_strings++;
  948. sprintf(p, "\tDest IP addr: 0x%x\n",
  949. fsc->fs.h_u.usr_ip4_spec.ip4dst);
  950. p += ETH_GSTRING_LEN;
  951. num_strings++;
  952. sprintf(p, "\tDest IP mask: 0x%x\n",
  953. fsc->fs.m_u.usr_ip4_spec.ip4dst);
  954. p += ETH_GSTRING_LEN;
  955. num_strings++;
  956. sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
  957. fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
  958. fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
  959. p += ETH_GSTRING_LEN;
  960. num_strings++;
  961. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  962. fsc->fs.h_u.usr_ip4_spec.tos,
  963. fsc->fs.m_u.usr_ip4_spec.tos);
  964. p += ETH_GSTRING_LEN;
  965. num_strings++;
  966. sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
  967. fsc->fs.h_u.usr_ip4_spec.ip_ver,
  968. fsc->fs.m_u.usr_ip4_spec.ip_ver);
  969. p += ETH_GSTRING_LEN;
  970. num_strings++;
  971. sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
  972. fsc->fs.h_u.usr_ip4_spec.proto,
  973. fsc->fs.m_u.usr_ip4_spec.proto);
  974. p += ETH_GSTRING_LEN;
  975. num_strings++;
  976. break;
  977. }
  978. sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
  979. fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
  980. p += ETH_GSTRING_LEN;
  981. num_strings++;
  982. sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
  983. p += ETH_GSTRING_LEN;
  984. num_strings++;
  985. sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
  986. p += ETH_GSTRING_LEN;
  987. num_strings++;
  988. if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
  989. sprintf(p, "\tAction: Drop\n");
  990. else
  991. sprintf(p, "\tAction: Direct to queue %d\n",
  992. fsc->fs.action);
  993. p += ETH_GSTRING_LEN;
  994. num_strings++;
  995. unknown_filter:
  996. i++;
  997. }
  998. copy:
  999. /* indicate to userspace how many strings we actually have */
  1000. gstrings.len = num_strings;
  1001. ret = -EFAULT;
  1002. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1003. goto out;
  1004. useraddr += sizeof(gstrings);
  1005. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1006. goto out;
  1007. ret = 0;
  1008. out:
  1009. kfree(data);
  1010. return ret;
  1011. }
  1012. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  1013. {
  1014. struct ethtool_regs regs;
  1015. const struct ethtool_ops *ops = dev->ethtool_ops;
  1016. void *regbuf;
  1017. int reglen, ret;
  1018. if (!ops->get_regs || !ops->get_regs_len)
  1019. return -EOPNOTSUPP;
  1020. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  1021. return -EFAULT;
  1022. reglen = ops->get_regs_len(dev);
  1023. if (regs.len > reglen)
  1024. regs.len = reglen;
  1025. regbuf = vzalloc(reglen);
  1026. if (!regbuf)
  1027. return -ENOMEM;
  1028. ops->get_regs(dev, &regs, regbuf);
  1029. ret = -EFAULT;
  1030. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  1031. goto out;
  1032. useraddr += offsetof(struct ethtool_regs, data);
  1033. if (copy_to_user(useraddr, regbuf, regs.len))
  1034. goto out;
  1035. ret = 0;
  1036. out:
  1037. vfree(regbuf);
  1038. return ret;
  1039. }
  1040. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  1041. {
  1042. struct ethtool_value reset;
  1043. int ret;
  1044. if (!dev->ethtool_ops->reset)
  1045. return -EOPNOTSUPP;
  1046. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  1047. return -EFAULT;
  1048. ret = dev->ethtool_ops->reset(dev, &reset.data);
  1049. if (ret)
  1050. return ret;
  1051. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  1052. return -EFAULT;
  1053. return 0;
  1054. }
  1055. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  1056. {
  1057. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1058. if (!dev->ethtool_ops->get_wol)
  1059. return -EOPNOTSUPP;
  1060. dev->ethtool_ops->get_wol(dev, &wol);
  1061. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  1062. return -EFAULT;
  1063. return 0;
  1064. }
  1065. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  1066. {
  1067. struct ethtool_wolinfo wol;
  1068. if (!dev->ethtool_ops->set_wol)
  1069. return -EOPNOTSUPP;
  1070. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  1071. return -EFAULT;
  1072. return dev->ethtool_ops->set_wol(dev, &wol);
  1073. }
  1074. static int ethtool_nway_reset(struct net_device *dev)
  1075. {
  1076. if (!dev->ethtool_ops->nway_reset)
  1077. return -EOPNOTSUPP;
  1078. return dev->ethtool_ops->nway_reset(dev);
  1079. }
  1080. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  1081. {
  1082. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  1083. if (!dev->ethtool_ops->get_link)
  1084. return -EOPNOTSUPP;
  1085. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  1086. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1087. return -EFAULT;
  1088. return 0;
  1089. }
  1090. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  1091. {
  1092. struct ethtool_eeprom eeprom;
  1093. const struct ethtool_ops *ops = dev->ethtool_ops;
  1094. void __user *userbuf = useraddr + sizeof(eeprom);
  1095. u32 bytes_remaining;
  1096. u8 *data;
  1097. int ret = 0;
  1098. if (!ops->get_eeprom || !ops->get_eeprom_len)
  1099. return -EOPNOTSUPP;
  1100. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1101. return -EFAULT;
  1102. /* Check for wrap and zero */
  1103. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1104. return -EINVAL;
  1105. /* Check for exceeding total eeprom len */
  1106. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1107. return -EINVAL;
  1108. data = kmalloc(PAGE_SIZE, GFP_USER);
  1109. if (!data)
  1110. return -ENOMEM;
  1111. bytes_remaining = eeprom.len;
  1112. while (bytes_remaining > 0) {
  1113. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1114. ret = ops->get_eeprom(dev, &eeprom, data);
  1115. if (ret)
  1116. break;
  1117. if (copy_to_user(userbuf, data, eeprom.len)) {
  1118. ret = -EFAULT;
  1119. break;
  1120. }
  1121. userbuf += eeprom.len;
  1122. eeprom.offset += eeprom.len;
  1123. bytes_remaining -= eeprom.len;
  1124. }
  1125. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  1126. eeprom.offset -= eeprom.len;
  1127. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  1128. ret = -EFAULT;
  1129. kfree(data);
  1130. return ret;
  1131. }
  1132. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  1133. {
  1134. struct ethtool_eeprom eeprom;
  1135. const struct ethtool_ops *ops = dev->ethtool_ops;
  1136. void __user *userbuf = useraddr + sizeof(eeprom);
  1137. u32 bytes_remaining;
  1138. u8 *data;
  1139. int ret = 0;
  1140. if (!ops->set_eeprom || !ops->get_eeprom_len)
  1141. return -EOPNOTSUPP;
  1142. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  1143. return -EFAULT;
  1144. /* Check for wrap and zero */
  1145. if (eeprom.offset + eeprom.len <= eeprom.offset)
  1146. return -EINVAL;
  1147. /* Check for exceeding total eeprom len */
  1148. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  1149. return -EINVAL;
  1150. data = kmalloc(PAGE_SIZE, GFP_USER);
  1151. if (!data)
  1152. return -ENOMEM;
  1153. bytes_remaining = eeprom.len;
  1154. while (bytes_remaining > 0) {
  1155. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  1156. if (copy_from_user(data, userbuf, eeprom.len)) {
  1157. ret = -EFAULT;
  1158. break;
  1159. }
  1160. ret = ops->set_eeprom(dev, &eeprom, data);
  1161. if (ret)
  1162. break;
  1163. userbuf += eeprom.len;
  1164. eeprom.offset += eeprom.len;
  1165. bytes_remaining -= eeprom.len;
  1166. }
  1167. kfree(data);
  1168. return ret;
  1169. }
  1170. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  1171. void __user *useraddr)
  1172. {
  1173. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  1174. if (!dev->ethtool_ops->get_coalesce)
  1175. return -EOPNOTSUPP;
  1176. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  1177. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  1178. return -EFAULT;
  1179. return 0;
  1180. }
  1181. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  1182. void __user *useraddr)
  1183. {
  1184. struct ethtool_coalesce coalesce;
  1185. if (!dev->ethtool_ops->set_coalesce)
  1186. return -EOPNOTSUPP;
  1187. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  1188. return -EFAULT;
  1189. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  1190. }
  1191. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  1192. {
  1193. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  1194. if (!dev->ethtool_ops->get_ringparam)
  1195. return -EOPNOTSUPP;
  1196. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  1197. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  1198. return -EFAULT;
  1199. return 0;
  1200. }
  1201. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  1202. {
  1203. struct ethtool_ringparam ringparam;
  1204. if (!dev->ethtool_ops->set_ringparam)
  1205. return -EOPNOTSUPP;
  1206. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  1207. return -EFAULT;
  1208. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  1209. }
  1210. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  1211. void __user *useraddr)
  1212. {
  1213. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  1214. if (!dev->ethtool_ops->get_channels)
  1215. return -EOPNOTSUPP;
  1216. dev->ethtool_ops->get_channels(dev, &channels);
  1217. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  1218. return -EFAULT;
  1219. return 0;
  1220. }
  1221. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  1222. void __user *useraddr)
  1223. {
  1224. struct ethtool_channels channels;
  1225. if (!dev->ethtool_ops->set_channels)
  1226. return -EOPNOTSUPP;
  1227. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  1228. return -EFAULT;
  1229. return dev->ethtool_ops->set_channels(dev, &channels);
  1230. }
  1231. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  1232. {
  1233. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  1234. if (!dev->ethtool_ops->get_pauseparam)
  1235. return -EOPNOTSUPP;
  1236. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  1237. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  1238. return -EFAULT;
  1239. return 0;
  1240. }
  1241. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  1242. {
  1243. struct ethtool_pauseparam pauseparam;
  1244. if (!dev->ethtool_ops->set_pauseparam)
  1245. return -EOPNOTSUPP;
  1246. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  1247. return -EFAULT;
  1248. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  1249. }
  1250. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  1251. {
  1252. int err;
  1253. if (!dev->ethtool_ops->set_sg)
  1254. return -EOPNOTSUPP;
  1255. if (data && !(dev->features & NETIF_F_ALL_CSUM))
  1256. return -EINVAL;
  1257. if (!data && dev->ethtool_ops->set_tso) {
  1258. err = dev->ethtool_ops->set_tso(dev, 0);
  1259. if (err)
  1260. return err;
  1261. }
  1262. if (!data && dev->ethtool_ops->set_ufo) {
  1263. err = dev->ethtool_ops->set_ufo(dev, 0);
  1264. if (err)
  1265. return err;
  1266. }
  1267. return dev->ethtool_ops->set_sg(dev, data);
  1268. }
  1269. static int __ethtool_set_tx_csum(struct net_device *dev, u32 data)
  1270. {
  1271. int err;
  1272. if (!dev->ethtool_ops->set_tx_csum)
  1273. return -EOPNOTSUPP;
  1274. if (!data && dev->ethtool_ops->set_sg) {
  1275. err = __ethtool_set_sg(dev, 0);
  1276. if (err)
  1277. return err;
  1278. }
  1279. return dev->ethtool_ops->set_tx_csum(dev, data);
  1280. }
  1281. static int __ethtool_set_rx_csum(struct net_device *dev, u32 data)
  1282. {
  1283. if (!dev->ethtool_ops->set_rx_csum)
  1284. return -EOPNOTSUPP;
  1285. if (!data)
  1286. dev->features &= ~NETIF_F_GRO;
  1287. return dev->ethtool_ops->set_rx_csum(dev, data);
  1288. }
  1289. static int __ethtool_set_tso(struct net_device *dev, u32 data)
  1290. {
  1291. if (!dev->ethtool_ops->set_tso)
  1292. return -EOPNOTSUPP;
  1293. if (data && !(dev->features & NETIF_F_SG))
  1294. return -EINVAL;
  1295. return dev->ethtool_ops->set_tso(dev, data);
  1296. }
  1297. static int __ethtool_set_ufo(struct net_device *dev, u32 data)
  1298. {
  1299. if (!dev->ethtool_ops->set_ufo)
  1300. return -EOPNOTSUPP;
  1301. if (data && !(dev->features & NETIF_F_SG))
  1302. return -EINVAL;
  1303. if (data && !((dev->features & NETIF_F_GEN_CSUM) ||
  1304. (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
  1305. == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)))
  1306. return -EINVAL;
  1307. return dev->ethtool_ops->set_ufo(dev, data);
  1308. }
  1309. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  1310. {
  1311. struct ethtool_test test;
  1312. const struct ethtool_ops *ops = dev->ethtool_ops;
  1313. u64 *data;
  1314. int ret, test_len;
  1315. if (!ops->self_test || !ops->get_sset_count)
  1316. return -EOPNOTSUPP;
  1317. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  1318. if (test_len < 0)
  1319. return test_len;
  1320. WARN_ON(test_len == 0);
  1321. if (copy_from_user(&test, useraddr, sizeof(test)))
  1322. return -EFAULT;
  1323. test.len = test_len;
  1324. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1325. if (!data)
  1326. return -ENOMEM;
  1327. ops->self_test(dev, &test, data);
  1328. ret = -EFAULT;
  1329. if (copy_to_user(useraddr, &test, sizeof(test)))
  1330. goto out;
  1331. useraddr += sizeof(test);
  1332. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1333. goto out;
  1334. ret = 0;
  1335. out:
  1336. kfree(data);
  1337. return ret;
  1338. }
  1339. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1340. {
  1341. struct ethtool_gstrings gstrings;
  1342. u8 *data;
  1343. int ret;
  1344. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1345. return -EFAULT;
  1346. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  1347. if (ret < 0)
  1348. return ret;
  1349. gstrings.len = ret;
  1350. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  1351. if (!data)
  1352. return -ENOMEM;
  1353. __ethtool_get_strings(dev, gstrings.string_set, data);
  1354. ret = -EFAULT;
  1355. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1356. goto out;
  1357. useraddr += sizeof(gstrings);
  1358. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1359. goto out;
  1360. ret = 0;
  1361. out:
  1362. kfree(data);
  1363. return ret;
  1364. }
  1365. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1366. {
  1367. struct ethtool_value id;
  1368. static bool busy;
  1369. int rc;
  1370. if (!dev->ethtool_ops->set_phys_id)
  1371. return -EOPNOTSUPP;
  1372. if (busy)
  1373. return -EBUSY;
  1374. if (copy_from_user(&id, useraddr, sizeof(id)))
  1375. return -EFAULT;
  1376. rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  1377. if (rc < 0)
  1378. return rc;
  1379. /* Drop the RTNL lock while waiting, but prevent reentry or
  1380. * removal of the device.
  1381. */
  1382. busy = true;
  1383. dev_hold(dev);
  1384. rtnl_unlock();
  1385. if (rc == 0) {
  1386. /* Driver will handle this itself */
  1387. schedule_timeout_interruptible(
  1388. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  1389. } else {
  1390. /* Driver expects to be called at twice the frequency in rc */
  1391. int n = rc * 2, i, interval = HZ / n;
  1392. /* Count down seconds */
  1393. do {
  1394. /* Count down iterations per second */
  1395. i = n;
  1396. do {
  1397. rtnl_lock();
  1398. rc = dev->ethtool_ops->set_phys_id(dev,
  1399. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  1400. rtnl_unlock();
  1401. if (rc)
  1402. break;
  1403. schedule_timeout_interruptible(interval);
  1404. } while (!signal_pending(current) && --i != 0);
  1405. } while (!signal_pending(current) &&
  1406. (id.data == 0 || --id.data != 0));
  1407. }
  1408. rtnl_lock();
  1409. dev_put(dev);
  1410. busy = false;
  1411. (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  1412. return rc;
  1413. }
  1414. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1415. {
  1416. struct ethtool_stats stats;
  1417. const struct ethtool_ops *ops = dev->ethtool_ops;
  1418. u64 *data;
  1419. int ret, n_stats;
  1420. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1421. return -EOPNOTSUPP;
  1422. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1423. if (n_stats < 0)
  1424. return n_stats;
  1425. WARN_ON(n_stats == 0);
  1426. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1427. return -EFAULT;
  1428. stats.n_stats = n_stats;
  1429. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  1430. if (!data)
  1431. return -ENOMEM;
  1432. ops->get_ethtool_stats(dev, &stats, data);
  1433. ret = -EFAULT;
  1434. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1435. goto out;
  1436. useraddr += sizeof(stats);
  1437. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1438. goto out;
  1439. ret = 0;
  1440. out:
  1441. kfree(data);
  1442. return ret;
  1443. }
  1444. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1445. {
  1446. struct ethtool_perm_addr epaddr;
  1447. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1448. return -EFAULT;
  1449. if (epaddr.size < dev->addr_len)
  1450. return -ETOOSMALL;
  1451. epaddr.size = dev->addr_len;
  1452. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1453. return -EFAULT;
  1454. useraddr += sizeof(epaddr);
  1455. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1456. return -EFAULT;
  1457. return 0;
  1458. }
  1459. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1460. u32 cmd, u32 (*actor)(struct net_device *))
  1461. {
  1462. struct ethtool_value edata = { .cmd = cmd };
  1463. if (!actor)
  1464. return -EOPNOTSUPP;
  1465. edata.data = actor(dev);
  1466. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1467. return -EFAULT;
  1468. return 0;
  1469. }
  1470. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1471. void (*actor)(struct net_device *, u32))
  1472. {
  1473. struct ethtool_value edata;
  1474. if (!actor)
  1475. return -EOPNOTSUPP;
  1476. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1477. return -EFAULT;
  1478. actor(dev, edata.data);
  1479. return 0;
  1480. }
  1481. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1482. int (*actor)(struct net_device *, u32))
  1483. {
  1484. struct ethtool_value edata;
  1485. if (!actor)
  1486. return -EOPNOTSUPP;
  1487. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1488. return -EFAULT;
  1489. return actor(dev, edata.data);
  1490. }
  1491. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1492. char __user *useraddr)
  1493. {
  1494. struct ethtool_flash efl;
  1495. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1496. return -EFAULT;
  1497. if (!dev->ethtool_ops->flash_device)
  1498. return -EOPNOTSUPP;
  1499. return dev->ethtool_ops->flash_device(dev, &efl);
  1500. }
  1501. static int ethtool_set_dump(struct net_device *dev,
  1502. void __user *useraddr)
  1503. {
  1504. struct ethtool_dump dump;
  1505. if (!dev->ethtool_ops->set_dump)
  1506. return -EOPNOTSUPP;
  1507. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1508. return -EFAULT;
  1509. return dev->ethtool_ops->set_dump(dev, &dump);
  1510. }
  1511. static int ethtool_get_dump_flag(struct net_device *dev,
  1512. void __user *useraddr)
  1513. {
  1514. int ret;
  1515. struct ethtool_dump dump;
  1516. const struct ethtool_ops *ops = dev->ethtool_ops;
  1517. if (!dev->ethtool_ops->get_dump_flag)
  1518. return -EOPNOTSUPP;
  1519. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1520. return -EFAULT;
  1521. ret = ops->get_dump_flag(dev, &dump);
  1522. if (ret)
  1523. return ret;
  1524. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  1525. return -EFAULT;
  1526. return 0;
  1527. }
  1528. static int ethtool_get_dump_data(struct net_device *dev,
  1529. void __user *useraddr)
  1530. {
  1531. int ret;
  1532. __u32 len;
  1533. struct ethtool_dump dump, tmp;
  1534. const struct ethtool_ops *ops = dev->ethtool_ops;
  1535. void *data = NULL;
  1536. if (!dev->ethtool_ops->get_dump_data ||
  1537. !dev->ethtool_ops->get_dump_flag)
  1538. return -EOPNOTSUPP;
  1539. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  1540. return -EFAULT;
  1541. memset(&tmp, 0, sizeof(tmp));
  1542. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  1543. ret = ops->get_dump_flag(dev, &tmp);
  1544. if (ret)
  1545. return ret;
  1546. len = (tmp.len > dump.len) ? dump.len : tmp.len;
  1547. if (!len)
  1548. return -EFAULT;
  1549. data = vzalloc(tmp.len);
  1550. if (!data)
  1551. return -ENOMEM;
  1552. ret = ops->get_dump_data(dev, &dump, data);
  1553. if (ret)
  1554. goto out;
  1555. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1556. ret = -EFAULT;
  1557. goto out;
  1558. }
  1559. useraddr += offsetof(struct ethtool_dump, data);
  1560. if (copy_to_user(useraddr, data, len))
  1561. ret = -EFAULT;
  1562. out:
  1563. vfree(data);
  1564. return ret;
  1565. }
  1566. /* The main entry point in this file. Called from net/core/dev.c */
  1567. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1568. {
  1569. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1570. void __user *useraddr = ifr->ifr_data;
  1571. u32 ethcmd;
  1572. int rc;
  1573. u32 old_features;
  1574. if (!dev || !netif_device_present(dev))
  1575. return -ENODEV;
  1576. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1577. return -EFAULT;
  1578. if (!dev->ethtool_ops) {
  1579. /* ETHTOOL_GDRVINFO does not require any driver support.
  1580. * It is also unprivileged and does not change anything,
  1581. * so we can take a shortcut to it. */
  1582. if (ethcmd == ETHTOOL_GDRVINFO)
  1583. return ethtool_get_drvinfo(dev, useraddr);
  1584. else
  1585. return -EOPNOTSUPP;
  1586. }
  1587. /* Allow some commands to be done by anyone */
  1588. switch (ethcmd) {
  1589. case ETHTOOL_GSET:
  1590. case ETHTOOL_GDRVINFO:
  1591. case ETHTOOL_GMSGLVL:
  1592. case ETHTOOL_GCOALESCE:
  1593. case ETHTOOL_GRINGPARAM:
  1594. case ETHTOOL_GPAUSEPARAM:
  1595. case ETHTOOL_GRXCSUM:
  1596. case ETHTOOL_GTXCSUM:
  1597. case ETHTOOL_GSG:
  1598. case ETHTOOL_GSTRINGS:
  1599. case ETHTOOL_GTSO:
  1600. case ETHTOOL_GPERMADDR:
  1601. case ETHTOOL_GUFO:
  1602. case ETHTOOL_GGSO:
  1603. case ETHTOOL_GGRO:
  1604. case ETHTOOL_GFLAGS:
  1605. case ETHTOOL_GPFLAGS:
  1606. case ETHTOOL_GRXFH:
  1607. case ETHTOOL_GRXRINGS:
  1608. case ETHTOOL_GRXCLSRLCNT:
  1609. case ETHTOOL_GRXCLSRULE:
  1610. case ETHTOOL_GRXCLSRLALL:
  1611. case ETHTOOL_GFEATURES:
  1612. break;
  1613. default:
  1614. if (!capable(CAP_NET_ADMIN))
  1615. return -EPERM;
  1616. }
  1617. if (dev->ethtool_ops->begin) {
  1618. rc = dev->ethtool_ops->begin(dev);
  1619. if (rc < 0)
  1620. return rc;
  1621. }
  1622. old_features = dev->features;
  1623. switch (ethcmd) {
  1624. case ETHTOOL_GSET:
  1625. rc = ethtool_get_settings(dev, useraddr);
  1626. break;
  1627. case ETHTOOL_SSET:
  1628. rc = ethtool_set_settings(dev, useraddr);
  1629. break;
  1630. case ETHTOOL_GDRVINFO:
  1631. rc = ethtool_get_drvinfo(dev, useraddr);
  1632. break;
  1633. case ETHTOOL_GREGS:
  1634. rc = ethtool_get_regs(dev, useraddr);
  1635. break;
  1636. case ETHTOOL_GWOL:
  1637. rc = ethtool_get_wol(dev, useraddr);
  1638. break;
  1639. case ETHTOOL_SWOL:
  1640. rc = ethtool_set_wol(dev, useraddr);
  1641. break;
  1642. case ETHTOOL_GMSGLVL:
  1643. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1644. dev->ethtool_ops->get_msglevel);
  1645. break;
  1646. case ETHTOOL_SMSGLVL:
  1647. rc = ethtool_set_value_void(dev, useraddr,
  1648. dev->ethtool_ops->set_msglevel);
  1649. break;
  1650. case ETHTOOL_NWAY_RST:
  1651. rc = ethtool_nway_reset(dev);
  1652. break;
  1653. case ETHTOOL_GLINK:
  1654. rc = ethtool_get_link(dev, useraddr);
  1655. break;
  1656. case ETHTOOL_GEEPROM:
  1657. rc = ethtool_get_eeprom(dev, useraddr);
  1658. break;
  1659. case ETHTOOL_SEEPROM:
  1660. rc = ethtool_set_eeprom(dev, useraddr);
  1661. break;
  1662. case ETHTOOL_GCOALESCE:
  1663. rc = ethtool_get_coalesce(dev, useraddr);
  1664. break;
  1665. case ETHTOOL_SCOALESCE:
  1666. rc = ethtool_set_coalesce(dev, useraddr);
  1667. break;
  1668. case ETHTOOL_GRINGPARAM:
  1669. rc = ethtool_get_ringparam(dev, useraddr);
  1670. break;
  1671. case ETHTOOL_SRINGPARAM:
  1672. rc = ethtool_set_ringparam(dev, useraddr);
  1673. break;
  1674. case ETHTOOL_GPAUSEPARAM:
  1675. rc = ethtool_get_pauseparam(dev, useraddr);
  1676. break;
  1677. case ETHTOOL_SPAUSEPARAM:
  1678. rc = ethtool_set_pauseparam(dev, useraddr);
  1679. break;
  1680. case ETHTOOL_TEST:
  1681. rc = ethtool_self_test(dev, useraddr);
  1682. break;
  1683. case ETHTOOL_GSTRINGS:
  1684. rc = ethtool_get_strings(dev, useraddr);
  1685. break;
  1686. case ETHTOOL_PHYS_ID:
  1687. rc = ethtool_phys_id(dev, useraddr);
  1688. break;
  1689. case ETHTOOL_GSTATS:
  1690. rc = ethtool_get_stats(dev, useraddr);
  1691. break;
  1692. case ETHTOOL_GPERMADDR:
  1693. rc = ethtool_get_perm_addr(dev, useraddr);
  1694. break;
  1695. case ETHTOOL_GFLAGS:
  1696. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1697. (dev->ethtool_ops->get_flags ?
  1698. dev->ethtool_ops->get_flags :
  1699. ethtool_op_get_flags));
  1700. break;
  1701. case ETHTOOL_SFLAGS:
  1702. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1703. break;
  1704. case ETHTOOL_GPFLAGS:
  1705. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1706. dev->ethtool_ops->get_priv_flags);
  1707. break;
  1708. case ETHTOOL_SPFLAGS:
  1709. rc = ethtool_set_value(dev, useraddr,
  1710. dev->ethtool_ops->set_priv_flags);
  1711. break;
  1712. case ETHTOOL_GRXFH:
  1713. case ETHTOOL_GRXRINGS:
  1714. case ETHTOOL_GRXCLSRLCNT:
  1715. case ETHTOOL_GRXCLSRULE:
  1716. case ETHTOOL_GRXCLSRLALL:
  1717. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1718. break;
  1719. case ETHTOOL_SRXFH:
  1720. case ETHTOOL_SRXCLSRLDEL:
  1721. case ETHTOOL_SRXCLSRLINS:
  1722. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1723. break;
  1724. case ETHTOOL_FLASHDEV:
  1725. rc = ethtool_flash_device(dev, useraddr);
  1726. break;
  1727. case ETHTOOL_RESET:
  1728. rc = ethtool_reset(dev, useraddr);
  1729. break;
  1730. case ETHTOOL_SRXNTUPLE:
  1731. rc = ethtool_set_rx_ntuple(dev, useraddr);
  1732. break;
  1733. case ETHTOOL_GRXNTUPLE:
  1734. rc = ethtool_get_rx_ntuple(dev, useraddr);
  1735. break;
  1736. case ETHTOOL_GSSET_INFO:
  1737. rc = ethtool_get_sset_info(dev, useraddr);
  1738. break;
  1739. case ETHTOOL_GRXFHINDIR:
  1740. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1741. break;
  1742. case ETHTOOL_SRXFHINDIR:
  1743. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1744. break;
  1745. case ETHTOOL_GFEATURES:
  1746. rc = ethtool_get_features(dev, useraddr);
  1747. break;
  1748. case ETHTOOL_SFEATURES:
  1749. rc = ethtool_set_features(dev, useraddr);
  1750. break;
  1751. case ETHTOOL_GTXCSUM:
  1752. case ETHTOOL_GRXCSUM:
  1753. case ETHTOOL_GSG:
  1754. case ETHTOOL_GTSO:
  1755. case ETHTOOL_GUFO:
  1756. case ETHTOOL_GGSO:
  1757. case ETHTOOL_GGRO:
  1758. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1759. break;
  1760. case ETHTOOL_STXCSUM:
  1761. case ETHTOOL_SRXCSUM:
  1762. case ETHTOOL_SSG:
  1763. case ETHTOOL_STSO:
  1764. case ETHTOOL_SUFO:
  1765. case ETHTOOL_SGSO:
  1766. case ETHTOOL_SGRO:
  1767. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1768. break;
  1769. case ETHTOOL_GCHANNELS:
  1770. rc = ethtool_get_channels(dev, useraddr);
  1771. break;
  1772. case ETHTOOL_SCHANNELS:
  1773. rc = ethtool_set_channels(dev, useraddr);
  1774. break;
  1775. case ETHTOOL_SET_DUMP:
  1776. rc = ethtool_set_dump(dev, useraddr);
  1777. break;
  1778. case ETHTOOL_GET_DUMP_FLAG:
  1779. rc = ethtool_get_dump_flag(dev, useraddr);
  1780. break;
  1781. case ETHTOOL_GET_DUMP_DATA:
  1782. rc = ethtool_get_dump_data(dev, useraddr);
  1783. break;
  1784. default:
  1785. rc = -EOPNOTSUPP;
  1786. }
  1787. if (dev->ethtool_ops->complete)
  1788. dev->ethtool_ops->complete(dev);
  1789. if (old_features != dev->features)
  1790. netdev_features_change(dev);
  1791. return rc;
  1792. }