debugfs_key.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. default:
  70. return 0;
  71. }
  72. return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
  73. }
  74. KEY_OPS(algorithm);
  75. static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
  76. size_t count, loff_t *ppos)
  77. {
  78. const u8 *tpn;
  79. char buf[20];
  80. int len;
  81. struct ieee80211_key *key = file->private_data;
  82. switch (key->conf.alg) {
  83. case ALG_WEP:
  84. len = scnprintf(buf, sizeof(buf), "\n");
  85. break;
  86. case ALG_TKIP:
  87. len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
  88. key->u.tkip.iv32,
  89. key->u.tkip.iv16);
  90. break;
  91. case ALG_CCMP:
  92. tpn = key->u.ccmp.tx_pn;
  93. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  94. tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], 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.alg) {
  110. case ALG_WEP:
  111. len = scnprintf(buf, sizeof(buf), "\n");
  112. break;
  113. case ALG_TKIP:
  114. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  115. p += scnprintf(p, sizeof(buf)+buf-p,
  116. "%08x %04x\n",
  117. key->u.tkip.iv32_rx[i],
  118. key->u.tkip.iv16_rx[i]);
  119. len = p - buf;
  120. break;
  121. case ALG_CCMP:
  122. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  123. rpn = key->u.ccmp.rx_pn[i];
  124. p += scnprintf(p, sizeof(buf)+buf-p,
  125. "%02x%02x%02x%02x%02x%02x\n",
  126. rpn[0], rpn[1], rpn[2],
  127. rpn[3], rpn[4], rpn[5]);
  128. }
  129. len = p - buf;
  130. break;
  131. default:
  132. return 0;
  133. }
  134. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  135. }
  136. KEY_OPS(rx_spec);
  137. static ssize_t key_replays_read(struct file *file, char __user *userbuf,
  138. size_t count, loff_t *ppos)
  139. {
  140. struct ieee80211_key *key = file->private_data;
  141. char buf[20];
  142. int len;
  143. if (key->conf.alg != ALG_CCMP)
  144. return 0;
  145. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
  146. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  147. }
  148. KEY_OPS(replays);
  149. static ssize_t key_key_read(struct file *file, char __user *userbuf,
  150. size_t count, loff_t *ppos)
  151. {
  152. struct ieee80211_key *key = file->private_data;
  153. int i, res, bufsize = 2 * key->conf.keylen + 2;
  154. char *buf = kmalloc(bufsize, GFP_KERNEL);
  155. char *p = buf;
  156. for (i = 0; i < key->conf.keylen; i++)
  157. p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
  158. p += scnprintf(p, bufsize+buf-p, "\n");
  159. res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  160. kfree(buf);
  161. return res;
  162. }
  163. KEY_OPS(key);
  164. #define DEBUGFS_ADD(name) \
  165. key->debugfs.name = debugfs_create_file(#name, 0400,\
  166. key->debugfs.dir, key, &key_##name##_ops);
  167. void ieee80211_debugfs_key_add(struct ieee80211_key *key)
  168. {
  169. static int keycount;
  170. char buf[50];
  171. DECLARE_MAC_BUF(mac);
  172. struct sta_info *sta;
  173. if (!key->local->debugfs.keys)
  174. return;
  175. sprintf(buf, "%d", keycount);
  176. key->debugfs.cnt = keycount;
  177. keycount++;
  178. key->debugfs.dir = debugfs_create_dir(buf,
  179. key->local->debugfs.keys);
  180. if (!key->debugfs.dir)
  181. return;
  182. rcu_read_lock();
  183. sta = rcu_dereference(key->sta);
  184. if (sta)
  185. sprintf(buf, "../../stations/%s", print_mac(mac, sta->addr));
  186. rcu_read_unlock();
  187. /* using sta as a boolean is fine outside RCU lock */
  188. if (sta)
  189. key->debugfs.stalink =
  190. debugfs_create_symlink("station", key->debugfs.dir, buf);
  191. DEBUGFS_ADD(keylen);
  192. DEBUGFS_ADD(flags);
  193. DEBUGFS_ADD(keyidx);
  194. DEBUGFS_ADD(hw_key_idx);
  195. DEBUGFS_ADD(tx_rx_count);
  196. DEBUGFS_ADD(algorithm);
  197. DEBUGFS_ADD(tx_spec);
  198. DEBUGFS_ADD(rx_spec);
  199. DEBUGFS_ADD(replays);
  200. DEBUGFS_ADD(key);
  201. DEBUGFS_ADD(ifindex);
  202. };
  203. #define DEBUGFS_DEL(name) \
  204. debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
  205. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  206. {
  207. if (!key)
  208. return;
  209. DEBUGFS_DEL(keylen);
  210. DEBUGFS_DEL(flags);
  211. DEBUGFS_DEL(keyidx);
  212. DEBUGFS_DEL(hw_key_idx);
  213. DEBUGFS_DEL(tx_rx_count);
  214. DEBUGFS_DEL(algorithm);
  215. DEBUGFS_DEL(tx_spec);
  216. DEBUGFS_DEL(rx_spec);
  217. DEBUGFS_DEL(replays);
  218. DEBUGFS_DEL(key);
  219. DEBUGFS_DEL(ifindex);
  220. debugfs_remove(key->debugfs.stalink);
  221. key->debugfs.stalink = NULL;
  222. debugfs_remove(key->debugfs.dir);
  223. key->debugfs.dir = NULL;
  224. }
  225. void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
  226. {
  227. char buf[50];
  228. struct ieee80211_key *key;
  229. if (!sdata->debugfsdir)
  230. return;
  231. /* this is running under the key lock */
  232. key = sdata->default_key;
  233. if (key) {
  234. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  235. sdata->debugfs.default_key =
  236. debugfs_create_symlink("default_key",
  237. sdata->debugfsdir, buf);
  238. } else
  239. ieee80211_debugfs_key_remove_default(sdata);
  240. }
  241. void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
  242. {
  243. if (!sdata)
  244. return;
  245. debugfs_remove(sdata->debugfs.default_key);
  246. sdata->debugfs.default_key = NULL;
  247. }
  248. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  249. struct sta_info *sta)
  250. {
  251. debugfs_remove(key->debugfs.stalink);
  252. key->debugfs.stalink = NULL;
  253. }