ethtool.c 40 KB

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