debugfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cfg80211 debugfs
  4. *
  5. * Copyright 2009 Luis R. Rodriguez <lrodriguez@atheros.com>
  6. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright (C) 2023 Intel Corporation
  8. */
  9. #include <linux/slab.h>
  10. #include "core.h"
  11. #include "debugfs.h"
  12. #define DEBUGFS_READONLY_FILE(name, buflen, fmt, value...) \
  13. static ssize_t name## _read(struct file *file, char __user *userbuf, \
  14. size_t count, loff_t *ppos) \
  15. { \
  16. struct wiphy *wiphy = file->private_data; \
  17. char buf[buflen]; \
  18. int res; \
  19. \
  20. res = scnprintf(buf, buflen, fmt "\n", ##value); \
  21. return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
  22. } \
  23. \
  24. static const struct file_operations name## _ops = { \
  25. .read = name## _read, \
  26. .open = simple_open, \
  27. .llseek = generic_file_llseek, \
  28. }
  29. #define DEBUGFS_RADIO_READONLY_FILE(name, buflen, fmt, value...) \
  30. static ssize_t name## _read(struct file *file, char __user *userbuf, \
  31. size_t count, loff_t *ppos) \
  32. { \
  33. struct wiphy_radio_cfg *radio_cfg = file->private_data; \
  34. char buf[buflen]; \
  35. int res; \
  36. \
  37. res = scnprintf(buf, buflen, fmt "\n", ##value); \
  38. return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
  39. } \
  40. \
  41. static const struct file_operations name## _ops = { \
  42. .read = name## _read, \
  43. .open = simple_open, \
  44. .llseek = generic_file_llseek, \
  45. }
  46. DEBUGFS_READONLY_FILE(rts_threshold, 20, "%d",
  47. wiphy->rts_threshold);
  48. DEBUGFS_READONLY_FILE(fragmentation_threshold, 20, "%d",
  49. wiphy->frag_threshold);
  50. DEBUGFS_READONLY_FILE(short_retry_limit, 20, "%d",
  51. wiphy->retry_short);
  52. DEBUGFS_READONLY_FILE(long_retry_limit, 20, "%d",
  53. wiphy->retry_long);
  54. DEBUGFS_RADIO_READONLY_FILE(radio_rts_threshold, 20, "%d",
  55. radio_cfg->rts_threshold);
  56. static int ht_print_chan(struct ieee80211_channel *chan,
  57. char *buf, int buf_size, int offset)
  58. {
  59. if (WARN_ON(offset > buf_size))
  60. return 0;
  61. if (chan->flags & IEEE80211_CHAN_DISABLED)
  62. return scnprintf(buf + offset,
  63. buf_size - offset,
  64. "%d Disabled\n",
  65. chan->center_freq);
  66. return scnprintf(buf + offset,
  67. buf_size - offset,
  68. "%d HT40 %c%c\n",
  69. chan->center_freq,
  70. (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) ?
  71. ' ' : '-',
  72. (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) ?
  73. ' ' : '+');
  74. }
  75. static ssize_t ht40allow_map_read(struct file *file,
  76. char __user *user_buf,
  77. size_t count, loff_t *ppos)
  78. {
  79. struct wiphy *wiphy = file->private_data;
  80. char *buf;
  81. unsigned int offset = 0, buf_size = PAGE_SIZE, i;
  82. enum nl80211_band band;
  83. struct ieee80211_supported_band *sband;
  84. ssize_t r;
  85. buf = kzalloc(buf_size, GFP_KERNEL);
  86. if (!buf)
  87. return -ENOMEM;
  88. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  89. sband = wiphy->bands[band];
  90. if (!sband)
  91. continue;
  92. for (i = 0; i < sband->n_channels; i++)
  93. offset += ht_print_chan(&sband->channels[i],
  94. buf, buf_size, offset);
  95. }
  96. r = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  97. kfree(buf);
  98. return r;
  99. }
  100. static const struct file_operations ht40allow_map_ops = {
  101. .read = ht40allow_map_read,
  102. .open = simple_open,
  103. .llseek = default_llseek,
  104. };
  105. #define DEBUGFS_ADD(name) \
  106. debugfs_create_file(#name, 0444, phyd, &rdev->wiphy, &name## _ops)
  107. #define DEBUGFS_RADIO_ADD(name, radio_idx) \
  108. debugfs_create_file(#name, 0444, radiod, \
  109. &rdev->wiphy.radio_cfg[radio_idx], \
  110. &name## _ops)
  111. void cfg80211_debugfs_rdev_add(struct cfg80211_registered_device *rdev)
  112. {
  113. struct dentry *phyd = rdev->wiphy.debugfsdir;
  114. struct dentry *radiod;
  115. u8 i;
  116. DEBUGFS_ADD(rts_threshold);
  117. DEBUGFS_ADD(fragmentation_threshold);
  118. DEBUGFS_ADD(short_retry_limit);
  119. DEBUGFS_ADD(long_retry_limit);
  120. DEBUGFS_ADD(ht40allow_map);
  121. for (i = 0; i < rdev->wiphy.n_radio; i++) {
  122. radiod = rdev->wiphy.radio_cfg[i].radio_debugfsdir;
  123. DEBUGFS_RADIO_ADD(radio_rts_threshold, i);
  124. }
  125. }
  126. struct debugfs_read_work {
  127. struct wiphy_work work;
  128. ssize_t (*handler)(struct wiphy *wiphy,
  129. struct file *file,
  130. char *buf,
  131. size_t count,
  132. void *data);
  133. struct wiphy *wiphy;
  134. struct file *file;
  135. char *buf;
  136. size_t bufsize;
  137. void *data;
  138. ssize_t ret;
  139. struct completion completion;
  140. };
  141. static void wiphy_locked_debugfs_read_work(struct wiphy *wiphy,
  142. struct wiphy_work *work)
  143. {
  144. struct debugfs_read_work *w = container_of(work, typeof(*w), work);
  145. w->ret = w->handler(w->wiphy, w->file, w->buf, w->bufsize, w->data);
  146. complete(&w->completion);
  147. }
  148. static void wiphy_locked_debugfs_read_cancel(struct dentry *dentry,
  149. void *data)
  150. {
  151. struct debugfs_read_work *w = data;
  152. wiphy_work_cancel(w->wiphy, &w->work);
  153. complete(&w->completion);
  154. }
  155. ssize_t wiphy_locked_debugfs_read(struct wiphy *wiphy, struct file *file,
  156. char *buf, size_t bufsize,
  157. char __user *userbuf, size_t count,
  158. loff_t *ppos,
  159. ssize_t (*handler)(struct wiphy *wiphy,
  160. struct file *file,
  161. char *buf,
  162. size_t bufsize,
  163. void *data),
  164. void *data)
  165. {
  166. struct debugfs_read_work work = {
  167. .handler = handler,
  168. .wiphy = wiphy,
  169. .file = file,
  170. .buf = buf,
  171. .bufsize = bufsize,
  172. .data = data,
  173. .ret = -ENODEV,
  174. .completion = COMPLETION_INITIALIZER_ONSTACK(work.completion),
  175. };
  176. struct debugfs_cancellation cancellation = {
  177. .cancel = wiphy_locked_debugfs_read_cancel,
  178. .cancel_data = &work,
  179. };
  180. /* don't leak stack data or whatever */
  181. memset(buf, 0, bufsize);
  182. wiphy_work_init(&work.work, wiphy_locked_debugfs_read_work);
  183. wiphy_work_queue(wiphy, &work.work);
  184. debugfs_enter_cancellation(file, &cancellation);
  185. wait_for_completion(&work.completion);
  186. debugfs_leave_cancellation(file, &cancellation);
  187. if (work.ret < 0)
  188. return work.ret;
  189. if (WARN_ON(work.ret > bufsize))
  190. return -EINVAL;
  191. return simple_read_from_buffer(userbuf, count, ppos, buf, work.ret);
  192. }
  193. EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_read);
  194. struct debugfs_write_work {
  195. struct wiphy_work work;
  196. ssize_t (*handler)(struct wiphy *wiphy,
  197. struct file *file,
  198. char *buf,
  199. size_t count,
  200. void *data);
  201. struct wiphy *wiphy;
  202. struct file *file;
  203. char *buf;
  204. size_t count;
  205. void *data;
  206. ssize_t ret;
  207. struct completion completion;
  208. };
  209. static void wiphy_locked_debugfs_write_work(struct wiphy *wiphy,
  210. struct wiphy_work *work)
  211. {
  212. struct debugfs_write_work *w = container_of(work, typeof(*w), work);
  213. w->ret = w->handler(w->wiphy, w->file, w->buf, w->count, w->data);
  214. complete(&w->completion);
  215. }
  216. static void wiphy_locked_debugfs_write_cancel(struct dentry *dentry,
  217. void *data)
  218. {
  219. struct debugfs_write_work *w = data;
  220. wiphy_work_cancel(w->wiphy, &w->work);
  221. complete(&w->completion);
  222. }
  223. ssize_t wiphy_locked_debugfs_write(struct wiphy *wiphy,
  224. struct file *file, char *buf, size_t bufsize,
  225. const char __user *userbuf, size_t count,
  226. ssize_t (*handler)(struct wiphy *wiphy,
  227. struct file *file,
  228. char *buf,
  229. size_t count,
  230. void *data),
  231. void *data)
  232. {
  233. struct debugfs_write_work work = {
  234. .handler = handler,
  235. .wiphy = wiphy,
  236. .file = file,
  237. .buf = buf,
  238. .count = count,
  239. .data = data,
  240. .ret = -ENODEV,
  241. .completion = COMPLETION_INITIALIZER_ONSTACK(work.completion),
  242. };
  243. struct debugfs_cancellation cancellation = {
  244. .cancel = wiphy_locked_debugfs_write_cancel,
  245. .cancel_data = &work,
  246. };
  247. /* mostly used for strings so enforce NUL-termination for safety */
  248. if (count >= bufsize)
  249. return -EINVAL;
  250. memset(buf, 0, bufsize);
  251. if (copy_from_user(buf, userbuf, count))
  252. return -EFAULT;
  253. wiphy_work_init(&work.work, wiphy_locked_debugfs_write_work);
  254. wiphy_work_queue(wiphy, &work.work);
  255. debugfs_enter_cancellation(file, &cancellation);
  256. wait_for_completion(&work.completion);
  257. debugfs_leave_cancellation(file, &cancellation);
  258. return work.ret;
  259. }
  260. EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_write);