ethtool.c 38 KB

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