debugfs_key.c 8.9 KB

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