debugfs_key.c 8.6 KB

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