ethtool.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. /* Handlers for each ethtool command */
  36. #define ETHTOOL_DEV_FEATURE_WORDS 1
  37. static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
  38. [NETIF_F_SG_BIT] = "tx-scatter-gather",
  39. [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
  40. [NETIF_F_NO_CSUM_BIT] = "tx-checksum-unneeded",
  41. [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
  42. [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
  43. [NETIF_F_HIGHDMA_BIT] = "highdma",
  44. [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
  45. [NETIF_F_HW_VLAN_TX_BIT] = "tx-vlan-hw-insert",
  46. [NETIF_F_HW_VLAN_RX_BIT] = "rx-vlan-hw-parse",
  47. [NETIF_F_HW_VLAN_FILTER_BIT] = "rx-vlan-filter",
  48. [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
  49. [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
  50. [NETIF_F_LLTX_BIT] = "tx-lockless",
  51. [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
  52. [NETIF_F_GRO_BIT] = "rx-gro",
  53. [NETIF_F_LRO_BIT] = "rx-lro",
  54. [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
  55. [NETIF_F_UFO_BIT] = "tx-udp-fragmentation",
  56. [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
  57. [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
  58. [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
  59. [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
  60. [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
  61. [NETIF_F_SCTP_CSUM_BIT] = "tx-checksum-sctp",
  62. [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
  63. [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
  64. [NETIF_F_RXHASH_BIT] = "rx-hashing",
  65. [NETIF_F_RXCSUM_BIT] = "rx-checksum",
  66. [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
  67. [NETIF_F_LOOPBACK_BIT] = "loopback",
  68. };
  69. static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
  70. {
  71. struct ethtool_gfeatures cmd = {
  72. .cmd = ETHTOOL_GFEATURES,
  73. .size = ETHTOOL_DEV_FEATURE_WORDS,
  74. };
  75. struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS] = {
  76. {
  77. .available = dev->hw_features,
  78. .requested = dev->wanted_features,
  79. .active = dev->features,
  80. .never_changed = NETIF_F_NEVER_CHANGE,
  81. },
  82. };
  83. u32 __user *sizeaddr;
  84. u32 copy_size;
  85. sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
  86. if (get_user(copy_size, sizeaddr))
  87. return -EFAULT;
  88. if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
  89. copy_size = ETHTOOL_DEV_FEATURE_WORDS;
  90. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  91. return -EFAULT;
  92. useraddr += sizeof(cmd);
  93. if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
  94. return -EFAULT;
  95. return 0;
  96. }
  97. static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
  98. {
  99. struct ethtool_sfeatures cmd;
  100. struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
  101. int ret = 0;
  102. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  103. return -EFAULT;
  104. useraddr += sizeof(cmd);
  105. if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
  106. return -EINVAL;
  107. if (copy_from_user(features, useraddr, sizeof(features)))
  108. return -EFAULT;
  109. if (features[0].valid & ~NETIF_F_ETHTOOL_BITS)
  110. return -EINVAL;
  111. if (features[0].valid & ~dev->hw_features) {
  112. features[0].valid &= dev->hw_features;
  113. ret |= ETHTOOL_F_UNSUPPORTED;
  114. }
  115. dev->wanted_features &= ~features[0].valid;
  116. dev->wanted_features |= features[0].valid & features[0].requested;
  117. __netdev_update_features(dev);
  118. if ((dev->wanted_features ^ dev->features) & features[0].valid)
  119. ret |= ETHTOOL_F_WISH;
  120. return ret;
  121. }
  122. static int __ethtool_get_sset_count(struct net_device *dev, int sset)
  123. {
  124. const struct ethtool_ops *ops = dev->ethtool_ops;
  125. if (sset == ETH_SS_FEATURES)
  126. return ARRAY_SIZE(netdev_features_strings);
  127. if (ops && ops->get_sset_count && ops->get_strings)
  128. return ops->get_sset_count(dev, sset);
  129. else
  130. return -EOPNOTSUPP;
  131. }
  132. static void __ethtool_get_strings(struct net_device *dev,
  133. u32 stringset, u8 *data)
  134. {
  135. const struct ethtool_ops *ops = dev->ethtool_ops;
  136. if (stringset == ETH_SS_FEATURES)
  137. memcpy(data, netdev_features_strings,
  138. sizeof(netdev_features_strings));
  139. else
  140. /* ops->get_strings is valid because checked earlier */
  141. ops->get_strings(dev, stringset, data);
  142. }
  143. static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
  144. {
  145. /* feature masks of legacy discrete ethtool ops */
  146. switch (eth_cmd) {
  147. case ETHTOOL_GTXCSUM:
  148. case ETHTOOL_STXCSUM:
  149. return NETIF_F_ALL_CSUM | NETIF_F_SCTP_CSUM;
  150. case ETHTOOL_GRXCSUM:
  151. case ETHTOOL_SRXCSUM:
  152. return NETIF_F_RXCSUM;
  153. case ETHTOOL_GSG:
  154. case ETHTOOL_SSG:
  155. return NETIF_F_SG;
  156. case ETHTOOL_GTSO:
  157. case ETHTOOL_STSO:
  158. return NETIF_F_ALL_TSO;
  159. case ETHTOOL_GUFO:
  160. case ETHTOOL_SUFO:
  161. return NETIF_F_UFO;
  162. case ETHTOOL_GGSO:
  163. case ETHTOOL_SGSO:
  164. return NETIF_F_GSO;
  165. case ETHTOOL_GGRO:
  166. case ETHTOOL_SGRO:
  167. return NETIF_F_GRO;
  168. default:
  169. BUG();
  170. }
  171. }
  172. static int ethtool_get_one_feature(struct net_device *dev,
  173. char __user *useraddr, u32 ethcmd)
  174. {
  175. netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
  176. struct ethtool_value edata = {
  177. .cmd = ethcmd,
  178. .data = !!(dev->features & mask),
  179. };
  180. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  181. return -EFAULT;
  182. return 0;
  183. }
  184. static int ethtool_set_one_feature(struct net_device *dev,
  185. void __user *useraddr, u32 ethcmd)
  186. {
  187. struct ethtool_value edata;
  188. netdev_features_t mask;
  189. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  190. return -EFAULT;
  191. mask = ethtool_get_feature_mask(ethcmd);
  192. mask &= dev->hw_features;
  193. if (!mask)
  194. return -EOPNOTSUPP;
  195. if (edata.data)
  196. dev->wanted_features |= mask;
  197. else
  198. dev->wanted_features &= ~mask;
  199. __netdev_update_features(dev);
  200. return 0;
  201. }
  202. #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
  203. ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
  204. #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_RX | \
  205. NETIF_F_HW_VLAN_TX | NETIF_F_NTUPLE | NETIF_F_RXHASH)
  206. static u32 __ethtool_get_flags(struct net_device *dev)
  207. {
  208. u32 flags = 0;
  209. if (dev->features & NETIF_F_LRO) flags |= ETH_FLAG_LRO;
  210. if (dev->features & NETIF_F_HW_VLAN_RX) flags |= ETH_FLAG_RXVLAN;
  211. if (dev->features & NETIF_F_HW_VLAN_TX) flags |= ETH_FLAG_TXVLAN;
  212. if (dev->features & NETIF_F_NTUPLE) flags |= ETH_FLAG_NTUPLE;
  213. if (dev->features & NETIF_F_RXHASH) flags |= ETH_FLAG_RXHASH;
  214. return flags;
  215. }
  216. static int __ethtool_set_flags(struct net_device *dev, u32 data)
  217. {
  218. netdev_features_t features = 0, changed;
  219. if (data & ~ETH_ALL_FLAGS)
  220. return -EINVAL;
  221. if (data & ETH_FLAG_LRO) features |= NETIF_F_LRO;
  222. if (data & ETH_FLAG_RXVLAN) features |= NETIF_F_HW_VLAN_RX;
  223. if (data & ETH_FLAG_TXVLAN) features |= NETIF_F_HW_VLAN_TX;
  224. if (data & ETH_FLAG_NTUPLE) features |= NETIF_F_NTUPLE;
  225. if (data & ETH_FLAG_RXHASH) features |= NETIF_F_RXHASH;
  226. /* allow changing only bits set in hw_features */
  227. changed = (features ^ dev->features) & ETH_ALL_FEATURES;
  228. if (changed & ~dev->hw_features)
  229. return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
  230. dev->wanted_features =
  231. (dev->wanted_features & ~changed) | (features & changed);
  232. __netdev_update_features(dev);
  233. return 0;
  234. }
  235. int __ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  236. {
  237. ASSERT_RTNL();
  238. if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings)
  239. return -EOPNOTSUPP;
  240. memset(cmd, 0, sizeof(struct ethtool_cmd));
  241. cmd->cmd = ETHTOOL_GSET;
  242. return dev->ethtool_ops->get_settings(dev, cmd);
  243. }
  244. EXPORT_SYMBOL(__ethtool_get_settings);
  245. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  246. {
  247. int err;
  248. struct ethtool_cmd cmd;
  249. err = __ethtool_get_settings(dev, &cmd);
  250. if (err < 0)
  251. return err;
  252. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  253. return -EFAULT;
  254. return 0;
  255. }
  256. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  257. {
  258. struct ethtool_cmd cmd;
  259. if (!dev->ethtool_ops->set_settings)
  260. return -EOPNOTSUPP;
  261. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  262. return -EFAULT;
  263. return dev->ethtool_ops->set_settings(dev, &cmd);
  264. }
  265. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  266. void __user *useraddr)
  267. {
  268. struct ethtool_drvinfo info;
  269. const struct ethtool_ops *ops = dev->ethtool_ops;
  270. memset(&info, 0, sizeof(info));
  271. info.cmd = ETHTOOL_GDRVINFO;
  272. if (ops && ops->get_drvinfo) {
  273. ops->get_drvinfo(dev, &info);
  274. } else if (dev->dev.parent && dev->dev.parent->driver) {
  275. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  276. sizeof(info.bus_info));
  277. strlcpy(info.driver, dev->dev.parent->driver->name,
  278. sizeof(info.driver));
  279. } else {
  280. return -EOPNOTSUPP;
  281. }
  282. /*
  283. * this method of obtaining string set info is deprecated;
  284. * Use ETHTOOL_GSSET_INFO instead.
  285. */
  286. if (ops && ops->get_sset_count) {
  287. int rc;
  288. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  289. if (rc >= 0)
  290. info.testinfo_len = rc;
  291. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  292. if (rc >= 0)
  293. info.n_stats = rc;
  294. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  295. if (rc >= 0)
  296. info.n_priv_flags = rc;
  297. }
  298. if (ops && ops->get_regs_len)
  299. info.regdump_len = ops->get_regs_len(dev);
  300. if (ops && ops->get_eeprom_len)
  301. info.eedump_len = ops->get_eeprom_len(dev);
  302. if (copy_to_user(useraddr, &info, sizeof(info)))
  303. return -EFAULT;
  304. return 0;
  305. }
  306. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  307. void __user *useraddr)
  308. {
  309. struct ethtool_sset_info info;
  310. u64 sset_mask;
  311. int i, idx = 0, n_bits = 0, ret, rc;
  312. u32 *info_buf = NULL;
  313. if (copy_from_user(&info, useraddr, sizeof(info)))
  314. return -EFAULT;
  315. /* store copy of mask, because we zero struct later on */
  316. sset_mask = info.sset_mask;
  317. if (!sset_mask)
  318. return 0;
  319. /* calculate size of return buffer */
  320. n_bits = hweight64(sset_mask);
  321. memset(&info, 0, sizeof(info));
  322. info.cmd = ETHTOOL_GSSET_INFO;
  323. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  324. if (!info_buf)
  325. return -ENOMEM;
  326. /*
  327. * fill return buffer based on input bitmask and successful
  328. * get_sset_count return
  329. */
  330. for (i = 0; i < 64; i++) {
  331. if (!(sset_mask & (1ULL << i)))
  332. continue;
  333. rc = __ethtool_get_sset_count(dev, i);
  334. if (rc >= 0) {
  335. info.sset_mask |= (1ULL << i);
  336. info_buf[idx++] = rc;
  337. }
  338. }
  339. ret = -EFAULT;
  340. if (copy_to_user(useraddr, &info, sizeof(info)))
  341. goto out;
  342. useraddr += offsetof(struct ethtool_sset_info, data);
  343. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  344. goto out;
  345. ret = 0;
  346. out:
  347. kfree(info_buf);
  348. return ret;
  349. }
  350. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  351. u32 cmd, void __user *useraddr)
  352. {
  353. struct ethtool_rxnfc info;
  354. size_t info_size = sizeof(info);
  355. if (!dev->ethtool_ops->set_rxnfc)
  356. return -EOPNOTSUPP;
  357. /* struct ethtool_rxnfc was originally defined for
  358. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  359. * members. User-space might still be using that
  360. * definition. */
  361. if (cmd == ETHTOOL_SRXFH)
  362. info_size = (offsetof(struct ethtool_rxnfc, data) +
  363. sizeof(info.data));
  364. if (copy_from_user(&info, useraddr, info_size))
  365. return -EFAULT;
  366. return dev->ethtool_ops->set_rxnfc(dev, &info);
  367. }
  368. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  369. u32 cmd, void __user *useraddr)
  370. {
  371. struct ethtool_rxnfc info;
  372. size_t info_size = sizeof(info);
  373. const struct ethtool_ops *ops = dev->ethtool_ops;
  374. int ret;
  375. void *rule_buf = NULL;
  376. if (!ops->get_rxnfc)
  377. return -EOPNOTSUPP;
  378. /* struct ethtool_rxnfc was originally defined for
  379. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  380. * members. User-space might still be using that
  381. * definition. */
  382. if (cmd == ETHTOOL_GRXFH)
  383. info_size = (offsetof(struct ethtool_rxnfc, data) +
  384. sizeof(info.data));
  385. if (copy_from_user(&info, useraddr, info_size))
  386. return -EFAULT;
  387. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  388. if (info.rule_cnt > 0) {
  389. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  390. rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
  391. GFP_USER);
  392. if (!rule_buf)
  393. return -ENOMEM;
  394. }
  395. }
  396. ret = ops->get_rxnfc(dev, &info, rule_buf);
  397. if (ret < 0)
  398. goto err_out;
  399. ret = -EFAULT;
  400. if (copy_to_user(useraddr, &info, info_size))
  401. goto err_out;
  402. if (rule_buf) {
  403. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  404. if (copy_to_user(useraddr, rule_buf,
  405. info.rule_cnt * sizeof(u32)))
  406. goto err_out;
  407. }
  408. ret = 0;
  409. err_out:
  410. kfree(rule_buf);
  411. return ret;
  412. }
  413. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  414. void __user *useraddr)
  415. {
  416. struct ethtool_rxfh_indir *indir;
  417. u32 table_size;
  418. size_t full_size;
  419. int ret;
  420. if (!dev->ethtool_ops->get_rxfh_indir)
  421. return -EOPNOTSUPP;
  422. if (copy_from_user(&table_size,
  423. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  424. sizeof(table_size)))
  425. return -EFAULT;
  426. if (table_size >
  427. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  428. return -ENOMEM;
  429. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  430. indir = kzalloc(full_size, GFP_USER);
  431. if (!indir)
  432. return -ENOMEM;
  433. indir->cmd = ETHTOOL_GRXFHINDIR;
  434. indir->size = table_size;
  435. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  436. if (ret)
  437. goto out;
  438. if (copy_to_user(useraddr, indir, full_size))
  439. ret = -EFAULT;
  440. out:
  441. kfree(indir);
  442. return ret;
  443. }
  444. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  445. void __user *useraddr)
  446. {
  447. struct ethtool_rxfh_indir *indir;
  448. u32 table_size;
  449. size_t full_size;
  450. int ret;
  451. if (!dev->ethtool_ops->set_rxfh_indir)
  452. return -EOPNOTSUPP;
  453. if (copy_from_user(&table_size,
  454. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  455. sizeof(table_size)))
  456. return -EFAULT;
  457. if (table_size >
  458. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  459. return -ENOMEM;
  460. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  461. indir = kmalloc(full_size, GFP_USER);
  462. if (!indir)
  463. return -ENOMEM;
  464. if (copy_from_user(indir, useraddr, full_size)) {
  465. ret = -EFAULT;
  466. goto out;
  467. }
  468. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  469. out:
  470. kfree(indir);
  471. return ret;
  472. }
  473. /*
  474. * ethtool does not (or did not) set masks for flow parameters that are
  475. * not specified, so if both value and mask are 0 then this must be
  476. * treated as equivalent to a mask with all bits set. Implement that
  477. * here rather than in drivers.
  478. */
  479. static void rx_ntuple_fix_masks(struct ethtool_rx_ntuple_flow_spec *fs)
  480. {
  481. struct ethtool_tcpip4_spec *entry = &fs->h_u.tcp_ip4_spec;
  482. struct ethtool_tcpip4_spec *mask = &fs->m_u.tcp_ip4_spec;
  483. if (fs->flow_type != TCP_V4_FLOW &&
  484. fs->flow_type != UDP_V4_FLOW &&
  485. fs->flow_type != SCTP_V4_FLOW)
  486. return;
  487. if (!(entry->ip4src | mask->ip4src))
  488. mask->ip4src = htonl(0xffffffff);
  489. if (!(entry->ip4dst | mask->ip4dst))
  490. mask->ip4dst = htonl(0xffffffff);
  491. if (!(entry->psrc | mask->psrc))
  492. mask->psrc = htons(0xffff);
  493. if (!(entry->pdst | mask->pdst))
  494. mask->pdst = htons(0xffff);
  495. if (!(entry->tos | mask->tos))
  496. mask->tos = 0xff;
  497. if (!(fs->vlan_tag | fs->vlan_tag_mask))
  498. fs->vlan_tag_mask = 0xffff;
  499. if (!(fs->data | fs->data_mask))
  500. fs->data_mask = 0xffffffffffffffffULL;
  501. }
  502. static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
  503. void __user *useraddr)
  504. {
  505. struct ethtool_rx_ntuple cmd;
  506. const struct ethtool_ops *ops = dev->ethtool_ops;
  507. if (!ops->set_rx_ntuple)
  508. return -EOPNOTSUPP;
  509. if (!(dev->features & NETIF_F_NTUPLE))
  510. return -EINVAL;
  511. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  512. return -EFAULT;
  513. rx_ntuple_fix_masks(&cmd.fs);
  514. return ops->set_rx_ntuple(dev, &cmd);
  515. }
  516. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  517. {
  518. struct ethtool_regs regs;
  519. const struct ethtool_ops *ops = dev->ethtool_ops;
  520. void *regbuf;
  521. int reglen, ret;
  522. if (!ops->get_regs || !ops->get_regs_len)
  523. return -EOPNOTSUPP;
  524. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  525. return -EFAULT;
  526. reglen = ops->get_regs_len(dev);
  527. if (regs.len > reglen)
  528. regs.len = reglen;
  529. regbuf = vzalloc(reglen);
  530. if (reglen && !regbuf)
  531. return -ENOMEM;
  532. ops->get_regs(dev, &regs, regbuf);
  533. ret = -EFAULT;
  534. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  535. goto out;
  536. useraddr += offsetof(struct ethtool_regs, data);
  537. if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
  538. goto out;
  539. ret = 0;
  540. out:
  541. vfree(regbuf);
  542. return ret;
  543. }
  544. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  545. {
  546. struct ethtool_value reset;
  547. int ret;
  548. if (!dev->ethtool_ops->reset)
  549. return -EOPNOTSUPP;
  550. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  551. return -EFAULT;
  552. ret = dev->ethtool_ops->reset(dev, &reset.data);
  553. if (ret)
  554. return ret;
  555. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  556. return -EFAULT;
  557. return 0;
  558. }
  559. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  560. {
  561. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  562. if (!dev->ethtool_ops->get_wol)
  563. return -EOPNOTSUPP;
  564. dev->ethtool_ops->get_wol(dev, &wol);
  565. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  566. return -EFAULT;
  567. return 0;
  568. }
  569. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  570. {
  571. struct ethtool_wolinfo wol;
  572. if (!dev->ethtool_ops->set_wol)
  573. return -EOPNOTSUPP;
  574. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  575. return -EFAULT;
  576. return dev->ethtool_ops->set_wol(dev, &wol);
  577. }
  578. static int ethtool_nway_reset(struct net_device *dev)
  579. {
  580. if (!dev->ethtool_ops->nway_reset)
  581. return -EOPNOTSUPP;
  582. return dev->ethtool_ops->nway_reset(dev);
  583. }
  584. static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
  585. {
  586. struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
  587. if (!dev->ethtool_ops->get_link)
  588. return -EOPNOTSUPP;
  589. edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
  590. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  591. return -EFAULT;
  592. return 0;
  593. }
  594. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  595. {
  596. struct ethtool_eeprom eeprom;
  597. const struct ethtool_ops *ops = dev->ethtool_ops;
  598. void __user *userbuf = useraddr + sizeof(eeprom);
  599. u32 bytes_remaining;
  600. u8 *data;
  601. int ret = 0;
  602. if (!ops->get_eeprom || !ops->get_eeprom_len)
  603. return -EOPNOTSUPP;
  604. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  605. return -EFAULT;
  606. /* Check for wrap and zero */
  607. if (eeprom.offset + eeprom.len <= eeprom.offset)
  608. return -EINVAL;
  609. /* Check for exceeding total eeprom len */
  610. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  611. return -EINVAL;
  612. data = kmalloc(PAGE_SIZE, GFP_USER);
  613. if (!data)
  614. return -ENOMEM;
  615. bytes_remaining = eeprom.len;
  616. while (bytes_remaining > 0) {
  617. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  618. ret = ops->get_eeprom(dev, &eeprom, data);
  619. if (ret)
  620. break;
  621. if (copy_to_user(userbuf, data, eeprom.len)) {
  622. ret = -EFAULT;
  623. break;
  624. }
  625. userbuf += eeprom.len;
  626. eeprom.offset += eeprom.len;
  627. bytes_remaining -= eeprom.len;
  628. }
  629. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  630. eeprom.offset -= eeprom.len;
  631. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  632. ret = -EFAULT;
  633. kfree(data);
  634. return ret;
  635. }
  636. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  637. {
  638. struct ethtool_eeprom eeprom;
  639. const struct ethtool_ops *ops = dev->ethtool_ops;
  640. void __user *userbuf = useraddr + sizeof(eeprom);
  641. u32 bytes_remaining;
  642. u8 *data;
  643. int ret = 0;
  644. if (!ops->set_eeprom || !ops->get_eeprom_len)
  645. return -EOPNOTSUPP;
  646. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  647. return -EFAULT;
  648. /* Check for wrap and zero */
  649. if (eeprom.offset + eeprom.len <= eeprom.offset)
  650. return -EINVAL;
  651. /* Check for exceeding total eeprom len */
  652. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  653. return -EINVAL;
  654. data = kmalloc(PAGE_SIZE, GFP_USER);
  655. if (!data)
  656. return -ENOMEM;
  657. bytes_remaining = eeprom.len;
  658. while (bytes_remaining > 0) {
  659. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  660. if (copy_from_user(data, userbuf, eeprom.len)) {
  661. ret = -EFAULT;
  662. break;
  663. }
  664. ret = ops->set_eeprom(dev, &eeprom, data);
  665. if (ret)
  666. break;
  667. userbuf += eeprom.len;
  668. eeprom.offset += eeprom.len;
  669. bytes_remaining -= eeprom.len;
  670. }
  671. kfree(data);
  672. return ret;
  673. }
  674. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  675. void __user *useraddr)
  676. {
  677. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  678. if (!dev->ethtool_ops->get_coalesce)
  679. return -EOPNOTSUPP;
  680. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  681. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  682. return -EFAULT;
  683. return 0;
  684. }
  685. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  686. void __user *useraddr)
  687. {
  688. struct ethtool_coalesce coalesce;
  689. if (!dev->ethtool_ops->set_coalesce)
  690. return -EOPNOTSUPP;
  691. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  692. return -EFAULT;
  693. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  694. }
  695. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  696. {
  697. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  698. if (!dev->ethtool_ops->get_ringparam)
  699. return -EOPNOTSUPP;
  700. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  701. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  702. return -EFAULT;
  703. return 0;
  704. }
  705. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  706. {
  707. struct ethtool_ringparam ringparam;
  708. if (!dev->ethtool_ops->set_ringparam)
  709. return -EOPNOTSUPP;
  710. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  711. return -EFAULT;
  712. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  713. }
  714. static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
  715. void __user *useraddr)
  716. {
  717. struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
  718. if (!dev->ethtool_ops->get_channels)
  719. return -EOPNOTSUPP;
  720. dev->ethtool_ops->get_channels(dev, &channels);
  721. if (copy_to_user(useraddr, &channels, sizeof(channels)))
  722. return -EFAULT;
  723. return 0;
  724. }
  725. static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
  726. void __user *useraddr)
  727. {
  728. struct ethtool_channels channels;
  729. if (!dev->ethtool_ops->set_channels)
  730. return -EOPNOTSUPP;
  731. if (copy_from_user(&channels, useraddr, sizeof(channels)))
  732. return -EFAULT;
  733. return dev->ethtool_ops->set_channels(dev, &channels);
  734. }
  735. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  736. {
  737. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  738. if (!dev->ethtool_ops->get_pauseparam)
  739. return -EOPNOTSUPP;
  740. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  741. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  742. return -EFAULT;
  743. return 0;
  744. }
  745. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  746. {
  747. struct ethtool_pauseparam pauseparam;
  748. if (!dev->ethtool_ops->set_pauseparam)
  749. return -EOPNOTSUPP;
  750. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  751. return -EFAULT;
  752. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  753. }
  754. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  755. {
  756. struct ethtool_test test;
  757. const struct ethtool_ops *ops = dev->ethtool_ops;
  758. u64 *data;
  759. int ret, test_len;
  760. if (!ops->self_test || !ops->get_sset_count)
  761. return -EOPNOTSUPP;
  762. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  763. if (test_len < 0)
  764. return test_len;
  765. WARN_ON(test_len == 0);
  766. if (copy_from_user(&test, useraddr, sizeof(test)))
  767. return -EFAULT;
  768. test.len = test_len;
  769. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  770. if (!data)
  771. return -ENOMEM;
  772. ops->self_test(dev, &test, data);
  773. ret = -EFAULT;
  774. if (copy_to_user(useraddr, &test, sizeof(test)))
  775. goto out;
  776. useraddr += sizeof(test);
  777. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  778. goto out;
  779. ret = 0;
  780. out:
  781. kfree(data);
  782. return ret;
  783. }
  784. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  785. {
  786. struct ethtool_gstrings gstrings;
  787. u8 *data;
  788. int ret;
  789. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  790. return -EFAULT;
  791. ret = __ethtool_get_sset_count(dev, gstrings.string_set);
  792. if (ret < 0)
  793. return ret;
  794. gstrings.len = ret;
  795. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  796. if (!data)
  797. return -ENOMEM;
  798. __ethtool_get_strings(dev, gstrings.string_set, data);
  799. ret = -EFAULT;
  800. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  801. goto out;
  802. useraddr += sizeof(gstrings);
  803. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  804. goto out;
  805. ret = 0;
  806. out:
  807. kfree(data);
  808. return ret;
  809. }
  810. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  811. {
  812. struct ethtool_value id;
  813. static bool busy;
  814. int rc;
  815. if (!dev->ethtool_ops->set_phys_id)
  816. return -EOPNOTSUPP;
  817. if (busy)
  818. return -EBUSY;
  819. if (copy_from_user(&id, useraddr, sizeof(id)))
  820. return -EFAULT;
  821. rc = dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
  822. if (rc < 0)
  823. return rc;
  824. /* Drop the RTNL lock while waiting, but prevent reentry or
  825. * removal of the device.
  826. */
  827. busy = true;
  828. dev_hold(dev);
  829. rtnl_unlock();
  830. if (rc == 0) {
  831. /* Driver will handle this itself */
  832. schedule_timeout_interruptible(
  833. id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
  834. } else {
  835. /* Driver expects to be called at twice the frequency in rc */
  836. int n = rc * 2, i, interval = HZ / n;
  837. /* Count down seconds */
  838. do {
  839. /* Count down iterations per second */
  840. i = n;
  841. do {
  842. rtnl_lock();
  843. rc = dev->ethtool_ops->set_phys_id(dev,
  844. (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
  845. rtnl_unlock();
  846. if (rc)
  847. break;
  848. schedule_timeout_interruptible(interval);
  849. } while (!signal_pending(current) && --i != 0);
  850. } while (!signal_pending(current) &&
  851. (id.data == 0 || --id.data != 0));
  852. }
  853. rtnl_lock();
  854. dev_put(dev);
  855. busy = false;
  856. (void)dev->ethtool_ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
  857. return rc;
  858. }
  859. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  860. {
  861. struct ethtool_stats stats;
  862. const struct ethtool_ops *ops = dev->ethtool_ops;
  863. u64 *data;
  864. int ret, n_stats;
  865. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  866. return -EOPNOTSUPP;
  867. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  868. if (n_stats < 0)
  869. return n_stats;
  870. WARN_ON(n_stats == 0);
  871. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  872. return -EFAULT;
  873. stats.n_stats = n_stats;
  874. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  875. if (!data)
  876. return -ENOMEM;
  877. ops->get_ethtool_stats(dev, &stats, data);
  878. ret = -EFAULT;
  879. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  880. goto out;
  881. useraddr += sizeof(stats);
  882. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  883. goto out;
  884. ret = 0;
  885. out:
  886. kfree(data);
  887. return ret;
  888. }
  889. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  890. {
  891. struct ethtool_perm_addr epaddr;
  892. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  893. return -EFAULT;
  894. if (epaddr.size < dev->addr_len)
  895. return -ETOOSMALL;
  896. epaddr.size = dev->addr_len;
  897. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  898. return -EFAULT;
  899. useraddr += sizeof(epaddr);
  900. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  901. return -EFAULT;
  902. return 0;
  903. }
  904. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  905. u32 cmd, u32 (*actor)(struct net_device *))
  906. {
  907. struct ethtool_value edata = { .cmd = cmd };
  908. if (!actor)
  909. return -EOPNOTSUPP;
  910. edata.data = actor(dev);
  911. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  912. return -EFAULT;
  913. return 0;
  914. }
  915. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  916. void (*actor)(struct net_device *, u32))
  917. {
  918. struct ethtool_value edata;
  919. if (!actor)
  920. return -EOPNOTSUPP;
  921. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  922. return -EFAULT;
  923. actor(dev, edata.data);
  924. return 0;
  925. }
  926. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  927. int (*actor)(struct net_device *, u32))
  928. {
  929. struct ethtool_value edata;
  930. if (!actor)
  931. return -EOPNOTSUPP;
  932. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  933. return -EFAULT;
  934. return actor(dev, edata.data);
  935. }
  936. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  937. char __user *useraddr)
  938. {
  939. struct ethtool_flash efl;
  940. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  941. return -EFAULT;
  942. if (!dev->ethtool_ops->flash_device)
  943. return -EOPNOTSUPP;
  944. return dev->ethtool_ops->flash_device(dev, &efl);
  945. }
  946. static int ethtool_set_dump(struct net_device *dev,
  947. void __user *useraddr)
  948. {
  949. struct ethtool_dump dump;
  950. if (!dev->ethtool_ops->set_dump)
  951. return -EOPNOTSUPP;
  952. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  953. return -EFAULT;
  954. return dev->ethtool_ops->set_dump(dev, &dump);
  955. }
  956. static int ethtool_get_dump_flag(struct net_device *dev,
  957. void __user *useraddr)
  958. {
  959. int ret;
  960. struct ethtool_dump dump;
  961. const struct ethtool_ops *ops = dev->ethtool_ops;
  962. if (!dev->ethtool_ops->get_dump_flag)
  963. return -EOPNOTSUPP;
  964. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  965. return -EFAULT;
  966. ret = ops->get_dump_flag(dev, &dump);
  967. if (ret)
  968. return ret;
  969. if (copy_to_user(useraddr, &dump, sizeof(dump)))
  970. return -EFAULT;
  971. return 0;
  972. }
  973. static int ethtool_get_dump_data(struct net_device *dev,
  974. void __user *useraddr)
  975. {
  976. int ret;
  977. __u32 len;
  978. struct ethtool_dump dump, tmp;
  979. const struct ethtool_ops *ops = dev->ethtool_ops;
  980. void *data = NULL;
  981. if (!dev->ethtool_ops->get_dump_data ||
  982. !dev->ethtool_ops->get_dump_flag)
  983. return -EOPNOTSUPP;
  984. if (copy_from_user(&dump, useraddr, sizeof(dump)))
  985. return -EFAULT;
  986. memset(&tmp, 0, sizeof(tmp));
  987. tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
  988. ret = ops->get_dump_flag(dev, &tmp);
  989. if (ret)
  990. return ret;
  991. len = (tmp.len > dump.len) ? dump.len : tmp.len;
  992. if (!len)
  993. return -EFAULT;
  994. data = vzalloc(tmp.len);
  995. if (!data)
  996. return -ENOMEM;
  997. ret = ops->get_dump_data(dev, &dump, data);
  998. if (ret)
  999. goto out;
  1000. if (copy_to_user(useraddr, &dump, sizeof(dump))) {
  1001. ret = -EFAULT;
  1002. goto out;
  1003. }
  1004. useraddr += offsetof(struct ethtool_dump, data);
  1005. if (copy_to_user(useraddr, data, len))
  1006. ret = -EFAULT;
  1007. out:
  1008. vfree(data);
  1009. return ret;
  1010. }
  1011. /* The main entry point in this file. Called from net/core/dev.c */
  1012. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1013. {
  1014. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1015. void __user *useraddr = ifr->ifr_data;
  1016. u32 ethcmd;
  1017. int rc;
  1018. u32 old_features;
  1019. if (!dev || !netif_device_present(dev))
  1020. return -ENODEV;
  1021. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1022. return -EFAULT;
  1023. if (!dev->ethtool_ops) {
  1024. /* ETHTOOL_GDRVINFO does not require any driver support.
  1025. * It is also unprivileged and does not change anything,
  1026. * so we can take a shortcut to it. */
  1027. if (ethcmd == ETHTOOL_GDRVINFO)
  1028. return ethtool_get_drvinfo(dev, useraddr);
  1029. else
  1030. return -EOPNOTSUPP;
  1031. }
  1032. /* Allow some commands to be done by anyone */
  1033. switch (ethcmd) {
  1034. case ETHTOOL_GSET:
  1035. case ETHTOOL_GDRVINFO:
  1036. case ETHTOOL_GMSGLVL:
  1037. case ETHTOOL_GCOALESCE:
  1038. case ETHTOOL_GRINGPARAM:
  1039. case ETHTOOL_GPAUSEPARAM:
  1040. case ETHTOOL_GRXCSUM:
  1041. case ETHTOOL_GTXCSUM:
  1042. case ETHTOOL_GSG:
  1043. case ETHTOOL_GSTRINGS:
  1044. case ETHTOOL_GTSO:
  1045. case ETHTOOL_GPERMADDR:
  1046. case ETHTOOL_GUFO:
  1047. case ETHTOOL_GGSO:
  1048. case ETHTOOL_GGRO:
  1049. case ETHTOOL_GFLAGS:
  1050. case ETHTOOL_GPFLAGS:
  1051. case ETHTOOL_GRXFH:
  1052. case ETHTOOL_GRXRINGS:
  1053. case ETHTOOL_GRXCLSRLCNT:
  1054. case ETHTOOL_GRXCLSRULE:
  1055. case ETHTOOL_GRXCLSRLALL:
  1056. case ETHTOOL_GFEATURES:
  1057. break;
  1058. default:
  1059. if (!capable(CAP_NET_ADMIN))
  1060. return -EPERM;
  1061. }
  1062. if (dev->ethtool_ops->begin) {
  1063. rc = dev->ethtool_ops->begin(dev);
  1064. if (rc < 0)
  1065. return rc;
  1066. }
  1067. old_features = dev->features;
  1068. switch (ethcmd) {
  1069. case ETHTOOL_GSET:
  1070. rc = ethtool_get_settings(dev, useraddr);
  1071. break;
  1072. case ETHTOOL_SSET:
  1073. rc = ethtool_set_settings(dev, useraddr);
  1074. break;
  1075. case ETHTOOL_GDRVINFO:
  1076. rc = ethtool_get_drvinfo(dev, useraddr);
  1077. break;
  1078. case ETHTOOL_GREGS:
  1079. rc = ethtool_get_regs(dev, useraddr);
  1080. break;
  1081. case ETHTOOL_GWOL:
  1082. rc = ethtool_get_wol(dev, useraddr);
  1083. break;
  1084. case ETHTOOL_SWOL:
  1085. rc = ethtool_set_wol(dev, useraddr);
  1086. break;
  1087. case ETHTOOL_GMSGLVL:
  1088. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1089. dev->ethtool_ops->get_msglevel);
  1090. break;
  1091. case ETHTOOL_SMSGLVL:
  1092. rc = ethtool_set_value_void(dev, useraddr,
  1093. dev->ethtool_ops->set_msglevel);
  1094. break;
  1095. case ETHTOOL_NWAY_RST:
  1096. rc = ethtool_nway_reset(dev);
  1097. break;
  1098. case ETHTOOL_GLINK:
  1099. rc = ethtool_get_link(dev, useraddr);
  1100. break;
  1101. case ETHTOOL_GEEPROM:
  1102. rc = ethtool_get_eeprom(dev, useraddr);
  1103. break;
  1104. case ETHTOOL_SEEPROM:
  1105. rc = ethtool_set_eeprom(dev, useraddr);
  1106. break;
  1107. case ETHTOOL_GCOALESCE:
  1108. rc = ethtool_get_coalesce(dev, useraddr);
  1109. break;
  1110. case ETHTOOL_SCOALESCE:
  1111. rc = ethtool_set_coalesce(dev, useraddr);
  1112. break;
  1113. case ETHTOOL_GRINGPARAM:
  1114. rc = ethtool_get_ringparam(dev, useraddr);
  1115. break;
  1116. case ETHTOOL_SRINGPARAM:
  1117. rc = ethtool_set_ringparam(dev, useraddr);
  1118. break;
  1119. case ETHTOOL_GPAUSEPARAM:
  1120. rc = ethtool_get_pauseparam(dev, useraddr);
  1121. break;
  1122. case ETHTOOL_SPAUSEPARAM:
  1123. rc = ethtool_set_pauseparam(dev, useraddr);
  1124. break;
  1125. case ETHTOOL_TEST:
  1126. rc = ethtool_self_test(dev, useraddr);
  1127. break;
  1128. case ETHTOOL_GSTRINGS:
  1129. rc = ethtool_get_strings(dev, useraddr);
  1130. break;
  1131. case ETHTOOL_PHYS_ID:
  1132. rc = ethtool_phys_id(dev, useraddr);
  1133. break;
  1134. case ETHTOOL_GSTATS:
  1135. rc = ethtool_get_stats(dev, useraddr);
  1136. break;
  1137. case ETHTOOL_GPERMADDR:
  1138. rc = ethtool_get_perm_addr(dev, useraddr);
  1139. break;
  1140. case ETHTOOL_GFLAGS:
  1141. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1142. __ethtool_get_flags);
  1143. break;
  1144. case ETHTOOL_SFLAGS:
  1145. rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
  1146. break;
  1147. case ETHTOOL_GPFLAGS:
  1148. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1149. dev->ethtool_ops->get_priv_flags);
  1150. break;
  1151. case ETHTOOL_SPFLAGS:
  1152. rc = ethtool_set_value(dev, useraddr,
  1153. dev->ethtool_ops->set_priv_flags);
  1154. break;
  1155. case ETHTOOL_GRXFH:
  1156. case ETHTOOL_GRXRINGS:
  1157. case ETHTOOL_GRXCLSRLCNT:
  1158. case ETHTOOL_GRXCLSRULE:
  1159. case ETHTOOL_GRXCLSRLALL:
  1160. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1161. break;
  1162. case ETHTOOL_SRXFH:
  1163. case ETHTOOL_SRXCLSRLDEL:
  1164. case ETHTOOL_SRXCLSRLINS:
  1165. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1166. break;
  1167. case ETHTOOL_FLASHDEV:
  1168. rc = ethtool_flash_device(dev, useraddr);
  1169. break;
  1170. case ETHTOOL_RESET:
  1171. rc = ethtool_reset(dev, useraddr);
  1172. break;
  1173. case ETHTOOL_SRXNTUPLE:
  1174. rc = ethtool_set_rx_ntuple(dev, useraddr);
  1175. break;
  1176. case ETHTOOL_GSSET_INFO:
  1177. rc = ethtool_get_sset_info(dev, useraddr);
  1178. break;
  1179. case ETHTOOL_GRXFHINDIR:
  1180. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1181. break;
  1182. case ETHTOOL_SRXFHINDIR:
  1183. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1184. break;
  1185. case ETHTOOL_GFEATURES:
  1186. rc = ethtool_get_features(dev, useraddr);
  1187. break;
  1188. case ETHTOOL_SFEATURES:
  1189. rc = ethtool_set_features(dev, useraddr);
  1190. break;
  1191. case ETHTOOL_GTXCSUM:
  1192. case ETHTOOL_GRXCSUM:
  1193. case ETHTOOL_GSG:
  1194. case ETHTOOL_GTSO:
  1195. case ETHTOOL_GUFO:
  1196. case ETHTOOL_GGSO:
  1197. case ETHTOOL_GGRO:
  1198. rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
  1199. break;
  1200. case ETHTOOL_STXCSUM:
  1201. case ETHTOOL_SRXCSUM:
  1202. case ETHTOOL_SSG:
  1203. case ETHTOOL_STSO:
  1204. case ETHTOOL_SUFO:
  1205. case ETHTOOL_SGSO:
  1206. case ETHTOOL_SGRO:
  1207. rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
  1208. break;
  1209. case ETHTOOL_GCHANNELS:
  1210. rc = ethtool_get_channels(dev, useraddr);
  1211. break;
  1212. case ETHTOOL_SCHANNELS:
  1213. rc = ethtool_set_channels(dev, useraddr);
  1214. break;
  1215. case ETHTOOL_SET_DUMP:
  1216. rc = ethtool_set_dump(dev, useraddr);
  1217. break;
  1218. case ETHTOOL_GET_DUMP_FLAG:
  1219. rc = ethtool_get_dump_flag(dev, useraddr);
  1220. break;
  1221. case ETHTOOL_GET_DUMP_DATA:
  1222. rc = ethtool_get_dump_data(dev, useraddr);
  1223. break;
  1224. default:
  1225. rc = -EOPNOTSUPP;
  1226. }
  1227. if (dev->ethtool_ops->complete)
  1228. dev->ethtool_ops->complete(dev);
  1229. if (old_features != dev->features)
  1230. netdev_features_change(dev);
  1231. return rc;
  1232. }