ethtool.c 37 KB

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