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