debugfs_netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  3. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/if.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/slab.h>
  16. #include <linux/notifier.h>
  17. #include <net/mac80211.h>
  18. #include <net/cfg80211.h>
  19. #include "ieee80211_i.h"
  20. #include "rate.h"
  21. #include "debugfs.h"
  22. #include "debugfs_netdev.h"
  23. static ssize_t ieee80211_if_read(
  24. struct ieee80211_sub_if_data *sdata,
  25. char __user *userbuf,
  26. size_t count, loff_t *ppos,
  27. ssize_t (*format)(const struct ieee80211_sub_if_data *, char *, int))
  28. {
  29. char buf[70];
  30. ssize_t ret = -EINVAL;
  31. read_lock(&dev_base_lock);
  32. if (sdata->dev->reg_state == NETREG_REGISTERED)
  33. ret = (*format)(sdata, buf, sizeof(buf));
  34. read_unlock(&dev_base_lock);
  35. if (ret != -EINVAL)
  36. ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
  37. return ret;
  38. }
  39. static ssize_t ieee80211_if_write(
  40. struct ieee80211_sub_if_data *sdata,
  41. const char __user *userbuf,
  42. size_t count, loff_t *ppos,
  43. ssize_t (*write)(struct ieee80211_sub_if_data *, const char *, int))
  44. {
  45. u8 *buf;
  46. ssize_t ret;
  47. buf = kmalloc(count, GFP_KERNEL);
  48. if (!buf)
  49. return -ENOMEM;
  50. ret = -EFAULT;
  51. if (copy_from_user(buf, userbuf, count))
  52. goto freebuf;
  53. ret = -ENODEV;
  54. rtnl_lock();
  55. if (sdata->dev->reg_state == NETREG_REGISTERED)
  56. ret = (*write)(sdata, buf, count);
  57. rtnl_unlock();
  58. freebuf:
  59. kfree(buf);
  60. return ret;
  61. }
  62. #define IEEE80211_IF_FMT(name, field, format_string) \
  63. static ssize_t ieee80211_if_fmt_##name( \
  64. const struct ieee80211_sub_if_data *sdata, char *buf, \
  65. int buflen) \
  66. { \
  67. return scnprintf(buf, buflen, format_string, sdata->field); \
  68. }
  69. #define IEEE80211_IF_FMT_DEC(name, field) \
  70. IEEE80211_IF_FMT(name, field, "%d\n")
  71. #define IEEE80211_IF_FMT_HEX(name, field) \
  72. IEEE80211_IF_FMT(name, field, "%#x\n")
  73. #define IEEE80211_IF_FMT_SIZE(name, field) \
  74. IEEE80211_IF_FMT(name, field, "%zd\n")
  75. #define IEEE80211_IF_FMT_ATOMIC(name, field) \
  76. static ssize_t ieee80211_if_fmt_##name( \
  77. const struct ieee80211_sub_if_data *sdata, \
  78. char *buf, int buflen) \
  79. { \
  80. return scnprintf(buf, buflen, "%d\n", atomic_read(&sdata->field));\
  81. }
  82. #define IEEE80211_IF_FMT_MAC(name, field) \
  83. static ssize_t ieee80211_if_fmt_##name( \
  84. const struct ieee80211_sub_if_data *sdata, char *buf, \
  85. int buflen) \
  86. { \
  87. return scnprintf(buf, buflen, "%pM\n", sdata->field); \
  88. }
  89. #define __IEEE80211_IF_FILE(name, _write) \
  90. static ssize_t ieee80211_if_read_##name(struct file *file, \
  91. char __user *userbuf, \
  92. size_t count, loff_t *ppos) \
  93. { \
  94. return ieee80211_if_read(file->private_data, \
  95. userbuf, count, ppos, \
  96. ieee80211_if_fmt_##name); \
  97. } \
  98. static const struct file_operations name##_ops = { \
  99. .read = ieee80211_if_read_##name, \
  100. .write = (_write), \
  101. .open = mac80211_open_file_generic, \
  102. }
  103. #define __IEEE80211_IF_FILE_W(name) \
  104. static ssize_t ieee80211_if_write_##name(struct file *file, \
  105. const char __user *userbuf, \
  106. size_t count, loff_t *ppos) \
  107. { \
  108. return ieee80211_if_write(file->private_data, userbuf, count, \
  109. ppos, ieee80211_if_parse_##name); \
  110. } \
  111. __IEEE80211_IF_FILE(name, ieee80211_if_write_##name)
  112. #define IEEE80211_IF_FILE(name, field, format) \
  113. IEEE80211_IF_FMT_##format(name, field) \
  114. __IEEE80211_IF_FILE(name, NULL)
  115. /* common attributes */
  116. IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
  117. IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[IEEE80211_BAND_2GHZ],
  118. HEX);
  119. IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[IEEE80211_BAND_5GHZ],
  120. HEX);
  121. /* STA attributes */
  122. IEEE80211_IF_FILE(bssid, u.mgd.bssid, MAC);
  123. IEEE80211_IF_FILE(aid, u.mgd.aid, DEC);
  124. static int ieee80211_set_smps(struct ieee80211_sub_if_data *sdata,
  125. enum ieee80211_smps_mode smps_mode)
  126. {
  127. struct ieee80211_local *local = sdata->local;
  128. int err;
  129. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_STATIC_SMPS) &&
  130. smps_mode == IEEE80211_SMPS_STATIC)
  131. return -EINVAL;
  132. /* auto should be dynamic if in PS mode */
  133. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS) &&
  134. (smps_mode == IEEE80211_SMPS_DYNAMIC ||
  135. smps_mode == IEEE80211_SMPS_AUTOMATIC))
  136. return -EINVAL;
  137. /* supported only on managed interfaces for now */
  138. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  139. return -EOPNOTSUPP;
  140. mutex_lock(&local->iflist_mtx);
  141. err = __ieee80211_request_smps(sdata, smps_mode);
  142. mutex_unlock(&local->iflist_mtx);
  143. return err;
  144. }
  145. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  146. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  147. [IEEE80211_SMPS_OFF] = "off",
  148. [IEEE80211_SMPS_STATIC] = "static",
  149. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  150. };
  151. static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_sub_if_data *sdata,
  152. char *buf, int buflen)
  153. {
  154. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  155. return -EOPNOTSUPP;
  156. return snprintf(buf, buflen, "request: %s\nused: %s\n",
  157. smps_modes[sdata->u.mgd.req_smps],
  158. smps_modes[sdata->u.mgd.ap_smps]);
  159. }
  160. static ssize_t ieee80211_if_parse_smps(struct ieee80211_sub_if_data *sdata,
  161. const char *buf, int buflen)
  162. {
  163. enum ieee80211_smps_mode mode;
  164. for (mode = 0; mode < IEEE80211_SMPS_NUM_MODES; mode++) {
  165. if (strncmp(buf, smps_modes[mode], buflen) == 0) {
  166. int err = ieee80211_set_smps(sdata, mode);
  167. if (!err)
  168. return buflen;
  169. return err;
  170. }
  171. }
  172. return -EINVAL;
  173. }
  174. __IEEE80211_IF_FILE_W(smps);
  175. /* AP attributes */
  176. IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
  177. IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);
  178. static ssize_t ieee80211_if_fmt_num_buffered_multicast(
  179. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  180. {
  181. return scnprintf(buf, buflen, "%u\n",
  182. skb_queue_len(&sdata->u.ap.ps_bc_buf));
  183. }
  184. __IEEE80211_IF_FILE(num_buffered_multicast, NULL);
  185. /* WDS attributes */
  186. IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);
  187. #ifdef CONFIG_MAC80211_MESH
  188. /* Mesh stats attributes */
  189. IEEE80211_IF_FILE(fwded_mcast, u.mesh.mshstats.fwded_mcast, DEC);
  190. IEEE80211_IF_FILE(fwded_unicast, u.mesh.mshstats.fwded_unicast, DEC);
  191. IEEE80211_IF_FILE(fwded_frames, u.mesh.mshstats.fwded_frames, DEC);
  192. IEEE80211_IF_FILE(dropped_frames_ttl, u.mesh.mshstats.dropped_frames_ttl, DEC);
  193. IEEE80211_IF_FILE(dropped_frames_no_route,
  194. u.mesh.mshstats.dropped_frames_no_route, DEC);
  195. IEEE80211_IF_FILE(estab_plinks, u.mesh.mshstats.estab_plinks, ATOMIC);
  196. /* Mesh parameters */
  197. IEEE80211_IF_FILE(dot11MeshMaxRetries,
  198. u.mesh.mshcfg.dot11MeshMaxRetries, DEC);
  199. IEEE80211_IF_FILE(dot11MeshRetryTimeout,
  200. u.mesh.mshcfg.dot11MeshRetryTimeout, DEC);
  201. IEEE80211_IF_FILE(dot11MeshConfirmTimeout,
  202. u.mesh.mshcfg.dot11MeshConfirmTimeout, DEC);
  203. IEEE80211_IF_FILE(dot11MeshHoldingTimeout,
  204. u.mesh.mshcfg.dot11MeshHoldingTimeout, DEC);
  205. IEEE80211_IF_FILE(dot11MeshTTL, u.mesh.mshcfg.dot11MeshTTL, DEC);
  206. IEEE80211_IF_FILE(auto_open_plinks, u.mesh.mshcfg.auto_open_plinks, DEC);
  207. IEEE80211_IF_FILE(dot11MeshMaxPeerLinks,
  208. u.mesh.mshcfg.dot11MeshMaxPeerLinks, DEC);
  209. IEEE80211_IF_FILE(dot11MeshHWMPactivePathTimeout,
  210. u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout, DEC);
  211. IEEE80211_IF_FILE(dot11MeshHWMPpreqMinInterval,
  212. u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval, DEC);
  213. IEEE80211_IF_FILE(dot11MeshHWMPnetDiameterTraversalTime,
  214. u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime, DEC);
  215. IEEE80211_IF_FILE(dot11MeshHWMPmaxPREQretries,
  216. u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries, DEC);
  217. IEEE80211_IF_FILE(path_refresh_time,
  218. u.mesh.mshcfg.path_refresh_time, DEC);
  219. IEEE80211_IF_FILE(min_discovery_timeout,
  220. u.mesh.mshcfg.min_discovery_timeout, DEC);
  221. IEEE80211_IF_FILE(dot11MeshHWMPRootMode,
  222. u.mesh.mshcfg.dot11MeshHWMPRootMode, DEC);
  223. #endif
  224. #define DEBUGFS_ADD(name) \
  225. debugfs_create_file(#name, 0400, sdata->debugfs.dir, \
  226. sdata, &name##_ops);
  227. #define DEBUGFS_ADD_MODE(name, mode) \
  228. debugfs_create_file(#name, mode, sdata->debugfs.dir, \
  229. sdata, &name##_ops);
  230. static void add_sta_files(struct ieee80211_sub_if_data *sdata)
  231. {
  232. DEBUGFS_ADD(drop_unencrypted);
  233. DEBUGFS_ADD(rc_rateidx_mask_2ghz);
  234. DEBUGFS_ADD(rc_rateidx_mask_5ghz);
  235. DEBUGFS_ADD(bssid);
  236. DEBUGFS_ADD(aid);
  237. DEBUGFS_ADD_MODE(smps, 0600);
  238. }
  239. static void add_ap_files(struct ieee80211_sub_if_data *sdata)
  240. {
  241. DEBUGFS_ADD(drop_unencrypted);
  242. DEBUGFS_ADD(rc_rateidx_mask_2ghz);
  243. DEBUGFS_ADD(rc_rateidx_mask_5ghz);
  244. DEBUGFS_ADD(num_sta_ps);
  245. DEBUGFS_ADD(dtim_count);
  246. DEBUGFS_ADD(num_buffered_multicast);
  247. }
  248. static void add_wds_files(struct ieee80211_sub_if_data *sdata)
  249. {
  250. DEBUGFS_ADD(drop_unencrypted);
  251. DEBUGFS_ADD(rc_rateidx_mask_2ghz);
  252. DEBUGFS_ADD(rc_rateidx_mask_5ghz);
  253. DEBUGFS_ADD(peer);
  254. }
  255. static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
  256. {
  257. DEBUGFS_ADD(drop_unencrypted);
  258. DEBUGFS_ADD(rc_rateidx_mask_2ghz);
  259. DEBUGFS_ADD(rc_rateidx_mask_5ghz);
  260. }
  261. static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
  262. {
  263. }
  264. #ifdef CONFIG_MAC80211_MESH
  265. static void add_mesh_stats(struct ieee80211_sub_if_data *sdata)
  266. {
  267. struct dentry *dir = debugfs_create_dir("mesh_stats",
  268. sdata->debugfs.dir);
  269. #define MESHSTATS_ADD(name)\
  270. debugfs_create_file(#name, 0400, dir, sdata, &name##_ops);
  271. MESHSTATS_ADD(fwded_mcast);
  272. MESHSTATS_ADD(fwded_unicast);
  273. MESHSTATS_ADD(fwded_frames);
  274. MESHSTATS_ADD(dropped_frames_ttl);
  275. MESHSTATS_ADD(dropped_frames_no_route);
  276. MESHSTATS_ADD(estab_plinks);
  277. #undef MESHSTATS_ADD
  278. }
  279. static void add_mesh_config(struct ieee80211_sub_if_data *sdata)
  280. {
  281. struct dentry *dir = debugfs_create_dir("mesh_config",
  282. sdata->debugfs.dir);
  283. #define MESHPARAMS_ADD(name) \
  284. debugfs_create_file(#name, 0600, dir, sdata, &name##_ops);
  285. MESHPARAMS_ADD(dot11MeshMaxRetries);
  286. MESHPARAMS_ADD(dot11MeshRetryTimeout);
  287. MESHPARAMS_ADD(dot11MeshConfirmTimeout);
  288. MESHPARAMS_ADD(dot11MeshHoldingTimeout);
  289. MESHPARAMS_ADD(dot11MeshTTL);
  290. MESHPARAMS_ADD(auto_open_plinks);
  291. MESHPARAMS_ADD(dot11MeshMaxPeerLinks);
  292. MESHPARAMS_ADD(dot11MeshHWMPactivePathTimeout);
  293. MESHPARAMS_ADD(dot11MeshHWMPpreqMinInterval);
  294. MESHPARAMS_ADD(dot11MeshHWMPnetDiameterTraversalTime);
  295. MESHPARAMS_ADD(dot11MeshHWMPmaxPREQretries);
  296. MESHPARAMS_ADD(path_refresh_time);
  297. MESHPARAMS_ADD(min_discovery_timeout);
  298. #undef MESHPARAMS_ADD
  299. }
  300. #endif
  301. static void add_files(struct ieee80211_sub_if_data *sdata)
  302. {
  303. if (!sdata->debugfs.dir)
  304. return;
  305. switch (sdata->vif.type) {
  306. case NL80211_IFTYPE_MESH_POINT:
  307. #ifdef CONFIG_MAC80211_MESH
  308. add_mesh_stats(sdata);
  309. add_mesh_config(sdata);
  310. #endif
  311. break;
  312. case NL80211_IFTYPE_STATION:
  313. add_sta_files(sdata);
  314. break;
  315. case NL80211_IFTYPE_ADHOC:
  316. /* XXX */
  317. break;
  318. case NL80211_IFTYPE_AP:
  319. add_ap_files(sdata);
  320. break;
  321. case NL80211_IFTYPE_WDS:
  322. add_wds_files(sdata);
  323. break;
  324. case NL80211_IFTYPE_MONITOR:
  325. add_monitor_files(sdata);
  326. break;
  327. case NL80211_IFTYPE_AP_VLAN:
  328. add_vlan_files(sdata);
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
  335. {
  336. char buf[10+IFNAMSIZ];
  337. sprintf(buf, "netdev:%s", sdata->name);
  338. sdata->debugfs.dir = debugfs_create_dir(buf,
  339. sdata->local->hw.wiphy->debugfsdir);
  340. add_files(sdata);
  341. }
  342. void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
  343. {
  344. if (!sdata->debugfs.dir)
  345. return;
  346. debugfs_remove_recursive(sdata->debugfs.dir);
  347. sdata->debugfs.dir = NULL;
  348. }
  349. void ieee80211_debugfs_rename_netdev(struct ieee80211_sub_if_data *sdata)
  350. {
  351. struct dentry *dir;
  352. char buf[10 + IFNAMSIZ];
  353. dir = sdata->debugfs.dir;
  354. if (!dir)
  355. return;
  356. sprintf(buf, "netdev:%s", sdata->name);
  357. if (!debugfs_rename(dir->d_parent, dir, dir->d_parent, buf))
  358. printk(KERN_ERR "mac80211: debugfs: failed to rename debugfs "
  359. "dir to %s\n", buf);
  360. }