debugfs_key.c 8.7 KB

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