ethtool.c 36 KB

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