ethtool.c 35 KB

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