debugfs_key.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "ieee80211_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_local *local,
  168. struct ieee80211_key *key)
  169. {
  170. static int keycount;
  171. char buf[20];
  172. if (!local->debugfs.keys)
  173. return;
  174. sprintf(buf, "%d", keycount);
  175. keycount++;
  176. key->debugfs.dir = debugfs_create_dir(buf,
  177. local->debugfs.keys);
  178. if (!key->debugfs.dir)
  179. return;
  180. DEBUGFS_ADD(keylen);
  181. DEBUGFS_ADD(flags);
  182. DEBUGFS_ADD(keyidx);
  183. DEBUGFS_ADD(hw_key_idx);
  184. DEBUGFS_ADD(tx_rx_count);
  185. DEBUGFS_ADD(algorithm);
  186. DEBUGFS_ADD(tx_spec);
  187. DEBUGFS_ADD(rx_spec);
  188. DEBUGFS_ADD(replays);
  189. DEBUGFS_ADD(key);
  190. DEBUGFS_ADD(ifindex);
  191. };
  192. #define DEBUGFS_DEL(name) \
  193. debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
  194. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  195. {
  196. if (!key)
  197. return;
  198. DEBUGFS_DEL(keylen);
  199. DEBUGFS_DEL(flags);
  200. DEBUGFS_DEL(keyidx);
  201. DEBUGFS_DEL(hw_key_idx);
  202. DEBUGFS_DEL(tx_rx_count);
  203. DEBUGFS_DEL(algorithm);
  204. DEBUGFS_DEL(tx_spec);
  205. DEBUGFS_DEL(rx_spec);
  206. DEBUGFS_DEL(replays);
  207. DEBUGFS_DEL(key);
  208. DEBUGFS_DEL(ifindex);
  209. debugfs_remove(key->debugfs.stalink);
  210. key->debugfs.stalink = NULL;
  211. debugfs_remove(key->debugfs.dir);
  212. key->debugfs.dir = NULL;
  213. }
  214. void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
  215. {
  216. char buf[50];
  217. if (!sdata->debugfsdir)
  218. return;
  219. sprintf(buf, "../keys/%d", sdata->default_key->conf.keyidx);
  220. sdata->debugfs.default_key =
  221. debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
  222. }
  223. void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
  224. {
  225. if (!sdata)
  226. return;
  227. debugfs_remove(sdata->debugfs.default_key);
  228. sdata->debugfs.default_key = NULL;
  229. }
  230. void ieee80211_debugfs_key_sta_link(struct ieee80211_key *key,
  231. struct sta_info *sta)
  232. {
  233. char buf[50];
  234. DECLARE_MAC_BUF(mac);
  235. if (!key->debugfs.dir)
  236. return;
  237. sprintf(buf, "../../stations/%s", print_mac(mac, sta->addr));
  238. key->debugfs.stalink =
  239. debugfs_create_symlink("station", key->debugfs.dir, buf);
  240. }
  241. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  242. struct sta_info *sta)
  243. {
  244. debugfs_remove(key->debugfs.stalink);
  245. key->debugfs.stalink = NULL;
  246. }