debugfs_key.c 8.4 KB

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