debugfs_key.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. case WLAN_CIPHER_SUITE_CCMP_256:
  86. case WLAN_CIPHER_SUITE_AES_CMAC:
  87. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  88. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  89. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  90. case WLAN_CIPHER_SUITE_GCMP:
  91. case WLAN_CIPHER_SUITE_GCMP_256:
  92. pn = atomic64_read(&key->conf.tx_pn);
  93. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  94. (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
  95. (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
  96. break;
  97. default:
  98. return 0;
  99. }
  100. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  101. }
  102. KEY_OPS(tx_spec);
  103. static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
  104. size_t count, loff_t *ppos)
  105. {
  106. struct ieee80211_key *key = file->private_data;
  107. char buf[14*IEEE80211_NUM_TIDS+1], *p = buf;
  108. int i, len;
  109. const u8 *rpn;
  110. switch (key->conf.cipher) {
  111. case WLAN_CIPHER_SUITE_WEP40:
  112. case WLAN_CIPHER_SUITE_WEP104:
  113. len = scnprintf(buf, sizeof(buf), "\n");
  114. break;
  115. case WLAN_CIPHER_SUITE_TKIP:
  116. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  117. p += scnprintf(p, sizeof(buf)+buf-p,
  118. "%08x %04x\n",
  119. key->u.tkip.rx[i].iv32,
  120. key->u.tkip.rx[i].iv16);
  121. len = p - buf;
  122. break;
  123. case WLAN_CIPHER_SUITE_CCMP:
  124. case WLAN_CIPHER_SUITE_CCMP_256:
  125. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  126. rpn = key->u.ccmp.rx_pn[i];
  127. p += scnprintf(p, sizeof(buf)+buf-p,
  128. "%02x%02x%02x%02x%02x%02x\n",
  129. rpn[0], rpn[1], rpn[2],
  130. rpn[3], rpn[4], rpn[5]);
  131. }
  132. len = p - buf;
  133. break;
  134. case WLAN_CIPHER_SUITE_AES_CMAC:
  135. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  136. rpn = key->u.aes_cmac.rx_pn;
  137. p += scnprintf(p, sizeof(buf)+buf-p,
  138. "%02x%02x%02x%02x%02x%02x\n",
  139. rpn[0], rpn[1], rpn[2],
  140. rpn[3], rpn[4], rpn[5]);
  141. len = p - buf;
  142. break;
  143. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  144. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  145. rpn = key->u.aes_gmac.rx_pn;
  146. p += scnprintf(p, sizeof(buf)+buf-p,
  147. "%02x%02x%02x%02x%02x%02x\n",
  148. rpn[0], rpn[1], rpn[2],
  149. rpn[3], rpn[4], rpn[5]);
  150. len = p - buf;
  151. break;
  152. case WLAN_CIPHER_SUITE_GCMP:
  153. case WLAN_CIPHER_SUITE_GCMP_256:
  154. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  155. rpn = key->u.gcmp.rx_pn[i];
  156. p += scnprintf(p, sizeof(buf)+buf-p,
  157. "%02x%02x%02x%02x%02x%02x\n",
  158. rpn[0], rpn[1], rpn[2],
  159. rpn[3], rpn[4], rpn[5]);
  160. }
  161. len = p - buf;
  162. break;
  163. default:
  164. return 0;
  165. }
  166. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  167. }
  168. KEY_OPS(rx_spec);
  169. static ssize_t key_replays_read(struct file *file, char __user *userbuf,
  170. size_t count, loff_t *ppos)
  171. {
  172. struct ieee80211_key *key = file->private_data;
  173. char buf[20];
  174. int len;
  175. switch (key->conf.cipher) {
  176. case WLAN_CIPHER_SUITE_CCMP:
  177. case WLAN_CIPHER_SUITE_CCMP_256:
  178. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
  179. break;
  180. case WLAN_CIPHER_SUITE_AES_CMAC:
  181. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  182. len = scnprintf(buf, sizeof(buf), "%u\n",
  183. key->u.aes_cmac.replays);
  184. break;
  185. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  186. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  187. len = scnprintf(buf, sizeof(buf), "%u\n",
  188. key->u.aes_gmac.replays);
  189. break;
  190. case WLAN_CIPHER_SUITE_GCMP:
  191. case WLAN_CIPHER_SUITE_GCMP_256:
  192. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.gcmp.replays);
  193. break;
  194. default:
  195. return 0;
  196. }
  197. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  198. }
  199. KEY_OPS(replays);
  200. static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
  201. size_t count, loff_t *ppos)
  202. {
  203. struct ieee80211_key *key = file->private_data;
  204. char buf[20];
  205. int len;
  206. switch (key->conf.cipher) {
  207. case WLAN_CIPHER_SUITE_AES_CMAC:
  208. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  209. len = scnprintf(buf, sizeof(buf), "%u\n",
  210. key->u.aes_cmac.icverrors);
  211. break;
  212. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  213. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  214. len = scnprintf(buf, sizeof(buf), "%u\n",
  215. key->u.aes_gmac.icverrors);
  216. break;
  217. default:
  218. return 0;
  219. }
  220. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  221. }
  222. KEY_OPS(icverrors);
  223. static ssize_t key_mic_failures_read(struct file *file, char __user *userbuf,
  224. size_t count, loff_t *ppos)
  225. {
  226. struct ieee80211_key *key = file->private_data;
  227. char buf[20];
  228. int len;
  229. if (key->conf.cipher != WLAN_CIPHER_SUITE_TKIP)
  230. return -EINVAL;
  231. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.tkip.mic_failures);
  232. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  233. }
  234. KEY_OPS(mic_failures);
  235. static ssize_t key_key_read(struct file *file, char __user *userbuf,
  236. size_t count, loff_t *ppos)
  237. {
  238. struct ieee80211_key *key = file->private_data;
  239. int i, bufsize = 2 * key->conf.keylen + 2;
  240. char *buf = kmalloc(bufsize, GFP_KERNEL);
  241. char *p = buf;
  242. ssize_t res;
  243. if (!buf)
  244. return -ENOMEM;
  245. for (i = 0; i < key->conf.keylen; i++)
  246. p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
  247. p += scnprintf(p, bufsize+buf-p, "\n");
  248. res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  249. kfree(buf);
  250. return res;
  251. }
  252. KEY_OPS(key);
  253. #define DEBUGFS_ADD(name) \
  254. debugfs_create_file(#name, 0400, key->debugfs.dir, \
  255. key, &key_##name##_ops);
  256. void ieee80211_debugfs_key_add(struct ieee80211_key *key)
  257. {
  258. static int keycount;
  259. char buf[100];
  260. struct sta_info *sta;
  261. if (!key->local->debugfs.keys)
  262. return;
  263. sprintf(buf, "%d", keycount);
  264. key->debugfs.cnt = keycount;
  265. keycount++;
  266. key->debugfs.dir = debugfs_create_dir(buf,
  267. key->local->debugfs.keys);
  268. if (!key->debugfs.dir)
  269. return;
  270. sta = key->sta;
  271. if (sta) {
  272. sprintf(buf, "../../netdev:%s/stations/%pM",
  273. sta->sdata->name, sta->sta.addr);
  274. key->debugfs.stalink =
  275. debugfs_create_symlink("station", key->debugfs.dir, buf);
  276. }
  277. DEBUGFS_ADD(keylen);
  278. DEBUGFS_ADD(flags);
  279. DEBUGFS_ADD(keyidx);
  280. DEBUGFS_ADD(hw_key_idx);
  281. DEBUGFS_ADD(tx_rx_count);
  282. DEBUGFS_ADD(algorithm);
  283. DEBUGFS_ADD(tx_spec);
  284. DEBUGFS_ADD(rx_spec);
  285. DEBUGFS_ADD(replays);
  286. DEBUGFS_ADD(icverrors);
  287. DEBUGFS_ADD(mic_failures);
  288. DEBUGFS_ADD(key);
  289. DEBUGFS_ADD(ifindex);
  290. };
  291. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  292. {
  293. if (!key)
  294. return;
  295. debugfs_remove_recursive(key->debugfs.dir);
  296. key->debugfs.dir = NULL;
  297. }
  298. void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
  299. {
  300. char buf[50];
  301. struct ieee80211_key *key;
  302. if (!sdata->vif.debugfs_dir)
  303. return;
  304. lockdep_assert_held(&sdata->local->key_mtx);
  305. debugfs_remove(sdata->debugfs.default_unicast_key);
  306. sdata->debugfs.default_unicast_key = NULL;
  307. if (sdata->default_unicast_key) {
  308. key = key_mtx_dereference(sdata->local,
  309. sdata->default_unicast_key);
  310. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  311. sdata->debugfs.default_unicast_key =
  312. debugfs_create_symlink("default_unicast_key",
  313. sdata->vif.debugfs_dir, buf);
  314. }
  315. debugfs_remove(sdata->debugfs.default_multicast_key);
  316. sdata->debugfs.default_multicast_key = NULL;
  317. if (sdata->default_multicast_key) {
  318. key = key_mtx_dereference(sdata->local,
  319. sdata->default_multicast_key);
  320. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  321. sdata->debugfs.default_multicast_key =
  322. debugfs_create_symlink("default_multicast_key",
  323. sdata->vif.debugfs_dir, buf);
  324. }
  325. }
  326. void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
  327. {
  328. char buf[50];
  329. struct ieee80211_key *key;
  330. if (!sdata->vif.debugfs_dir)
  331. return;
  332. key = key_mtx_dereference(sdata->local,
  333. sdata->default_mgmt_key);
  334. if (key) {
  335. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  336. sdata->debugfs.default_mgmt_key =
  337. debugfs_create_symlink("default_mgmt_key",
  338. sdata->vif.debugfs_dir, buf);
  339. } else
  340. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  341. }
  342. void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
  343. {
  344. if (!sdata)
  345. return;
  346. debugfs_remove(sdata->debugfs.default_mgmt_key);
  347. sdata->debugfs.default_mgmt_key = NULL;
  348. }
  349. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  350. struct sta_info *sta)
  351. {
  352. debugfs_remove(key->debugfs.stalink);
  353. key->debugfs.stalink = NULL;
  354. }