ethtool.c 37 KB

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