debugfs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. DEBUGFS_READONLY_FILE(rts_threshold, 20, "%d",
  30. wiphy->rts_threshold);
  31. DEBUGFS_READONLY_FILE(fragmentation_threshold, 20, "%d",
  32. wiphy->frag_threshold);
  33. DEBUGFS_READONLY_FILE(short_retry_limit, 20, "%d",
  34. wiphy->retry_short);
  35. DEBUGFS_READONLY_FILE(long_retry_limit, 20, "%d",
  36. wiphy->retry_long);
  37. static int ht_print_chan(struct ieee80211_channel *chan,
  38. char *buf, int buf_size, int offset)
  39. {
  40. if (WARN_ON(offset > buf_size))
  41. return 0;
  42. if (chan->flags & IEEE80211_CHAN_DISABLED)
  43. return scnprintf(buf + offset,
  44. buf_size - offset,
  45. "%d Disabled\n",
  46. chan->center_freq);
  47. return scnprintf(buf + offset,
  48. buf_size - offset,
  49. "%d HT40 %c%c\n",
  50. chan->center_freq,
  51. (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) ?
  52. ' ' : '-',
  53. (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) ?
  54. ' ' : '+');
  55. }
  56. static ssize_t ht40allow_map_read(struct file *file,
  57. char __user *user_buf,
  58. size_t count, loff_t *ppos)
  59. {
  60. struct wiphy *wiphy = file->private_data;
  61. char *buf;
  62. unsigned int offset = 0, buf_size = PAGE_SIZE, i;
  63. enum nl80211_band band;
  64. struct ieee80211_supported_band *sband;
  65. ssize_t r;
  66. buf = kzalloc(buf_size, GFP_KERNEL);
  67. if (!buf)
  68. return -ENOMEM;
  69. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  70. sband = wiphy->bands[band];
  71. if (!sband)
  72. continue;
  73. for (i = 0; i < sband->n_channels; i++)
  74. offset += ht_print_chan(&sband->channels[i],
  75. buf, buf_size, offset);
  76. }
  77. r = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  78. kfree(buf);
  79. return r;
  80. }
  81. static const struct file_operations ht40allow_map_ops = {
  82. .read = ht40allow_map_read,
  83. .open = simple_open,
  84. .llseek = default_llseek,
  85. };
  86. #define DEBUGFS_ADD(name) \
  87. debugfs_create_file(#name, 0444, phyd, &rdev->wiphy, &name## _ops)
  88. void cfg80211_debugfs_rdev_add(struct cfg80211_registered_device *rdev)
  89. {
  90. struct dentry *phyd = rdev->wiphy.debugfsdir;
  91. DEBUGFS_ADD(rts_threshold);
  92. DEBUGFS_ADD(fragmentation_threshold);
  93. DEBUGFS_ADD(short_retry_limit);
  94. DEBUGFS_ADD(long_retry_limit);
  95. DEBUGFS_ADD(ht40allow_map);
  96. }
  97. struct debugfs_read_work {
  98. struct wiphy_work work;
  99. ssize_t (*handler)(struct wiphy *wiphy,
  100. struct file *file,
  101. char *buf,
  102. size_t count,
  103. void *data);
  104. struct wiphy *wiphy;
  105. struct file *file;
  106. char *buf;
  107. size_t bufsize;
  108. void *data;
  109. ssize_t ret;
  110. struct completion completion;
  111. };
  112. static void wiphy_locked_debugfs_read_work(struct wiphy *wiphy,
  113. struct wiphy_work *work)
  114. {
  115. struct debugfs_read_work *w = container_of(work, typeof(*w), work);
  116. w->ret = w->handler(w->wiphy, w->file, w->buf, w->bufsize, w->data);
  117. complete(&w->completion);
  118. }
  119. static void wiphy_locked_debugfs_read_cancel(struct dentry *dentry,
  120. void *data)
  121. {
  122. struct debugfs_read_work *w = data;
  123. wiphy_work_cancel(w->wiphy, &w->work);
  124. complete(&w->completion);
  125. }
  126. ssize_t wiphy_locked_debugfs_read(struct wiphy *wiphy, struct file *file,
  127. char *buf, size_t bufsize,
  128. char __user *userbuf, size_t count,
  129. loff_t *ppos,
  130. ssize_t (*handler)(struct wiphy *wiphy,
  131. struct file *file,
  132. char *buf,
  133. size_t bufsize,
  134. void *data),
  135. void *data)
  136. {
  137. struct debugfs_read_work work = {
  138. .handler = handler,
  139. .wiphy = wiphy,
  140. .file = file,
  141. .buf = buf,
  142. .bufsize = bufsize,
  143. .data = data,
  144. .ret = -ENODEV,
  145. .completion = COMPLETION_INITIALIZER_ONSTACK(work.completion),
  146. };
  147. struct debugfs_cancellation cancellation = {
  148. .cancel = wiphy_locked_debugfs_read_cancel,
  149. .cancel_data = &work,
  150. };
  151. /* don't leak stack data or whatever */
  152. memset(buf, 0, bufsize);
  153. wiphy_work_init(&work.work, wiphy_locked_debugfs_read_work);
  154. wiphy_work_queue(wiphy, &work.work);
  155. debugfs_enter_cancellation(file, &cancellation);
  156. wait_for_completion(&work.completion);
  157. debugfs_leave_cancellation(file, &cancellation);
  158. if (work.ret < 0)
  159. return work.ret;
  160. if (WARN_ON(work.ret > bufsize))
  161. return -EINVAL;
  162. return simple_read_from_buffer(userbuf, count, ppos, buf, work.ret);
  163. }
  164. EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_read);
  165. struct debugfs_write_work {
  166. struct wiphy_work work;
  167. ssize_t (*handler)(struct wiphy *wiphy,
  168. struct file *file,
  169. char *buf,
  170. size_t count,
  171. void *data);
  172. struct wiphy *wiphy;
  173. struct file *file;
  174. char *buf;
  175. size_t count;
  176. void *data;
  177. ssize_t ret;
  178. struct completion completion;
  179. };
  180. static void wiphy_locked_debugfs_write_work(struct wiphy *wiphy,
  181. struct wiphy_work *work)
  182. {
  183. struct debugfs_write_work *w = container_of(work, typeof(*w), work);
  184. w->ret = w->handler(w->wiphy, w->file, w->buf, w->count, w->data);
  185. complete(&w->completion);
  186. }
  187. static void wiphy_locked_debugfs_write_cancel(struct dentry *dentry,
  188. void *data)
  189. {
  190. struct debugfs_write_work *w = data;
  191. wiphy_work_cancel(w->wiphy, &w->work);
  192. complete(&w->completion);
  193. }
  194. ssize_t wiphy_locked_debugfs_write(struct wiphy *wiphy,
  195. struct file *file, char *buf, size_t bufsize,
  196. const char __user *userbuf, size_t count,
  197. ssize_t (*handler)(struct wiphy *wiphy,
  198. struct file *file,
  199. char *buf,
  200. size_t count,
  201. void *data),
  202. void *data)
  203. {
  204. struct debugfs_write_work work = {
  205. .handler = handler,
  206. .wiphy = wiphy,
  207. .file = file,
  208. .buf = buf,
  209. .count = count,
  210. .data = data,
  211. .ret = -ENODEV,
  212. .completion = COMPLETION_INITIALIZER_ONSTACK(work.completion),
  213. };
  214. struct debugfs_cancellation cancellation = {
  215. .cancel = wiphy_locked_debugfs_write_cancel,
  216. .cancel_data = &work,
  217. };
  218. /* mostly used for strings so enforce NUL-termination for safety */
  219. if (count >= bufsize)
  220. return -EINVAL;
  221. memset(buf, 0, bufsize);
  222. if (copy_from_user(buf, userbuf, count))
  223. return -EFAULT;
  224. wiphy_work_init(&work.work, wiphy_locked_debugfs_write_work);
  225. wiphy_work_queue(wiphy, &work.work);
  226. debugfs_enter_cancellation(file, &cancellation);
  227. wait_for_completion(&work.completion);
  228. debugfs_leave_cancellation(file, &cancellation);
  229. return work.ret;
  230. }
  231. EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_write);