ethtool.c 36 KB

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