ethtool.c 44 KB

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