debugfs_key.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright 2003-2005 Devicescape Software, Inc.
  3. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kobject.h>
  11. #include <linux/slab.h>
  12. #include "ieee80211_i.h"
  13. #include "key.h"
  14. #include "debugfs.h"
  15. #include "debugfs_key.h"
  16. #define KEY_READ(name, prop, format_string) \
  17. static ssize_t key_##name##_read(struct file *file, \
  18. char __user *userbuf, \
  19. size_t count, loff_t *ppos) \
  20. { \
  21. struct ieee80211_key *key = file->private_data; \
  22. return mac80211_format_buffer(userbuf, count, ppos, \
  23. format_string, key->prop); \
  24. }
  25. #define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
  26. #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
  27. #define KEY_OPS(name) \
  28. static const struct file_operations key_ ##name## _ops = { \
  29. .read = key_##name##_read, \
  30. .open = mac80211_open_file_generic, \
  31. .llseek = generic_file_llseek, \
  32. }
  33. #define KEY_FILE(name, format) \
  34. KEY_READ_##format(name) \
  35. KEY_OPS(name)
  36. #define KEY_CONF_READ(name, format_string) \
  37. KEY_READ(conf_##name, conf.name, format_string)
  38. #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
  39. #define KEY_CONF_OPS(name) \
  40. static const struct file_operations key_ ##name## _ops = { \
  41. .read = key_conf_##name##_read, \
  42. .open = mac80211_open_file_generic, \
  43. .llseek = generic_file_llseek, \
  44. }
  45. #define KEY_CONF_FILE(name, format) \
  46. KEY_CONF_READ_##format(name) \
  47. KEY_CONF_OPS(name)
  48. KEY_CONF_FILE(keylen, D);
  49. KEY_CONF_FILE(keyidx, D);
  50. KEY_CONF_FILE(hw_key_idx, D);
  51. KEY_FILE(flags, X);
  52. KEY_FILE(tx_rx_count, D);
  53. KEY_READ(ifindex, sdata->name, "%s\n");
  54. KEY_OPS(ifindex);
  55. static ssize_t key_algorithm_read(struct file *file,
  56. char __user *userbuf,
  57. size_t count, loff_t *ppos)
  58. {
  59. char buf[15];
  60. struct ieee80211_key *key = file->private_data;
  61. u32 c = key->conf.cipher;
  62. sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
  63. c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
  64. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  65. }
  66. KEY_OPS(algorithm);
  67. static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
  68. size_t count, loff_t *ppos)
  69. {
  70. const u8 *tpn;
  71. char buf[20];
  72. int len;
  73. struct ieee80211_key *key = file->private_data;
  74. switch (key->conf.cipher) {
  75. case WLAN_CIPHER_SUITE_WEP40:
  76. case WLAN_CIPHER_SUITE_WEP104:
  77. len = scnprintf(buf, sizeof(buf), "\n");
  78. break;
  79. case WLAN_CIPHER_SUITE_TKIP:
  80. len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
  81. key->u.tkip.tx.iv32,
  82. key->u.tkip.tx.iv16);
  83. break;
  84. case WLAN_CIPHER_SUITE_CCMP:
  85. tpn = key->u.ccmp.tx_pn;
  86. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  87. tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
  88. break;
  89. case WLAN_CIPHER_SUITE_AES_CMAC:
  90. tpn = key->u.aes_cmac.tx_pn;
  91. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  92. tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
  93. tpn[5]);
  94. break;
  95. default:
  96. return 0;
  97. }
  98. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  99. }
  100. KEY_OPS(tx_spec);
  101. static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
  102. size_t count, loff_t *ppos)
  103. {
  104. struct ieee80211_key *key = file->private_data;
  105. char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
  106. int i, len;
  107. const u8 *rpn;
  108. switch (key->conf.cipher) {
  109. case WLAN_CIPHER_SUITE_WEP40:
  110. case WLAN_CIPHER_SUITE_WEP104:
  111. len = scnprintf(buf, sizeof(buf), "\n");
  112. break;
  113. case WLAN_CIPHER_SUITE_TKIP:
  114. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  115. p += scnprintf(p, sizeof(buf)+buf-p,
  116. "%08x %04x\n",
  117. key->u.tkip.rx[i].iv32,
  118. key->u.tkip.rx[i].iv16);
  119. len = p - buf;
  120. break;
  121. case WLAN_CIPHER_SUITE_CCMP:
  122. for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
  123. rpn = key->u.ccmp.rx_pn[i];
  124. p += scnprintf(p, sizeof(buf)+buf-p,
  125. "%02x%02x%02x%02x%02x%02x\n",
  126. rpn[0], rpn[1], rpn[2],
  127. rpn[3], rpn[4], rpn[5]);
  128. }
  129. len = p - buf;
  130. break;
  131. case WLAN_CIPHER_SUITE_AES_CMAC:
  132. rpn = key->u.aes_cmac.rx_pn;
  133. p += scnprintf(p, sizeof(buf)+buf-p,
  134. "%02x%02x%02x%02x%02x%02x\n",
  135. rpn[0], rpn[1], rpn[2],
  136. rpn[3], rpn[4], rpn[5]);
  137. len = p - buf;
  138. break;
  139. default:
  140. return 0;
  141. }
  142. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  143. }
  144. KEY_OPS(rx_spec);
  145. static ssize_t key_replays_read(struct file *file, char __user *userbuf,
  146. size_t count, loff_t *ppos)
  147. {
  148. struct ieee80211_key *key = file->private_data;
  149. char buf[20];
  150. int len;
  151. switch (key->conf.cipher) {
  152. case WLAN_CIPHER_SUITE_CCMP:
  153. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
  154. break;
  155. case WLAN_CIPHER_SUITE_AES_CMAC:
  156. len = scnprintf(buf, sizeof(buf), "%u\n",
  157. key->u.aes_cmac.replays);
  158. break;
  159. default:
  160. return 0;
  161. }
  162. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  163. }
  164. KEY_OPS(replays);
  165. static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
  166. size_t count, loff_t *ppos)
  167. {
  168. struct ieee80211_key *key = file->private_data;
  169. char buf[20];
  170. int len;
  171. switch (key->conf.cipher) {
  172. case WLAN_CIPHER_SUITE_AES_CMAC:
  173. len = scnprintf(buf, sizeof(buf), "%u\n",
  174. key->u.aes_cmac.icverrors);
  175. break;
  176. default:
  177. return 0;
  178. }
  179. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  180. }
  181. KEY_OPS(icverrors);
  182. static ssize_t key_key_read(struct file *file, char __user *userbuf,
  183. size_t count, loff_t *ppos)
  184. {
  185. struct ieee80211_key *key = file->private_data;
  186. int i, bufsize = 2 * key->conf.keylen + 2;
  187. char *buf = kmalloc(bufsize, GFP_KERNEL);
  188. char *p = buf;
  189. ssize_t res;
  190. if (!buf)
  191. return -ENOMEM;
  192. for (i = 0; i < key->conf.keylen; i++)
  193. p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
  194. p += scnprintf(p, bufsize+buf-p, "\n");
  195. res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  196. kfree(buf);
  197. return res;
  198. }
  199. KEY_OPS(key);
  200. #define DEBUGFS_ADD(name) \
  201. debugfs_create_file(#name, 0400, key->debugfs.dir, \
  202. key, &key_##name##_ops);
  203. void ieee80211_debugfs_key_add(struct ieee80211_key *key)
  204. {
  205. static int keycount;
  206. char buf[50];
  207. struct sta_info *sta;
  208. if (!key->local->debugfs.keys)
  209. return;
  210. sprintf(buf, "%d", keycount);
  211. key->debugfs.cnt = keycount;
  212. keycount++;
  213. key->debugfs.dir = debugfs_create_dir(buf,
  214. key->local->debugfs.keys);
  215. if (!key->debugfs.dir)
  216. return;
  217. sta = key->sta;
  218. if (sta) {
  219. sprintf(buf, "../../stations/%pM", sta->sta.addr);
  220. key->debugfs.stalink =
  221. debugfs_create_symlink("station", key->debugfs.dir, buf);
  222. }
  223. DEBUGFS_ADD(keylen);
  224. DEBUGFS_ADD(flags);
  225. DEBUGFS_ADD(keyidx);
  226. DEBUGFS_ADD(hw_key_idx);
  227. DEBUGFS_ADD(tx_rx_count);
  228. DEBUGFS_ADD(algorithm);
  229. DEBUGFS_ADD(tx_spec);
  230. DEBUGFS_ADD(rx_spec);
  231. DEBUGFS_ADD(replays);
  232. DEBUGFS_ADD(icverrors);
  233. DEBUGFS_ADD(key);
  234. DEBUGFS_ADD(ifindex);
  235. };
  236. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  237. {
  238. if (!key)
  239. return;
  240. debugfs_remove_recursive(key->debugfs.dir);
  241. key->debugfs.dir = NULL;
  242. }
  243. void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
  244. {
  245. char buf[50];
  246. struct ieee80211_key *key;
  247. if (!sdata->debugfs.dir)
  248. return;
  249. lockdep_assert_held(&sdata->local->key_mtx);
  250. if (sdata->default_unicast_key) {
  251. key = key_mtx_dereference(sdata->local,
  252. sdata->default_unicast_key);
  253. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  254. sdata->debugfs.default_unicast_key =
  255. debugfs_create_symlink("default_unicast_key",
  256. sdata->debugfs.dir, buf);
  257. } else {
  258. debugfs_remove(sdata->debugfs.default_unicast_key);
  259. sdata->debugfs.default_unicast_key = NULL;
  260. }
  261. if (sdata->default_multicast_key) {
  262. key = key_mtx_dereference(sdata->local,
  263. sdata->default_multicast_key);
  264. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  265. sdata->debugfs.default_multicast_key =
  266. debugfs_create_symlink("default_multicast_key",
  267. sdata->debugfs.dir, buf);
  268. } else {
  269. debugfs_remove(sdata->debugfs.default_multicast_key);
  270. sdata->debugfs.default_multicast_key = NULL;
  271. }
  272. }
  273. void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
  274. {
  275. char buf[50];
  276. struct ieee80211_key *key;
  277. if (!sdata->debugfs.dir)
  278. return;
  279. key = key_mtx_dereference(sdata->local,
  280. sdata->default_mgmt_key);
  281. if (key) {
  282. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  283. sdata->debugfs.default_mgmt_key =
  284. debugfs_create_symlink("default_mgmt_key",
  285. sdata->debugfs.dir, buf);
  286. } else
  287. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  288. }
  289. void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
  290. {
  291. if (!sdata)
  292. return;
  293. debugfs_remove(sdata->debugfs.default_mgmt_key);
  294. sdata->debugfs.default_mgmt_key = NULL;
  295. }
  296. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  297. struct sta_info *sta)
  298. {
  299. debugfs_remove(key->debugfs.stalink);
  300. key->debugfs.stalink = NULL;
  301. }