debugfs_key.c 8.5 KB

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