ethtool.c 39 KB

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