debugfs_key.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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, 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. struct ieee80211_key *key = file->private_data; \
  22. return mac80211_format_buffer(userbuf, count, ppos, \
  23. format_string, key->prop); \
  24. }
  25. #define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
  26. #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
  27. #define KEY_OPS(name) \
  28. static const struct file_operations key_ ##name## _ops = { \
  29. .read = key_##name##_read, \
  30. .open = simple_open, \
  31. .llseek = generic_file_llseek, \
  32. }
  33. #define KEY_FILE(name, format) \
  34. KEY_READ_##format(name) \
  35. KEY_OPS(name)
  36. #define KEY_CONF_READ(name, format_string) \
  37. KEY_READ(conf_##name, conf.name, format_string)
  38. #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%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 = simple_open, \
  43. .llseek = generic_file_llseek, \
  44. }
  45. #define KEY_CONF_FILE(name, format) \
  46. KEY_CONF_READ_##format(name) \
  47. KEY_CONF_OPS(name)
  48. KEY_CONF_FILE(keylen, D);
  49. KEY_CONF_FILE(keyidx, D);
  50. KEY_CONF_FILE(hw_key_idx, D);
  51. KEY_FILE(flags, X);
  52. KEY_FILE(tx_rx_count, D);
  53. KEY_READ(ifindex, sdata->name, "%s\n");
  54. KEY_OPS(ifindex);
  55. static ssize_t key_algorithm_read(struct file *file,
  56. char __user *userbuf,
  57. size_t count, loff_t *ppos)
  58. {
  59. char buf[15];
  60. struct ieee80211_key *key = file->private_data;
  61. u32 c = key->conf.cipher;
  62. sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
  63. c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
  64. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  65. }
  66. KEY_OPS(algorithm);
  67. static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
  68. size_t count, loff_t *ppos)
  69. {
  70. u64 pn;
  71. char buf[20];
  72. int len;
  73. struct ieee80211_key *key = file->private_data;
  74. switch (key->conf.cipher) {
  75. case WLAN_CIPHER_SUITE_WEP40:
  76. case WLAN_CIPHER_SUITE_WEP104:
  77. len = scnprintf(buf, sizeof(buf), "\n");
  78. break;
  79. case WLAN_CIPHER_SUITE_TKIP:
  80. len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
  81. key->u.tkip.tx.iv32,
  82. key->u.tkip.tx.iv16);
  83. break;
  84. case WLAN_CIPHER_SUITE_CCMP:
  85. pn = atomic64_read(&key->u.ccmp.tx_pn);
  86. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  87. (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
  88. (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
  89. break;
  90. case WLAN_CIPHER_SUITE_AES_CMAC:
  91. pn = atomic64_read(&key->u.aes_cmac.tx_pn);
  92. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  93. (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
  94. (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
  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[100];
  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. sta = key->sta;
  219. if (sta) {
  220. sprintf(buf, "../../netdev:%s/stations/%pM",
  221. sta->sdata->name, sta->sta.addr);
  222. key->debugfs.stalink =
  223. debugfs_create_symlink("station", key->debugfs.dir, buf);
  224. }
  225. DEBUGFS_ADD(keylen);
  226. DEBUGFS_ADD(flags);
  227. DEBUGFS_ADD(keyidx);
  228. DEBUGFS_ADD(hw_key_idx);
  229. DEBUGFS_ADD(tx_rx_count);
  230. DEBUGFS_ADD(algorithm);
  231. DEBUGFS_ADD(tx_spec);
  232. DEBUGFS_ADD(rx_spec);
  233. DEBUGFS_ADD(replays);
  234. DEBUGFS_ADD(icverrors);
  235. DEBUGFS_ADD(key);
  236. DEBUGFS_ADD(ifindex);
  237. };
  238. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  239. {
  240. if (!key)
  241. return;
  242. debugfs_remove_recursive(key->debugfs.dir);
  243. key->debugfs.dir = NULL;
  244. }
  245. void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
  246. {
  247. char buf[50];
  248. struct ieee80211_key *key;
  249. if (!sdata->debugfs.dir)
  250. return;
  251. lockdep_assert_held(&sdata->local->key_mtx);
  252. if (sdata->default_unicast_key) {
  253. key = key_mtx_dereference(sdata->local,
  254. sdata->default_unicast_key);
  255. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  256. sdata->debugfs.default_unicast_key =
  257. debugfs_create_symlink("default_unicast_key",
  258. sdata->debugfs.dir, buf);
  259. } else {
  260. debugfs_remove(sdata->debugfs.default_unicast_key);
  261. sdata->debugfs.default_unicast_key = NULL;
  262. }
  263. if (sdata->default_multicast_key) {
  264. key = key_mtx_dereference(sdata->local,
  265. sdata->default_multicast_key);
  266. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  267. sdata->debugfs.default_multicast_key =
  268. debugfs_create_symlink("default_multicast_key",
  269. sdata->debugfs.dir, buf);
  270. } else {
  271. debugfs_remove(sdata->debugfs.default_multicast_key);
  272. sdata->debugfs.default_multicast_key = NULL;
  273. }
  274. }
  275. void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
  276. {
  277. char buf[50];
  278. struct ieee80211_key *key;
  279. if (!sdata->debugfs.dir)
  280. return;
  281. key = key_mtx_dereference(sdata->local,
  282. sdata->default_mgmt_key);
  283. if (key) {
  284. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  285. sdata->debugfs.default_mgmt_key =
  286. debugfs_create_symlink("default_mgmt_key",
  287. sdata->debugfs.dir, buf);
  288. } else
  289. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  290. }
  291. void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
  292. {
  293. if (!sdata)
  294. return;
  295. debugfs_remove(sdata->debugfs.default_mgmt_key);
  296. sdata->debugfs.default_mgmt_key = NULL;
  297. }
  298. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  299. struct sta_info *sta)
  300. {
  301. debugfs_remove(key->debugfs.stalink);
  302. key->debugfs.stalink = NULL;
  303. }