ethtool.c 40 KB

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