debugfs_netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "ieee80211_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. ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
  34. }
  35. read_unlock(&dev_base_lock);
  36. return ret;
  37. }
  38. #define IEEE80211_IF_FMT(name, field, format_string) \
  39. static ssize_t ieee80211_if_fmt_##name( \
  40. const struct ieee80211_sub_if_data *sdata, char *buf, \
  41. int buflen) \
  42. { \
  43. return scnprintf(buf, buflen, format_string, sdata->field); \
  44. }
  45. #define IEEE80211_IF_FMT_DEC(name, field) \
  46. IEEE80211_IF_FMT(name, field, "%d\n")
  47. #define IEEE80211_IF_FMT_HEX(name, field) \
  48. IEEE80211_IF_FMT(name, field, "%#x\n")
  49. #define IEEE80211_IF_FMT_SIZE(name, field) \
  50. IEEE80211_IF_FMT(name, field, "%zd\n")
  51. #define IEEE80211_IF_FMT_ATOMIC(name, field) \
  52. static ssize_t ieee80211_if_fmt_##name( \
  53. const struct ieee80211_sub_if_data *sdata, \
  54. char *buf, int buflen) \
  55. { \
  56. return scnprintf(buf, buflen, "%d\n", atomic_read(&sdata->field));\
  57. }
  58. #define IEEE80211_IF_FMT_MAC(name, field) \
  59. static ssize_t ieee80211_if_fmt_##name( \
  60. const struct ieee80211_sub_if_data *sdata, char *buf, \
  61. int buflen) \
  62. { \
  63. return scnprintf(buf, buflen, MAC_FMT "\n", MAC_ARG(sdata->field));\
  64. }
  65. #define __IEEE80211_IF_FILE(name) \
  66. static ssize_t ieee80211_if_read_##name(struct file *file, \
  67. char __user *userbuf, \
  68. size_t count, loff_t *ppos) \
  69. { \
  70. return ieee80211_if_read(file->private_data, \
  71. userbuf, count, ppos, \
  72. ieee80211_if_fmt_##name); \
  73. } \
  74. static const struct file_operations name##_ops = { \
  75. .read = ieee80211_if_read_##name, \
  76. .open = mac80211_open_file_generic, \
  77. }
  78. #define IEEE80211_IF_FILE(name, field, format) \
  79. IEEE80211_IF_FMT_##format(name, field) \
  80. __IEEE80211_IF_FILE(name)
  81. /* common attributes */
  82. IEEE80211_IF_FILE(channel_use, channel_use, DEC);
  83. IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
  84. IEEE80211_IF_FILE(eapol, eapol, DEC);
  85. IEEE80211_IF_FILE(ieee8021_x, ieee802_1x, DEC);
  86. /* STA/IBSS attributes */
  87. IEEE80211_IF_FILE(state, u.sta.state, DEC);
  88. IEEE80211_IF_FILE(bssid, u.sta.bssid, MAC);
  89. IEEE80211_IF_FILE(prev_bssid, u.sta.prev_bssid, MAC);
  90. IEEE80211_IF_FILE(ssid_len, u.sta.ssid_len, SIZE);
  91. IEEE80211_IF_FILE(aid, u.sta.aid, DEC);
  92. IEEE80211_IF_FILE(ap_capab, u.sta.ap_capab, HEX);
  93. IEEE80211_IF_FILE(capab, u.sta.capab, HEX);
  94. IEEE80211_IF_FILE(extra_ie_len, u.sta.extra_ie_len, SIZE);
  95. IEEE80211_IF_FILE(auth_tries, u.sta.auth_tries, DEC);
  96. IEEE80211_IF_FILE(assoc_tries, u.sta.assoc_tries, DEC);
  97. IEEE80211_IF_FILE(auth_algs, u.sta.auth_algs, HEX);
  98. IEEE80211_IF_FILE(auth_alg, u.sta.auth_alg, DEC);
  99. IEEE80211_IF_FILE(auth_transaction, u.sta.auth_transaction, DEC);
  100. static ssize_t ieee80211_if_fmt_flags(
  101. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  102. {
  103. return scnprintf(buf, buflen, "%s%s%s%s%s%s%s\n",
  104. sdata->u.sta.ssid_set ? "SSID\n" : "",
  105. sdata->u.sta.bssid_set ? "BSSID\n" : "",
  106. sdata->u.sta.prev_bssid_set ? "prev BSSID\n" : "",
  107. sdata->u.sta.authenticated ? "AUTH\n" : "",
  108. sdata->u.sta.associated ? "ASSOC\n" : "",
  109. sdata->u.sta.probereq_poll ? "PROBEREQ POLL\n" : "",
  110. sdata->use_protection ? "CTS prot\n" : "");
  111. }
  112. __IEEE80211_IF_FILE(flags);
  113. /* AP attributes */
  114. IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
  115. IEEE80211_IF_FILE(dtim_period, u.ap.dtim_period, DEC);
  116. IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);
  117. IEEE80211_IF_FILE(num_beacons, u.ap.num_beacons, DEC);
  118. IEEE80211_IF_FILE(force_unicast_rateidx, u.ap.force_unicast_rateidx, DEC);
  119. IEEE80211_IF_FILE(max_ratectrl_rateidx, u.ap.max_ratectrl_rateidx, DEC);
  120. static ssize_t ieee80211_if_fmt_num_buffered_multicast(
  121. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  122. {
  123. return scnprintf(buf, buflen, "%u\n",
  124. skb_queue_len(&sdata->u.ap.ps_bc_buf));
  125. }
  126. __IEEE80211_IF_FILE(num_buffered_multicast);
  127. static ssize_t ieee80211_if_fmt_beacon_head_len(
  128. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  129. {
  130. if (sdata->u.ap.beacon_head)
  131. return scnprintf(buf, buflen, "%d\n",
  132. sdata->u.ap.beacon_head_len);
  133. return scnprintf(buf, buflen, "\n");
  134. }
  135. __IEEE80211_IF_FILE(beacon_head_len);
  136. static ssize_t ieee80211_if_fmt_beacon_tail_len(
  137. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  138. {
  139. if (sdata->u.ap.beacon_tail)
  140. return scnprintf(buf, buflen, "%d\n",
  141. sdata->u.ap.beacon_tail_len);
  142. return scnprintf(buf, buflen, "\n");
  143. }
  144. __IEEE80211_IF_FILE(beacon_tail_len);
  145. /* WDS attributes */
  146. IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);
  147. /* VLAN attributes */
  148. IEEE80211_IF_FILE(vlan_id, u.vlan.id, DEC);
  149. /* MONITOR attributes */
  150. static ssize_t ieee80211_if_fmt_mode(
  151. const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
  152. {
  153. struct ieee80211_local *local = sdata->local;
  154. return scnprintf(buf, buflen, "%s\n",
  155. ((local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) ||
  156. local->open_count == local->monitors) ?
  157. "hard" : "soft");
  158. }
  159. __IEEE80211_IF_FILE(mode);
  160. #define DEBUGFS_ADD(name, type)\
  161. sdata->debugfs.type.name = debugfs_create_file(#name, 0444,\
  162. sdata->debugfsdir, sdata, &name##_ops);
  163. static void add_sta_files(struct ieee80211_sub_if_data *sdata)
  164. {
  165. DEBUGFS_ADD(channel_use, sta);
  166. DEBUGFS_ADD(drop_unencrypted, sta);
  167. DEBUGFS_ADD(eapol, sta);
  168. DEBUGFS_ADD(ieee8021_x, sta);
  169. DEBUGFS_ADD(state, sta);
  170. DEBUGFS_ADD(bssid, sta);
  171. DEBUGFS_ADD(prev_bssid, sta);
  172. DEBUGFS_ADD(ssid_len, sta);
  173. DEBUGFS_ADD(aid, sta);
  174. DEBUGFS_ADD(ap_capab, sta);
  175. DEBUGFS_ADD(capab, sta);
  176. DEBUGFS_ADD(extra_ie_len, sta);
  177. DEBUGFS_ADD(auth_tries, sta);
  178. DEBUGFS_ADD(assoc_tries, sta);
  179. DEBUGFS_ADD(auth_algs, sta);
  180. DEBUGFS_ADD(auth_alg, sta);
  181. DEBUGFS_ADD(auth_transaction, sta);
  182. DEBUGFS_ADD(flags, sta);
  183. }
  184. static void add_ap_files(struct ieee80211_sub_if_data *sdata)
  185. {
  186. DEBUGFS_ADD(channel_use, ap);
  187. DEBUGFS_ADD(drop_unencrypted, ap);
  188. DEBUGFS_ADD(eapol, ap);
  189. DEBUGFS_ADD(ieee8021_x, ap);
  190. DEBUGFS_ADD(num_sta_ps, ap);
  191. DEBUGFS_ADD(dtim_period, ap);
  192. DEBUGFS_ADD(dtim_count, ap);
  193. DEBUGFS_ADD(num_beacons, ap);
  194. DEBUGFS_ADD(force_unicast_rateidx, ap);
  195. DEBUGFS_ADD(max_ratectrl_rateidx, ap);
  196. DEBUGFS_ADD(num_buffered_multicast, ap);
  197. DEBUGFS_ADD(beacon_head_len, ap);
  198. DEBUGFS_ADD(beacon_tail_len, ap);
  199. }
  200. static void add_wds_files(struct ieee80211_sub_if_data *sdata)
  201. {
  202. DEBUGFS_ADD(channel_use, wds);
  203. DEBUGFS_ADD(drop_unencrypted, wds);
  204. DEBUGFS_ADD(eapol, wds);
  205. DEBUGFS_ADD(ieee8021_x, wds);
  206. DEBUGFS_ADD(peer, wds);
  207. }
  208. static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
  209. {
  210. DEBUGFS_ADD(channel_use, vlan);
  211. DEBUGFS_ADD(drop_unencrypted, vlan);
  212. DEBUGFS_ADD(eapol, vlan);
  213. DEBUGFS_ADD(ieee8021_x, vlan);
  214. DEBUGFS_ADD(vlan_id, vlan);
  215. }
  216. static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
  217. {
  218. DEBUGFS_ADD(mode, monitor);
  219. }
  220. static void add_files(struct ieee80211_sub_if_data *sdata)
  221. {
  222. if (!sdata->debugfsdir)
  223. return;
  224. switch (sdata->type) {
  225. case IEEE80211_IF_TYPE_STA:
  226. case IEEE80211_IF_TYPE_IBSS:
  227. add_sta_files(sdata);
  228. break;
  229. case IEEE80211_IF_TYPE_AP:
  230. add_ap_files(sdata);
  231. break;
  232. case IEEE80211_IF_TYPE_WDS:
  233. add_wds_files(sdata);
  234. break;
  235. case IEEE80211_IF_TYPE_MNTR:
  236. add_monitor_files(sdata);
  237. break;
  238. case IEEE80211_IF_TYPE_VLAN:
  239. add_vlan_files(sdata);
  240. break;
  241. default:
  242. break;
  243. }
  244. }
  245. #define DEBUGFS_DEL(name, type)\
  246. debugfs_remove(sdata->debugfs.type.name);\
  247. sdata->debugfs.type.name = NULL;
  248. static void del_sta_files(struct ieee80211_sub_if_data *sdata)
  249. {
  250. DEBUGFS_DEL(channel_use, sta);
  251. DEBUGFS_DEL(drop_unencrypted, sta);
  252. DEBUGFS_DEL(eapol, sta);
  253. DEBUGFS_DEL(ieee8021_x, sta);
  254. DEBUGFS_DEL(state, sta);
  255. DEBUGFS_DEL(bssid, sta);
  256. DEBUGFS_DEL(prev_bssid, sta);
  257. DEBUGFS_DEL(ssid_len, sta);
  258. DEBUGFS_DEL(aid, sta);
  259. DEBUGFS_DEL(ap_capab, sta);
  260. DEBUGFS_DEL(capab, sta);
  261. DEBUGFS_DEL(extra_ie_len, sta);
  262. DEBUGFS_DEL(auth_tries, sta);
  263. DEBUGFS_DEL(assoc_tries, sta);
  264. DEBUGFS_DEL(auth_algs, sta);
  265. DEBUGFS_DEL(auth_alg, sta);
  266. DEBUGFS_DEL(auth_transaction, sta);
  267. DEBUGFS_DEL(flags, sta);
  268. }
  269. static void del_ap_files(struct ieee80211_sub_if_data *sdata)
  270. {
  271. DEBUGFS_DEL(channel_use, ap);
  272. DEBUGFS_DEL(drop_unencrypted, ap);
  273. DEBUGFS_DEL(eapol, ap);
  274. DEBUGFS_DEL(ieee8021_x, ap);
  275. DEBUGFS_DEL(num_sta_ps, ap);
  276. DEBUGFS_DEL(dtim_period, ap);
  277. DEBUGFS_DEL(dtim_count, ap);
  278. DEBUGFS_DEL(num_beacons, ap);
  279. DEBUGFS_DEL(force_unicast_rateidx, ap);
  280. DEBUGFS_DEL(max_ratectrl_rateidx, ap);
  281. DEBUGFS_DEL(num_buffered_multicast, ap);
  282. DEBUGFS_DEL(beacon_head_len, ap);
  283. DEBUGFS_DEL(beacon_tail_len, ap);
  284. }
  285. static void del_wds_files(struct ieee80211_sub_if_data *sdata)
  286. {
  287. DEBUGFS_DEL(channel_use, wds);
  288. DEBUGFS_DEL(drop_unencrypted, wds);
  289. DEBUGFS_DEL(eapol, wds);
  290. DEBUGFS_DEL(ieee8021_x, wds);
  291. DEBUGFS_DEL(peer, wds);
  292. }
  293. static void del_vlan_files(struct ieee80211_sub_if_data *sdata)
  294. {
  295. DEBUGFS_DEL(channel_use, vlan);
  296. DEBUGFS_DEL(drop_unencrypted, vlan);
  297. DEBUGFS_DEL(eapol, vlan);
  298. DEBUGFS_DEL(ieee8021_x, vlan);
  299. DEBUGFS_DEL(vlan_id, vlan);
  300. }
  301. static void del_monitor_files(struct ieee80211_sub_if_data *sdata)
  302. {
  303. DEBUGFS_DEL(mode, monitor);
  304. }
  305. static void del_files(struct ieee80211_sub_if_data *sdata, int type)
  306. {
  307. if (!sdata->debugfsdir)
  308. return;
  309. switch (type) {
  310. case IEEE80211_IF_TYPE_STA:
  311. case IEEE80211_IF_TYPE_IBSS:
  312. del_sta_files(sdata);
  313. break;
  314. case IEEE80211_IF_TYPE_AP:
  315. del_ap_files(sdata);
  316. break;
  317. case IEEE80211_IF_TYPE_WDS:
  318. del_wds_files(sdata);
  319. break;
  320. case IEEE80211_IF_TYPE_MNTR:
  321. del_monitor_files(sdata);
  322. break;
  323. case IEEE80211_IF_TYPE_VLAN:
  324. del_vlan_files(sdata);
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. static int notif_registered;
  331. void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
  332. {
  333. char buf[10+IFNAMSIZ];
  334. if (!notif_registered)
  335. return;
  336. sprintf(buf, "netdev:%s", sdata->dev->name);
  337. sdata->debugfsdir = debugfs_create_dir(buf,
  338. sdata->local->hw.wiphy->debugfsdir);
  339. }
  340. void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
  341. {
  342. del_files(sdata, sdata->type);
  343. debugfs_remove(sdata->debugfsdir);
  344. sdata->debugfsdir = NULL;
  345. }
  346. void ieee80211_debugfs_change_if_type(struct ieee80211_sub_if_data *sdata,
  347. int oldtype)
  348. {
  349. del_files(sdata, oldtype);
  350. add_files(sdata);
  351. }
  352. static int netdev_notify(struct notifier_block * nb,
  353. unsigned long state,
  354. void *ndev)
  355. {
  356. struct net_device *dev = ndev;
  357. char buf[10+IFNAMSIZ];
  358. if (state != NETDEV_CHANGENAME)
  359. return 0;
  360. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  361. return 0;
  362. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  363. return 0;
  364. /* TODO
  365. sprintf(buf, "netdev:%s", dev->name);
  366. debugfs_rename(IEEE80211_DEV_TO_SUB_IF(dev)->debugfsdir, buf);
  367. */
  368. return 0;
  369. }
  370. static struct notifier_block mac80211_debugfs_netdev_notifier = {
  371. .notifier_call = netdev_notify,
  372. };
  373. void ieee80211_debugfs_netdev_init(void)
  374. {
  375. int err;
  376. err = register_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
  377. if (err) {
  378. printk(KERN_ERR
  379. "mac80211: failed to install netdev notifier,"
  380. " disabling per-netdev debugfs!\n");
  381. } else
  382. notif_registered = 1;
  383. }
  384. void ieee80211_debugfs_netdev_exit(void)
  385. {
  386. unregister_netdevice_notifier(&mac80211_debugfs_netdev_notifier);
  387. notif_registered = 0;
  388. }