ethtool.c 39 KB

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