ethtool.c 38 KB

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