ethtool.c 36 KB

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