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/bitops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/slab.h>
  22. /*
  23. * Some useful ethtool_ops methods that're device independent.
  24. * If we find that all drivers want to do the same thing here,
  25. * we can turn these into dev_() function calls.
  26. */
  27. u32 ethtool_op_get_link(struct net_device *dev)
  28. {
  29. return netif_carrier_ok(dev) ? 1 : 0;
  30. }
  31. EXPORT_SYMBOL(ethtool_op_get_link);
  32. u32 ethtool_op_get_rx_csum(struct net_device *dev)
  33. {
  34. return (dev->features & NETIF_F_ALL_CSUM) != 0;
  35. }
  36. EXPORT_SYMBOL(ethtool_op_get_rx_csum);
  37. u32 ethtool_op_get_tx_csum(struct net_device *dev)
  38. {
  39. return (dev->features & NETIF_F_ALL_CSUM) != 0;
  40. }
  41. EXPORT_SYMBOL(ethtool_op_get_tx_csum);
  42. int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
  43. {
  44. if (data)
  45. dev->features |= NETIF_F_IP_CSUM;
  46. else
  47. dev->features &= ~NETIF_F_IP_CSUM;
  48. return 0;
  49. }
  50. int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
  51. {
  52. if (data)
  53. dev->features |= NETIF_F_HW_CSUM;
  54. else
  55. dev->features &= ~NETIF_F_HW_CSUM;
  56. return 0;
  57. }
  58. EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
  59. int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
  60. {
  61. if (data)
  62. dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  63. else
  64. dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
  68. u32 ethtool_op_get_sg(struct net_device *dev)
  69. {
  70. return (dev->features & NETIF_F_SG) != 0;
  71. }
  72. EXPORT_SYMBOL(ethtool_op_get_sg);
  73. int ethtool_op_set_sg(struct net_device *dev, u32 data)
  74. {
  75. if (data)
  76. dev->features |= NETIF_F_SG;
  77. else
  78. dev->features &= ~NETIF_F_SG;
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(ethtool_op_set_sg);
  82. u32 ethtool_op_get_tso(struct net_device *dev)
  83. {
  84. return (dev->features & NETIF_F_TSO) != 0;
  85. }
  86. EXPORT_SYMBOL(ethtool_op_get_tso);
  87. int ethtool_op_set_tso(struct net_device *dev, u32 data)
  88. {
  89. if (data)
  90. dev->features |= NETIF_F_TSO;
  91. else
  92. dev->features &= ~NETIF_F_TSO;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(ethtool_op_set_tso);
  96. u32 ethtool_op_get_ufo(struct net_device *dev)
  97. {
  98. return (dev->features & NETIF_F_UFO) != 0;
  99. }
  100. EXPORT_SYMBOL(ethtool_op_get_ufo);
  101. int ethtool_op_set_ufo(struct net_device *dev, u32 data)
  102. {
  103. if (data)
  104. dev->features |= NETIF_F_UFO;
  105. else
  106. dev->features &= ~NETIF_F_UFO;
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(ethtool_op_set_ufo);
  110. /* the following list of flags are the same as their associated
  111. * NETIF_F_xxx values in include/linux/netdevice.h
  112. */
  113. static const u32 flags_dup_features =
  114. (ETH_FLAG_LRO | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH);
  115. u32 ethtool_op_get_flags(struct net_device *dev)
  116. {
  117. /* in the future, this function will probably contain additional
  118. * handling for flags which are not so easily handled
  119. * by a simple masking operation
  120. */
  121. return dev->features & flags_dup_features;
  122. }
  123. EXPORT_SYMBOL(ethtool_op_get_flags);
  124. int ethtool_op_set_flags(struct net_device *dev, u32 data, u32 supported)
  125. {
  126. if (data & ~supported)
  127. return -EINVAL;
  128. dev->features = ((dev->features & ~flags_dup_features) |
  129. (data & flags_dup_features));
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(ethtool_op_set_flags);
  133. void ethtool_ntuple_flush(struct net_device *dev)
  134. {
  135. struct ethtool_rx_ntuple_flow_spec_container *fsc, *f;
  136. list_for_each_entry_safe(fsc, f, &dev->ethtool_ntuple_list.list, list) {
  137. list_del(&fsc->list);
  138. kfree(fsc);
  139. }
  140. dev->ethtool_ntuple_list.count = 0;
  141. }
  142. EXPORT_SYMBOL(ethtool_ntuple_flush);
  143. /* Handlers for each ethtool command */
  144. static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
  145. {
  146. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  147. int err;
  148. if (!dev->ethtool_ops->get_settings)
  149. return -EOPNOTSUPP;
  150. err = dev->ethtool_ops->get_settings(dev, &cmd);
  151. if (err < 0)
  152. return err;
  153. if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
  158. {
  159. struct ethtool_cmd cmd;
  160. if (!dev->ethtool_ops->set_settings)
  161. return -EOPNOTSUPP;
  162. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  163. return -EFAULT;
  164. return dev->ethtool_ops->set_settings(dev, &cmd);
  165. }
  166. static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
  167. void __user *useraddr)
  168. {
  169. struct ethtool_drvinfo info;
  170. const struct ethtool_ops *ops = dev->ethtool_ops;
  171. memset(&info, 0, sizeof(info));
  172. info.cmd = ETHTOOL_GDRVINFO;
  173. if (ops && ops->get_drvinfo) {
  174. ops->get_drvinfo(dev, &info);
  175. } else if (dev->dev.parent && dev->dev.parent->driver) {
  176. strlcpy(info.bus_info, dev_name(dev->dev.parent),
  177. sizeof(info.bus_info));
  178. strlcpy(info.driver, dev->dev.parent->driver->name,
  179. sizeof(info.driver));
  180. } else {
  181. return -EOPNOTSUPP;
  182. }
  183. /*
  184. * this method of obtaining string set info is deprecated;
  185. * Use ETHTOOL_GSSET_INFO instead.
  186. */
  187. if (ops && ops->get_sset_count) {
  188. int rc;
  189. rc = ops->get_sset_count(dev, ETH_SS_TEST);
  190. if (rc >= 0)
  191. info.testinfo_len = rc;
  192. rc = ops->get_sset_count(dev, ETH_SS_STATS);
  193. if (rc >= 0)
  194. info.n_stats = rc;
  195. rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
  196. if (rc >= 0)
  197. info.n_priv_flags = rc;
  198. }
  199. if (ops && ops->get_regs_len)
  200. info.regdump_len = ops->get_regs_len(dev);
  201. if (ops && ops->get_eeprom_len)
  202. info.eedump_len = ops->get_eeprom_len(dev);
  203. if (copy_to_user(useraddr, &info, sizeof(info)))
  204. return -EFAULT;
  205. return 0;
  206. }
  207. static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
  208. void __user *useraddr)
  209. {
  210. struct ethtool_sset_info info;
  211. const struct ethtool_ops *ops = dev->ethtool_ops;
  212. u64 sset_mask;
  213. int i, idx = 0, n_bits = 0, ret, rc;
  214. u32 *info_buf = NULL;
  215. if (!ops->get_sset_count)
  216. return -EOPNOTSUPP;
  217. if (copy_from_user(&info, useraddr, sizeof(info)))
  218. return -EFAULT;
  219. /* store copy of mask, because we zero struct later on */
  220. sset_mask = info.sset_mask;
  221. if (!sset_mask)
  222. return 0;
  223. /* calculate size of return buffer */
  224. n_bits = hweight64(sset_mask);
  225. memset(&info, 0, sizeof(info));
  226. info.cmd = ETHTOOL_GSSET_INFO;
  227. info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
  228. if (!info_buf)
  229. return -ENOMEM;
  230. /*
  231. * fill return buffer based on input bitmask and successful
  232. * get_sset_count return
  233. */
  234. for (i = 0; i < 64; i++) {
  235. if (!(sset_mask & (1ULL << i)))
  236. continue;
  237. rc = ops->get_sset_count(dev, i);
  238. if (rc >= 0) {
  239. info.sset_mask |= (1ULL << i);
  240. info_buf[idx++] = rc;
  241. }
  242. }
  243. ret = -EFAULT;
  244. if (copy_to_user(useraddr, &info, sizeof(info)))
  245. goto out;
  246. useraddr += offsetof(struct ethtool_sset_info, data);
  247. if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
  248. goto out;
  249. ret = 0;
  250. out:
  251. kfree(info_buf);
  252. return ret;
  253. }
  254. static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
  255. u32 cmd, void __user *useraddr)
  256. {
  257. struct ethtool_rxnfc info;
  258. size_t info_size = sizeof(info);
  259. if (!dev->ethtool_ops->set_rxnfc)
  260. return -EOPNOTSUPP;
  261. /* struct ethtool_rxnfc was originally defined for
  262. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  263. * members. User-space might still be using that
  264. * definition. */
  265. if (cmd == ETHTOOL_SRXFH)
  266. info_size = (offsetof(struct ethtool_rxnfc, data) +
  267. sizeof(info.data));
  268. if (copy_from_user(&info, useraddr, info_size))
  269. return -EFAULT;
  270. return dev->ethtool_ops->set_rxnfc(dev, &info);
  271. }
  272. static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
  273. u32 cmd, void __user *useraddr)
  274. {
  275. struct ethtool_rxnfc info;
  276. size_t info_size = sizeof(info);
  277. const struct ethtool_ops *ops = dev->ethtool_ops;
  278. int ret;
  279. void *rule_buf = NULL;
  280. if (!ops->get_rxnfc)
  281. return -EOPNOTSUPP;
  282. /* struct ethtool_rxnfc was originally defined for
  283. * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
  284. * members. User-space might still be using that
  285. * definition. */
  286. if (cmd == ETHTOOL_GRXFH)
  287. info_size = (offsetof(struct ethtool_rxnfc, data) +
  288. sizeof(info.data));
  289. if (copy_from_user(&info, useraddr, info_size))
  290. return -EFAULT;
  291. if (info.cmd == ETHTOOL_GRXCLSRLALL) {
  292. if (info.rule_cnt > 0) {
  293. if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
  294. rule_buf = kmalloc(info.rule_cnt * sizeof(u32),
  295. GFP_USER);
  296. if (!rule_buf)
  297. return -ENOMEM;
  298. }
  299. }
  300. ret = ops->get_rxnfc(dev, &info, rule_buf);
  301. if (ret < 0)
  302. goto err_out;
  303. ret = -EFAULT;
  304. if (copy_to_user(useraddr, &info, info_size))
  305. goto err_out;
  306. if (rule_buf) {
  307. useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
  308. if (copy_to_user(useraddr, rule_buf,
  309. info.rule_cnt * sizeof(u32)))
  310. goto err_out;
  311. }
  312. ret = 0;
  313. err_out:
  314. kfree(rule_buf);
  315. return ret;
  316. }
  317. static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
  318. void __user *useraddr)
  319. {
  320. struct ethtool_rxfh_indir *indir;
  321. u32 table_size;
  322. size_t full_size;
  323. int ret;
  324. if (!dev->ethtool_ops->get_rxfh_indir)
  325. return -EOPNOTSUPP;
  326. if (copy_from_user(&table_size,
  327. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  328. sizeof(table_size)))
  329. return -EFAULT;
  330. if (table_size >
  331. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  332. return -ENOMEM;
  333. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  334. indir = kmalloc(full_size, GFP_USER);
  335. if (!indir)
  336. return -ENOMEM;
  337. indir->cmd = ETHTOOL_GRXFHINDIR;
  338. indir->size = table_size;
  339. ret = dev->ethtool_ops->get_rxfh_indir(dev, indir);
  340. if (ret)
  341. goto out;
  342. if (copy_to_user(useraddr, indir, full_size))
  343. ret = -EFAULT;
  344. out:
  345. kfree(indir);
  346. return ret;
  347. }
  348. static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
  349. void __user *useraddr)
  350. {
  351. struct ethtool_rxfh_indir *indir;
  352. u32 table_size;
  353. size_t full_size;
  354. int ret;
  355. if (!dev->ethtool_ops->set_rxfh_indir)
  356. return -EOPNOTSUPP;
  357. if (copy_from_user(&table_size,
  358. useraddr + offsetof(struct ethtool_rxfh_indir, size),
  359. sizeof(table_size)))
  360. return -EFAULT;
  361. if (table_size >
  362. (KMALLOC_MAX_SIZE - sizeof(*indir)) / sizeof(*indir->ring_index))
  363. return -ENOMEM;
  364. full_size = sizeof(*indir) + sizeof(*indir->ring_index) * table_size;
  365. indir = kmalloc(full_size, GFP_USER);
  366. if (!indir)
  367. return -ENOMEM;
  368. if (copy_from_user(indir, useraddr, full_size)) {
  369. ret = -EFAULT;
  370. goto out;
  371. }
  372. ret = dev->ethtool_ops->set_rxfh_indir(dev, indir);
  373. out:
  374. kfree(indir);
  375. return ret;
  376. }
  377. static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list *list,
  378. struct ethtool_rx_ntuple_flow_spec *spec,
  379. struct ethtool_rx_ntuple_flow_spec_container *fsc)
  380. {
  381. /* don't add filters forever */
  382. if (list->count >= ETHTOOL_MAX_NTUPLE_LIST_ENTRY) {
  383. /* free the container */
  384. kfree(fsc);
  385. return;
  386. }
  387. /* Copy the whole filter over */
  388. fsc->fs.flow_type = spec->flow_type;
  389. memcpy(&fsc->fs.h_u, &spec->h_u, sizeof(spec->h_u));
  390. memcpy(&fsc->fs.m_u, &spec->m_u, sizeof(spec->m_u));
  391. fsc->fs.vlan_tag = spec->vlan_tag;
  392. fsc->fs.vlan_tag_mask = spec->vlan_tag_mask;
  393. fsc->fs.data = spec->data;
  394. fsc->fs.data_mask = spec->data_mask;
  395. fsc->fs.action = spec->action;
  396. /* add to the list */
  397. list_add_tail_rcu(&fsc->list, &list->list);
  398. list->count++;
  399. }
  400. static noinline_for_stack int ethtool_set_rx_ntuple(struct net_device *dev,
  401. void __user *useraddr)
  402. {
  403. struct ethtool_rx_ntuple cmd;
  404. const struct ethtool_ops *ops = dev->ethtool_ops;
  405. struct ethtool_rx_ntuple_flow_spec_container *fsc = NULL;
  406. int ret;
  407. if (!(dev->features & NETIF_F_NTUPLE))
  408. return -EINVAL;
  409. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  410. return -EFAULT;
  411. /*
  412. * Cache filter in dev struct for GET operation only if
  413. * the underlying driver doesn't have its own GET operation, and
  414. * only if the filter was added successfully. First make sure we
  415. * can allocate the filter, then continue if successful.
  416. */
  417. if (!ops->get_rx_ntuple) {
  418. fsc = kmalloc(sizeof(*fsc), GFP_ATOMIC);
  419. if (!fsc)
  420. return -ENOMEM;
  421. }
  422. ret = ops->set_rx_ntuple(dev, &cmd);
  423. if (ret) {
  424. kfree(fsc);
  425. return ret;
  426. }
  427. if (!ops->get_rx_ntuple)
  428. __rx_ntuple_filter_add(&dev->ethtool_ntuple_list, &cmd.fs, fsc);
  429. return ret;
  430. }
  431. static int ethtool_get_rx_ntuple(struct net_device *dev, void __user *useraddr)
  432. {
  433. struct ethtool_gstrings gstrings;
  434. const struct ethtool_ops *ops = dev->ethtool_ops;
  435. struct ethtool_rx_ntuple_flow_spec_container *fsc;
  436. u8 *data;
  437. char *p;
  438. int ret, i, num_strings = 0;
  439. if (!ops->get_sset_count)
  440. return -EOPNOTSUPP;
  441. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  442. return -EFAULT;
  443. ret = ops->get_sset_count(dev, gstrings.string_set);
  444. if (ret < 0)
  445. return ret;
  446. gstrings.len = ret;
  447. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  448. if (!data)
  449. return -ENOMEM;
  450. if (ops->get_rx_ntuple) {
  451. /* driver-specific filter grab */
  452. ret = ops->get_rx_ntuple(dev, gstrings.string_set, data);
  453. goto copy;
  454. }
  455. /* default ethtool filter grab */
  456. i = 0;
  457. p = (char *)data;
  458. list_for_each_entry(fsc, &dev->ethtool_ntuple_list.list, list) {
  459. sprintf(p, "Filter %d:\n", i);
  460. p += ETH_GSTRING_LEN;
  461. num_strings++;
  462. switch (fsc->fs.flow_type) {
  463. case TCP_V4_FLOW:
  464. sprintf(p, "\tFlow Type: TCP\n");
  465. p += ETH_GSTRING_LEN;
  466. num_strings++;
  467. break;
  468. case UDP_V4_FLOW:
  469. sprintf(p, "\tFlow Type: UDP\n");
  470. p += ETH_GSTRING_LEN;
  471. num_strings++;
  472. break;
  473. case SCTP_V4_FLOW:
  474. sprintf(p, "\tFlow Type: SCTP\n");
  475. p += ETH_GSTRING_LEN;
  476. num_strings++;
  477. break;
  478. case AH_ESP_V4_FLOW:
  479. sprintf(p, "\tFlow Type: AH ESP\n");
  480. p += ETH_GSTRING_LEN;
  481. num_strings++;
  482. break;
  483. case ESP_V4_FLOW:
  484. sprintf(p, "\tFlow Type: ESP\n");
  485. p += ETH_GSTRING_LEN;
  486. num_strings++;
  487. break;
  488. case IP_USER_FLOW:
  489. sprintf(p, "\tFlow Type: Raw IP\n");
  490. p += ETH_GSTRING_LEN;
  491. num_strings++;
  492. break;
  493. case IPV4_FLOW:
  494. sprintf(p, "\tFlow Type: IPv4\n");
  495. p += ETH_GSTRING_LEN;
  496. num_strings++;
  497. break;
  498. default:
  499. sprintf(p, "\tFlow Type: Unknown\n");
  500. p += ETH_GSTRING_LEN;
  501. num_strings++;
  502. goto unknown_filter;
  503. }
  504. /* now the rest of the filters */
  505. switch (fsc->fs.flow_type) {
  506. case TCP_V4_FLOW:
  507. case UDP_V4_FLOW:
  508. case SCTP_V4_FLOW:
  509. sprintf(p, "\tSrc IP addr: 0x%x\n",
  510. fsc->fs.h_u.tcp_ip4_spec.ip4src);
  511. p += ETH_GSTRING_LEN;
  512. num_strings++;
  513. sprintf(p, "\tSrc IP mask: 0x%x\n",
  514. fsc->fs.m_u.tcp_ip4_spec.ip4src);
  515. p += ETH_GSTRING_LEN;
  516. num_strings++;
  517. sprintf(p, "\tDest IP addr: 0x%x\n",
  518. fsc->fs.h_u.tcp_ip4_spec.ip4dst);
  519. p += ETH_GSTRING_LEN;
  520. num_strings++;
  521. sprintf(p, "\tDest IP mask: 0x%x\n",
  522. fsc->fs.m_u.tcp_ip4_spec.ip4dst);
  523. p += ETH_GSTRING_LEN;
  524. num_strings++;
  525. sprintf(p, "\tSrc Port: %d, mask: 0x%x\n",
  526. fsc->fs.h_u.tcp_ip4_spec.psrc,
  527. fsc->fs.m_u.tcp_ip4_spec.psrc);
  528. p += ETH_GSTRING_LEN;
  529. num_strings++;
  530. sprintf(p, "\tDest Port: %d, mask: 0x%x\n",
  531. fsc->fs.h_u.tcp_ip4_spec.pdst,
  532. fsc->fs.m_u.tcp_ip4_spec.pdst);
  533. p += ETH_GSTRING_LEN;
  534. num_strings++;
  535. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  536. fsc->fs.h_u.tcp_ip4_spec.tos,
  537. fsc->fs.m_u.tcp_ip4_spec.tos);
  538. p += ETH_GSTRING_LEN;
  539. num_strings++;
  540. break;
  541. case AH_ESP_V4_FLOW:
  542. case ESP_V4_FLOW:
  543. sprintf(p, "\tSrc IP addr: 0x%x\n",
  544. fsc->fs.h_u.ah_ip4_spec.ip4src);
  545. p += ETH_GSTRING_LEN;
  546. num_strings++;
  547. sprintf(p, "\tSrc IP mask: 0x%x\n",
  548. fsc->fs.m_u.ah_ip4_spec.ip4src);
  549. p += ETH_GSTRING_LEN;
  550. num_strings++;
  551. sprintf(p, "\tDest IP addr: 0x%x\n",
  552. fsc->fs.h_u.ah_ip4_spec.ip4dst);
  553. p += ETH_GSTRING_LEN;
  554. num_strings++;
  555. sprintf(p, "\tDest IP mask: 0x%x\n",
  556. fsc->fs.m_u.ah_ip4_spec.ip4dst);
  557. p += ETH_GSTRING_LEN;
  558. num_strings++;
  559. sprintf(p, "\tSPI: %d, mask: 0x%x\n",
  560. fsc->fs.h_u.ah_ip4_spec.spi,
  561. fsc->fs.m_u.ah_ip4_spec.spi);
  562. p += ETH_GSTRING_LEN;
  563. num_strings++;
  564. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  565. fsc->fs.h_u.ah_ip4_spec.tos,
  566. fsc->fs.m_u.ah_ip4_spec.tos);
  567. p += ETH_GSTRING_LEN;
  568. num_strings++;
  569. break;
  570. case IP_USER_FLOW:
  571. sprintf(p, "\tSrc IP addr: 0x%x\n",
  572. fsc->fs.h_u.raw_ip4_spec.ip4src);
  573. p += ETH_GSTRING_LEN;
  574. num_strings++;
  575. sprintf(p, "\tSrc IP mask: 0x%x\n",
  576. fsc->fs.m_u.raw_ip4_spec.ip4src);
  577. p += ETH_GSTRING_LEN;
  578. num_strings++;
  579. sprintf(p, "\tDest IP addr: 0x%x\n",
  580. fsc->fs.h_u.raw_ip4_spec.ip4dst);
  581. p += ETH_GSTRING_LEN;
  582. num_strings++;
  583. sprintf(p, "\tDest IP mask: 0x%x\n",
  584. fsc->fs.m_u.raw_ip4_spec.ip4dst);
  585. p += ETH_GSTRING_LEN;
  586. num_strings++;
  587. break;
  588. case IPV4_FLOW:
  589. sprintf(p, "\tSrc IP addr: 0x%x\n",
  590. fsc->fs.h_u.usr_ip4_spec.ip4src);
  591. p += ETH_GSTRING_LEN;
  592. num_strings++;
  593. sprintf(p, "\tSrc IP mask: 0x%x\n",
  594. fsc->fs.m_u.usr_ip4_spec.ip4src);
  595. p += ETH_GSTRING_LEN;
  596. num_strings++;
  597. sprintf(p, "\tDest IP addr: 0x%x\n",
  598. fsc->fs.h_u.usr_ip4_spec.ip4dst);
  599. p += ETH_GSTRING_LEN;
  600. num_strings++;
  601. sprintf(p, "\tDest IP mask: 0x%x\n",
  602. fsc->fs.m_u.usr_ip4_spec.ip4dst);
  603. p += ETH_GSTRING_LEN;
  604. num_strings++;
  605. sprintf(p, "\tL4 bytes: 0x%x, mask: 0x%x\n",
  606. fsc->fs.h_u.usr_ip4_spec.l4_4_bytes,
  607. fsc->fs.m_u.usr_ip4_spec.l4_4_bytes);
  608. p += ETH_GSTRING_LEN;
  609. num_strings++;
  610. sprintf(p, "\tTOS: %d, mask: 0x%x\n",
  611. fsc->fs.h_u.usr_ip4_spec.tos,
  612. fsc->fs.m_u.usr_ip4_spec.tos);
  613. p += ETH_GSTRING_LEN;
  614. num_strings++;
  615. sprintf(p, "\tIP Version: %d, mask: 0x%x\n",
  616. fsc->fs.h_u.usr_ip4_spec.ip_ver,
  617. fsc->fs.m_u.usr_ip4_spec.ip_ver);
  618. p += ETH_GSTRING_LEN;
  619. num_strings++;
  620. sprintf(p, "\tProtocol: %d, mask: 0x%x\n",
  621. fsc->fs.h_u.usr_ip4_spec.proto,
  622. fsc->fs.m_u.usr_ip4_spec.proto);
  623. p += ETH_GSTRING_LEN;
  624. num_strings++;
  625. break;
  626. }
  627. sprintf(p, "\tVLAN: %d, mask: 0x%x\n",
  628. fsc->fs.vlan_tag, fsc->fs.vlan_tag_mask);
  629. p += ETH_GSTRING_LEN;
  630. num_strings++;
  631. sprintf(p, "\tUser-defined: 0x%Lx\n", fsc->fs.data);
  632. p += ETH_GSTRING_LEN;
  633. num_strings++;
  634. sprintf(p, "\tUser-defined mask: 0x%Lx\n", fsc->fs.data_mask);
  635. p += ETH_GSTRING_LEN;
  636. num_strings++;
  637. if (fsc->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP)
  638. sprintf(p, "\tAction: Drop\n");
  639. else
  640. sprintf(p, "\tAction: Direct to queue %d\n",
  641. fsc->fs.action);
  642. p += ETH_GSTRING_LEN;
  643. num_strings++;
  644. unknown_filter:
  645. i++;
  646. }
  647. copy:
  648. /* indicate to userspace how many strings we actually have */
  649. gstrings.len = num_strings;
  650. ret = -EFAULT;
  651. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  652. goto out;
  653. useraddr += sizeof(gstrings);
  654. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  655. goto out;
  656. ret = 0;
  657. out:
  658. kfree(data);
  659. return ret;
  660. }
  661. static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
  662. {
  663. struct ethtool_regs regs;
  664. const struct ethtool_ops *ops = dev->ethtool_ops;
  665. void *regbuf;
  666. int reglen, ret;
  667. if (!ops->get_regs || !ops->get_regs_len)
  668. return -EOPNOTSUPP;
  669. if (copy_from_user(&regs, useraddr, sizeof(regs)))
  670. return -EFAULT;
  671. reglen = ops->get_regs_len(dev);
  672. if (regs.len > reglen)
  673. regs.len = reglen;
  674. regbuf = kmalloc(reglen, GFP_USER);
  675. if (!regbuf)
  676. return -ENOMEM;
  677. ops->get_regs(dev, &regs, regbuf);
  678. ret = -EFAULT;
  679. if (copy_to_user(useraddr, &regs, sizeof(regs)))
  680. goto out;
  681. useraddr += offsetof(struct ethtool_regs, data);
  682. if (copy_to_user(useraddr, regbuf, regs.len))
  683. goto out;
  684. ret = 0;
  685. out:
  686. kfree(regbuf);
  687. return ret;
  688. }
  689. static int ethtool_reset(struct net_device *dev, char __user *useraddr)
  690. {
  691. struct ethtool_value reset;
  692. int ret;
  693. if (!dev->ethtool_ops->reset)
  694. return -EOPNOTSUPP;
  695. if (copy_from_user(&reset, useraddr, sizeof(reset)))
  696. return -EFAULT;
  697. ret = dev->ethtool_ops->reset(dev, &reset.data);
  698. if (ret)
  699. return ret;
  700. if (copy_to_user(useraddr, &reset, sizeof(reset)))
  701. return -EFAULT;
  702. return 0;
  703. }
  704. static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
  705. {
  706. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  707. if (!dev->ethtool_ops->get_wol)
  708. return -EOPNOTSUPP;
  709. dev->ethtool_ops->get_wol(dev, &wol);
  710. if (copy_to_user(useraddr, &wol, sizeof(wol)))
  711. return -EFAULT;
  712. return 0;
  713. }
  714. static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
  715. {
  716. struct ethtool_wolinfo wol;
  717. if (!dev->ethtool_ops->set_wol)
  718. return -EOPNOTSUPP;
  719. if (copy_from_user(&wol, useraddr, sizeof(wol)))
  720. return -EFAULT;
  721. return dev->ethtool_ops->set_wol(dev, &wol);
  722. }
  723. static int ethtool_nway_reset(struct net_device *dev)
  724. {
  725. if (!dev->ethtool_ops->nway_reset)
  726. return -EOPNOTSUPP;
  727. return dev->ethtool_ops->nway_reset(dev);
  728. }
  729. static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
  730. {
  731. struct ethtool_eeprom eeprom;
  732. const struct ethtool_ops *ops = dev->ethtool_ops;
  733. void __user *userbuf = useraddr + sizeof(eeprom);
  734. u32 bytes_remaining;
  735. u8 *data;
  736. int ret = 0;
  737. if (!ops->get_eeprom || !ops->get_eeprom_len)
  738. return -EOPNOTSUPP;
  739. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  740. return -EFAULT;
  741. /* Check for wrap and zero */
  742. if (eeprom.offset + eeprom.len <= eeprom.offset)
  743. return -EINVAL;
  744. /* Check for exceeding total eeprom len */
  745. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  746. return -EINVAL;
  747. data = kmalloc(PAGE_SIZE, GFP_USER);
  748. if (!data)
  749. return -ENOMEM;
  750. bytes_remaining = eeprom.len;
  751. while (bytes_remaining > 0) {
  752. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  753. ret = ops->get_eeprom(dev, &eeprom, data);
  754. if (ret)
  755. break;
  756. if (copy_to_user(userbuf, data, eeprom.len)) {
  757. ret = -EFAULT;
  758. break;
  759. }
  760. userbuf += eeprom.len;
  761. eeprom.offset += eeprom.len;
  762. bytes_remaining -= eeprom.len;
  763. }
  764. eeprom.len = userbuf - (useraddr + sizeof(eeprom));
  765. eeprom.offset -= eeprom.len;
  766. if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
  767. ret = -EFAULT;
  768. kfree(data);
  769. return ret;
  770. }
  771. static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
  772. {
  773. struct ethtool_eeprom eeprom;
  774. const struct ethtool_ops *ops = dev->ethtool_ops;
  775. void __user *userbuf = useraddr + sizeof(eeprom);
  776. u32 bytes_remaining;
  777. u8 *data;
  778. int ret = 0;
  779. if (!ops->set_eeprom || !ops->get_eeprom_len)
  780. return -EOPNOTSUPP;
  781. if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
  782. return -EFAULT;
  783. /* Check for wrap and zero */
  784. if (eeprom.offset + eeprom.len <= eeprom.offset)
  785. return -EINVAL;
  786. /* Check for exceeding total eeprom len */
  787. if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
  788. return -EINVAL;
  789. data = kmalloc(PAGE_SIZE, GFP_USER);
  790. if (!data)
  791. return -ENOMEM;
  792. bytes_remaining = eeprom.len;
  793. while (bytes_remaining > 0) {
  794. eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
  795. if (copy_from_user(data, userbuf, eeprom.len)) {
  796. ret = -EFAULT;
  797. break;
  798. }
  799. ret = ops->set_eeprom(dev, &eeprom, data);
  800. if (ret)
  801. break;
  802. userbuf += eeprom.len;
  803. eeprom.offset += eeprom.len;
  804. bytes_remaining -= eeprom.len;
  805. }
  806. kfree(data);
  807. return ret;
  808. }
  809. static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
  810. void __user *useraddr)
  811. {
  812. struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
  813. if (!dev->ethtool_ops->get_coalesce)
  814. return -EOPNOTSUPP;
  815. dev->ethtool_ops->get_coalesce(dev, &coalesce);
  816. if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
  817. return -EFAULT;
  818. return 0;
  819. }
  820. static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
  821. void __user *useraddr)
  822. {
  823. struct ethtool_coalesce coalesce;
  824. if (!dev->ethtool_ops->set_coalesce)
  825. return -EOPNOTSUPP;
  826. if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
  827. return -EFAULT;
  828. return dev->ethtool_ops->set_coalesce(dev, &coalesce);
  829. }
  830. static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
  831. {
  832. struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
  833. if (!dev->ethtool_ops->get_ringparam)
  834. return -EOPNOTSUPP;
  835. dev->ethtool_ops->get_ringparam(dev, &ringparam);
  836. if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
  837. return -EFAULT;
  838. return 0;
  839. }
  840. static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
  841. {
  842. struct ethtool_ringparam ringparam;
  843. if (!dev->ethtool_ops->set_ringparam)
  844. return -EOPNOTSUPP;
  845. if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
  846. return -EFAULT;
  847. return dev->ethtool_ops->set_ringparam(dev, &ringparam);
  848. }
  849. static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
  850. {
  851. struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
  852. if (!dev->ethtool_ops->get_pauseparam)
  853. return -EOPNOTSUPP;
  854. dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
  855. if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
  856. return -EFAULT;
  857. return 0;
  858. }
  859. static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
  860. {
  861. struct ethtool_pauseparam pauseparam;
  862. if (!dev->ethtool_ops->set_pauseparam)
  863. return -EOPNOTSUPP;
  864. if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
  865. return -EFAULT;
  866. return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
  867. }
  868. static int __ethtool_set_sg(struct net_device *dev, u32 data)
  869. {
  870. int err;
  871. if (!data && dev->ethtool_ops->set_tso) {
  872. err = dev->ethtool_ops->set_tso(dev, 0);
  873. if (err)
  874. return err;
  875. }
  876. if (!data && dev->ethtool_ops->set_ufo) {
  877. err = dev->ethtool_ops->set_ufo(dev, 0);
  878. if (err)
  879. return err;
  880. }
  881. return dev->ethtool_ops->set_sg(dev, data);
  882. }
  883. static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
  884. {
  885. struct ethtool_value edata;
  886. int err;
  887. if (!dev->ethtool_ops->set_tx_csum)
  888. return -EOPNOTSUPP;
  889. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  890. return -EFAULT;
  891. if (!edata.data && dev->ethtool_ops->set_sg) {
  892. err = __ethtool_set_sg(dev, 0);
  893. if (err)
  894. return err;
  895. }
  896. return dev->ethtool_ops->set_tx_csum(dev, edata.data);
  897. }
  898. EXPORT_SYMBOL(ethtool_op_set_tx_csum);
  899. static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
  900. {
  901. struct ethtool_value edata;
  902. if (!dev->ethtool_ops->set_rx_csum)
  903. return -EOPNOTSUPP;
  904. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  905. return -EFAULT;
  906. if (!edata.data && dev->ethtool_ops->set_sg)
  907. dev->features &= ~NETIF_F_GRO;
  908. return dev->ethtool_ops->set_rx_csum(dev, edata.data);
  909. }
  910. static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
  911. {
  912. struct ethtool_value edata;
  913. if (!dev->ethtool_ops->set_sg)
  914. return -EOPNOTSUPP;
  915. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  916. return -EFAULT;
  917. if (edata.data &&
  918. !(dev->features & NETIF_F_ALL_CSUM))
  919. return -EINVAL;
  920. return __ethtool_set_sg(dev, edata.data);
  921. }
  922. static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
  923. {
  924. struct ethtool_value edata;
  925. if (!dev->ethtool_ops->set_tso)
  926. return -EOPNOTSUPP;
  927. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  928. return -EFAULT;
  929. if (edata.data && !(dev->features & NETIF_F_SG))
  930. return -EINVAL;
  931. return dev->ethtool_ops->set_tso(dev, edata.data);
  932. }
  933. static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
  934. {
  935. struct ethtool_value edata;
  936. if (!dev->ethtool_ops->set_ufo)
  937. return -EOPNOTSUPP;
  938. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  939. return -EFAULT;
  940. if (edata.data && !(dev->features & NETIF_F_SG))
  941. return -EINVAL;
  942. if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
  943. return -EINVAL;
  944. return dev->ethtool_ops->set_ufo(dev, edata.data);
  945. }
  946. static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
  947. {
  948. struct ethtool_value edata = { ETHTOOL_GGSO };
  949. edata.data = dev->features & NETIF_F_GSO;
  950. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  951. return -EFAULT;
  952. return 0;
  953. }
  954. static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
  955. {
  956. struct ethtool_value edata;
  957. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  958. return -EFAULT;
  959. if (edata.data)
  960. dev->features |= NETIF_F_GSO;
  961. else
  962. dev->features &= ~NETIF_F_GSO;
  963. return 0;
  964. }
  965. static int ethtool_get_gro(struct net_device *dev, char __user *useraddr)
  966. {
  967. struct ethtool_value edata = { ETHTOOL_GGRO };
  968. edata.data = dev->features & NETIF_F_GRO;
  969. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  970. return -EFAULT;
  971. return 0;
  972. }
  973. static int ethtool_set_gro(struct net_device *dev, char __user *useraddr)
  974. {
  975. struct ethtool_value edata;
  976. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  977. return -EFAULT;
  978. if (edata.data) {
  979. if (!dev->ethtool_ops->get_rx_csum ||
  980. !dev->ethtool_ops->get_rx_csum(dev))
  981. return -EINVAL;
  982. dev->features |= NETIF_F_GRO;
  983. } else
  984. dev->features &= ~NETIF_F_GRO;
  985. return 0;
  986. }
  987. static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
  988. {
  989. struct ethtool_test test;
  990. const struct ethtool_ops *ops = dev->ethtool_ops;
  991. u64 *data;
  992. int ret, test_len;
  993. if (!ops->self_test || !ops->get_sset_count)
  994. return -EOPNOTSUPP;
  995. test_len = ops->get_sset_count(dev, ETH_SS_TEST);
  996. if (test_len < 0)
  997. return test_len;
  998. WARN_ON(test_len == 0);
  999. if (copy_from_user(&test, useraddr, sizeof(test)))
  1000. return -EFAULT;
  1001. test.len = test_len;
  1002. data = kmalloc(test_len * sizeof(u64), GFP_USER);
  1003. if (!data)
  1004. return -ENOMEM;
  1005. ops->self_test(dev, &test, data);
  1006. ret = -EFAULT;
  1007. if (copy_to_user(useraddr, &test, sizeof(test)))
  1008. goto out;
  1009. useraddr += sizeof(test);
  1010. if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
  1011. goto out;
  1012. ret = 0;
  1013. out:
  1014. kfree(data);
  1015. return ret;
  1016. }
  1017. static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
  1018. {
  1019. struct ethtool_gstrings gstrings;
  1020. const struct ethtool_ops *ops = dev->ethtool_ops;
  1021. u8 *data;
  1022. int ret;
  1023. if (!ops->get_strings || !ops->get_sset_count)
  1024. return -EOPNOTSUPP;
  1025. if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
  1026. return -EFAULT;
  1027. ret = ops->get_sset_count(dev, gstrings.string_set);
  1028. if (ret < 0)
  1029. return ret;
  1030. gstrings.len = ret;
  1031. data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
  1032. if (!data)
  1033. return -ENOMEM;
  1034. ops->get_strings(dev, gstrings.string_set, data);
  1035. ret = -EFAULT;
  1036. if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
  1037. goto out;
  1038. useraddr += sizeof(gstrings);
  1039. if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
  1040. goto out;
  1041. ret = 0;
  1042. out:
  1043. kfree(data);
  1044. return ret;
  1045. }
  1046. static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
  1047. {
  1048. struct ethtool_value id;
  1049. if (!dev->ethtool_ops->phys_id)
  1050. return -EOPNOTSUPP;
  1051. if (copy_from_user(&id, useraddr, sizeof(id)))
  1052. return -EFAULT;
  1053. return dev->ethtool_ops->phys_id(dev, id.data);
  1054. }
  1055. static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
  1056. {
  1057. struct ethtool_stats stats;
  1058. const struct ethtool_ops *ops = dev->ethtool_ops;
  1059. u64 *data;
  1060. int ret, n_stats;
  1061. if (!ops->get_ethtool_stats || !ops->get_sset_count)
  1062. return -EOPNOTSUPP;
  1063. n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
  1064. if (n_stats < 0)
  1065. return n_stats;
  1066. WARN_ON(n_stats == 0);
  1067. if (copy_from_user(&stats, useraddr, sizeof(stats)))
  1068. return -EFAULT;
  1069. stats.n_stats = n_stats;
  1070. data = kmalloc(n_stats * sizeof(u64), GFP_USER);
  1071. if (!data)
  1072. return -ENOMEM;
  1073. ops->get_ethtool_stats(dev, &stats, data);
  1074. ret = -EFAULT;
  1075. if (copy_to_user(useraddr, &stats, sizeof(stats)))
  1076. goto out;
  1077. useraddr += sizeof(stats);
  1078. if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
  1079. goto out;
  1080. ret = 0;
  1081. out:
  1082. kfree(data);
  1083. return ret;
  1084. }
  1085. static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
  1086. {
  1087. struct ethtool_perm_addr epaddr;
  1088. if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
  1089. return -EFAULT;
  1090. if (epaddr.size < dev->addr_len)
  1091. return -ETOOSMALL;
  1092. epaddr.size = dev->addr_len;
  1093. if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
  1094. return -EFAULT;
  1095. useraddr += sizeof(epaddr);
  1096. if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
  1097. return -EFAULT;
  1098. return 0;
  1099. }
  1100. static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
  1101. u32 cmd, u32 (*actor)(struct net_device *))
  1102. {
  1103. struct ethtool_value edata = { .cmd = cmd };
  1104. if (!actor)
  1105. return -EOPNOTSUPP;
  1106. edata.data = actor(dev);
  1107. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1108. return -EFAULT;
  1109. return 0;
  1110. }
  1111. static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
  1112. void (*actor)(struct net_device *, u32))
  1113. {
  1114. struct ethtool_value edata;
  1115. if (!actor)
  1116. return -EOPNOTSUPP;
  1117. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1118. return -EFAULT;
  1119. actor(dev, edata.data);
  1120. return 0;
  1121. }
  1122. static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
  1123. int (*actor)(struct net_device *, u32))
  1124. {
  1125. struct ethtool_value edata;
  1126. if (!actor)
  1127. return -EOPNOTSUPP;
  1128. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1129. return -EFAULT;
  1130. return actor(dev, edata.data);
  1131. }
  1132. static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
  1133. char __user *useraddr)
  1134. {
  1135. struct ethtool_flash efl;
  1136. if (copy_from_user(&efl, useraddr, sizeof(efl)))
  1137. return -EFAULT;
  1138. if (!dev->ethtool_ops->flash_device)
  1139. return -EOPNOTSUPP;
  1140. return dev->ethtool_ops->flash_device(dev, &efl);
  1141. }
  1142. /* The main entry point in this file. Called from net/core/dev.c */
  1143. int dev_ethtool(struct net *net, struct ifreq *ifr)
  1144. {
  1145. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  1146. void __user *useraddr = ifr->ifr_data;
  1147. u32 ethcmd;
  1148. int rc;
  1149. unsigned long old_features;
  1150. if (!dev || !netif_device_present(dev))
  1151. return -ENODEV;
  1152. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  1153. return -EFAULT;
  1154. if (!dev->ethtool_ops) {
  1155. /* ETHTOOL_GDRVINFO does not require any driver support.
  1156. * It is also unprivileged and does not change anything,
  1157. * so we can take a shortcut to it. */
  1158. if (ethcmd == ETHTOOL_GDRVINFO)
  1159. return ethtool_get_drvinfo(dev, useraddr);
  1160. else
  1161. return -EOPNOTSUPP;
  1162. }
  1163. /* Allow some commands to be done by anyone */
  1164. switch (ethcmd) {
  1165. case ETHTOOL_GDRVINFO:
  1166. case ETHTOOL_GMSGLVL:
  1167. case ETHTOOL_GCOALESCE:
  1168. case ETHTOOL_GRINGPARAM:
  1169. case ETHTOOL_GPAUSEPARAM:
  1170. case ETHTOOL_GRXCSUM:
  1171. case ETHTOOL_GTXCSUM:
  1172. case ETHTOOL_GSG:
  1173. case ETHTOOL_GSTRINGS:
  1174. case ETHTOOL_GTSO:
  1175. case ETHTOOL_GPERMADDR:
  1176. case ETHTOOL_GUFO:
  1177. case ETHTOOL_GGSO:
  1178. case ETHTOOL_GGRO:
  1179. case ETHTOOL_GFLAGS:
  1180. case ETHTOOL_GPFLAGS:
  1181. case ETHTOOL_GRXFH:
  1182. case ETHTOOL_GRXRINGS:
  1183. case ETHTOOL_GRXCLSRLCNT:
  1184. case ETHTOOL_GRXCLSRULE:
  1185. case ETHTOOL_GRXCLSRLALL:
  1186. break;
  1187. default:
  1188. if (!capable(CAP_NET_ADMIN))
  1189. return -EPERM;
  1190. }
  1191. if (dev->ethtool_ops->begin) {
  1192. rc = dev->ethtool_ops->begin(dev);
  1193. if (rc < 0)
  1194. return rc;
  1195. }
  1196. old_features = dev->features;
  1197. switch (ethcmd) {
  1198. case ETHTOOL_GSET:
  1199. rc = ethtool_get_settings(dev, useraddr);
  1200. break;
  1201. case ETHTOOL_SSET:
  1202. rc = ethtool_set_settings(dev, useraddr);
  1203. break;
  1204. case ETHTOOL_GDRVINFO:
  1205. rc = ethtool_get_drvinfo(dev, useraddr);
  1206. break;
  1207. case ETHTOOL_GREGS:
  1208. rc = ethtool_get_regs(dev, useraddr);
  1209. break;
  1210. case ETHTOOL_GWOL:
  1211. rc = ethtool_get_wol(dev, useraddr);
  1212. break;
  1213. case ETHTOOL_SWOL:
  1214. rc = ethtool_set_wol(dev, useraddr);
  1215. break;
  1216. case ETHTOOL_GMSGLVL:
  1217. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1218. dev->ethtool_ops->get_msglevel);
  1219. break;
  1220. case ETHTOOL_SMSGLVL:
  1221. rc = ethtool_set_value_void(dev, useraddr,
  1222. dev->ethtool_ops->set_msglevel);
  1223. break;
  1224. case ETHTOOL_NWAY_RST:
  1225. rc = ethtool_nway_reset(dev);
  1226. break;
  1227. case ETHTOOL_GLINK:
  1228. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1229. dev->ethtool_ops->get_link);
  1230. break;
  1231. case ETHTOOL_GEEPROM:
  1232. rc = ethtool_get_eeprom(dev, useraddr);
  1233. break;
  1234. case ETHTOOL_SEEPROM:
  1235. rc = ethtool_set_eeprom(dev, useraddr);
  1236. break;
  1237. case ETHTOOL_GCOALESCE:
  1238. rc = ethtool_get_coalesce(dev, useraddr);
  1239. break;
  1240. case ETHTOOL_SCOALESCE:
  1241. rc = ethtool_set_coalesce(dev, useraddr);
  1242. break;
  1243. case ETHTOOL_GRINGPARAM:
  1244. rc = ethtool_get_ringparam(dev, useraddr);
  1245. break;
  1246. case ETHTOOL_SRINGPARAM:
  1247. rc = ethtool_set_ringparam(dev, useraddr);
  1248. break;
  1249. case ETHTOOL_GPAUSEPARAM:
  1250. rc = ethtool_get_pauseparam(dev, useraddr);
  1251. break;
  1252. case ETHTOOL_SPAUSEPARAM:
  1253. rc = ethtool_set_pauseparam(dev, useraddr);
  1254. break;
  1255. case ETHTOOL_GRXCSUM:
  1256. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1257. (dev->ethtool_ops->get_rx_csum ?
  1258. dev->ethtool_ops->get_rx_csum :
  1259. ethtool_op_get_rx_csum));
  1260. break;
  1261. case ETHTOOL_SRXCSUM:
  1262. rc = ethtool_set_rx_csum(dev, useraddr);
  1263. break;
  1264. case ETHTOOL_GTXCSUM:
  1265. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1266. (dev->ethtool_ops->get_tx_csum ?
  1267. dev->ethtool_ops->get_tx_csum :
  1268. ethtool_op_get_tx_csum));
  1269. break;
  1270. case ETHTOOL_STXCSUM:
  1271. rc = ethtool_set_tx_csum(dev, useraddr);
  1272. break;
  1273. case ETHTOOL_GSG:
  1274. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1275. (dev->ethtool_ops->get_sg ?
  1276. dev->ethtool_ops->get_sg :
  1277. ethtool_op_get_sg));
  1278. break;
  1279. case ETHTOOL_SSG:
  1280. rc = ethtool_set_sg(dev, useraddr);
  1281. break;
  1282. case ETHTOOL_GTSO:
  1283. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1284. (dev->ethtool_ops->get_tso ?
  1285. dev->ethtool_ops->get_tso :
  1286. ethtool_op_get_tso));
  1287. break;
  1288. case ETHTOOL_STSO:
  1289. rc = ethtool_set_tso(dev, useraddr);
  1290. break;
  1291. case ETHTOOL_TEST:
  1292. rc = ethtool_self_test(dev, useraddr);
  1293. break;
  1294. case ETHTOOL_GSTRINGS:
  1295. rc = ethtool_get_strings(dev, useraddr);
  1296. break;
  1297. case ETHTOOL_PHYS_ID:
  1298. rc = ethtool_phys_id(dev, useraddr);
  1299. break;
  1300. case ETHTOOL_GSTATS:
  1301. rc = ethtool_get_stats(dev, useraddr);
  1302. break;
  1303. case ETHTOOL_GPERMADDR:
  1304. rc = ethtool_get_perm_addr(dev, useraddr);
  1305. break;
  1306. case ETHTOOL_GUFO:
  1307. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1308. (dev->ethtool_ops->get_ufo ?
  1309. dev->ethtool_ops->get_ufo :
  1310. ethtool_op_get_ufo));
  1311. break;
  1312. case ETHTOOL_SUFO:
  1313. rc = ethtool_set_ufo(dev, useraddr);
  1314. break;
  1315. case ETHTOOL_GGSO:
  1316. rc = ethtool_get_gso(dev, useraddr);
  1317. break;
  1318. case ETHTOOL_SGSO:
  1319. rc = ethtool_set_gso(dev, useraddr);
  1320. break;
  1321. case ETHTOOL_GFLAGS:
  1322. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1323. (dev->ethtool_ops->get_flags ?
  1324. dev->ethtool_ops->get_flags :
  1325. ethtool_op_get_flags));
  1326. break;
  1327. case ETHTOOL_SFLAGS:
  1328. rc = ethtool_set_value(dev, useraddr,
  1329. dev->ethtool_ops->set_flags);
  1330. break;
  1331. case ETHTOOL_GPFLAGS:
  1332. rc = ethtool_get_value(dev, useraddr, ethcmd,
  1333. dev->ethtool_ops->get_priv_flags);
  1334. break;
  1335. case ETHTOOL_SPFLAGS:
  1336. rc = ethtool_set_value(dev, useraddr,
  1337. dev->ethtool_ops->set_priv_flags);
  1338. break;
  1339. case ETHTOOL_GRXFH:
  1340. case ETHTOOL_GRXRINGS:
  1341. case ETHTOOL_GRXCLSRLCNT:
  1342. case ETHTOOL_GRXCLSRULE:
  1343. case ETHTOOL_GRXCLSRLALL:
  1344. rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
  1345. break;
  1346. case ETHTOOL_SRXFH:
  1347. case ETHTOOL_SRXCLSRLDEL:
  1348. case ETHTOOL_SRXCLSRLINS:
  1349. rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
  1350. break;
  1351. case ETHTOOL_GGRO:
  1352. rc = ethtool_get_gro(dev, useraddr);
  1353. break;
  1354. case ETHTOOL_SGRO:
  1355. rc = ethtool_set_gro(dev, useraddr);
  1356. break;
  1357. case ETHTOOL_FLASHDEV:
  1358. rc = ethtool_flash_device(dev, useraddr);
  1359. break;
  1360. case ETHTOOL_RESET:
  1361. rc = ethtool_reset(dev, useraddr);
  1362. break;
  1363. case ETHTOOL_SRXNTUPLE:
  1364. rc = ethtool_set_rx_ntuple(dev, useraddr);
  1365. break;
  1366. case ETHTOOL_GRXNTUPLE:
  1367. rc = ethtool_get_rx_ntuple(dev, useraddr);
  1368. break;
  1369. case ETHTOOL_GSSET_INFO:
  1370. rc = ethtool_get_sset_info(dev, useraddr);
  1371. break;
  1372. case ETHTOOL_GRXFHINDIR:
  1373. rc = ethtool_get_rxfh_indir(dev, useraddr);
  1374. break;
  1375. case ETHTOOL_SRXFHINDIR:
  1376. rc = ethtool_set_rxfh_indir(dev, useraddr);
  1377. break;
  1378. default:
  1379. rc = -EOPNOTSUPP;
  1380. }
  1381. if (dev->ethtool_ops->complete)
  1382. dev->ethtool_ops->complete(dev);
  1383. if (old_features != dev->features)
  1384. netdev_features_change(dev);
  1385. return rc;
  1386. }