ethtool.c 40 KB

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