debugfs_netdev.c 12 KB

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