ethtool.c 52 KB

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