debugfs_key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * Copyright (C) 2015 Intel Deutschland GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kobject.h>
  12. #include <linux/slab.h>
  13. #include "ieee80211_i.h"
  14. #include "key.h"
  15. #include "debugfs.h"
  16. #include "debugfs_key.h"
  17. #define KEY_READ(name, prop, format_string) \
  18. static ssize_t key_##name##_read(struct file *file, \
  19. char __user *userbuf, \
  20. size_t count, loff_t *ppos) \
  21. { \
  22. struct ieee80211_key *key = file->private_data; \
  23. return mac80211_format_buffer(userbuf, count, ppos, \
  24. format_string, key->prop); \
  25. }
  26. #define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
  27. #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
  28. #define KEY_OPS(name) \
  29. static const struct file_operations key_ ##name## _ops = { \
  30. .read = key_##name##_read, \
  31. .open = simple_open, \
  32. .llseek = generic_file_llseek, \
  33. }
  34. #define KEY_OPS_W(name) \
  35. static const struct file_operations key_ ##name## _ops = { \
  36. .read = key_##name##_read, \
  37. .write = key_##name##_write, \
  38. .open = simple_open, \
  39. .llseek = generic_file_llseek, \
  40. }
  41. #define KEY_FILE(name, format) \
  42. KEY_READ_##format(name) \
  43. KEY_OPS(name)
  44. #define KEY_CONF_READ(name, format_string) \
  45. KEY_READ(conf_##name, conf.name, format_string)
  46. #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
  47. #define KEY_CONF_OPS(name) \
  48. static const struct file_operations key_ ##name## _ops = { \
  49. .read = key_conf_##name##_read, \
  50. .open = simple_open, \
  51. .llseek = generic_file_llseek, \
  52. }
  53. #define KEY_CONF_FILE(name, format) \
  54. KEY_CONF_READ_##format(name) \
  55. KEY_CONF_OPS(name)
  56. KEY_CONF_FILE(keylen, D);
  57. KEY_CONF_FILE(keyidx, D);
  58. KEY_CONF_FILE(hw_key_idx, D);
  59. KEY_FILE(flags, X);
  60. KEY_READ(ifindex, sdata->name, "%s\n");
  61. KEY_OPS(ifindex);
  62. static ssize_t key_algorithm_read(struct file *file,
  63. char __user *userbuf,
  64. size_t count, loff_t *ppos)
  65. {
  66. char buf[15];
  67. struct ieee80211_key *key = file->private_data;
  68. u32 c = key->conf.cipher;
  69. sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
  70. c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
  71. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  72. }
  73. KEY_OPS(algorithm);
  74. static ssize_t key_tx_spec_write(struct file *file, const char __user *userbuf,
  75. size_t count, loff_t *ppos)
  76. {
  77. struct ieee80211_key *key = file->private_data;
  78. u64 pn;
  79. int ret;
  80. switch (key->conf.cipher) {
  81. case WLAN_CIPHER_SUITE_WEP40:
  82. case WLAN_CIPHER_SUITE_WEP104:
  83. return -EINVAL;
  84. case WLAN_CIPHER_SUITE_TKIP:
  85. /* not supported yet */
  86. return -EOPNOTSUPP;
  87. case WLAN_CIPHER_SUITE_CCMP:
  88. case WLAN_CIPHER_SUITE_CCMP_256:
  89. case WLAN_CIPHER_SUITE_AES_CMAC:
  90. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  91. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  92. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  93. case WLAN_CIPHER_SUITE_GCMP:
  94. case WLAN_CIPHER_SUITE_GCMP_256:
  95. ret = kstrtou64_from_user(userbuf, count, 16, &pn);
  96. if (ret)
  97. return ret;
  98. /* PN is a 48-bit counter */
  99. if (pn >= (1ULL << 48))
  100. return -ERANGE;
  101. atomic64_set(&key->conf.tx_pn, pn);
  102. return count;
  103. default:
  104. return 0;
  105. }
  106. }
  107. static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
  108. size_t count, loff_t *ppos)
  109. {
  110. u64 pn;
  111. char buf[20];
  112. int len;
  113. struct ieee80211_key *key = file->private_data;
  114. switch (key->conf.cipher) {
  115. case WLAN_CIPHER_SUITE_WEP40:
  116. case WLAN_CIPHER_SUITE_WEP104:
  117. len = scnprintf(buf, sizeof(buf), "\n");
  118. break;
  119. case WLAN_CIPHER_SUITE_TKIP:
  120. pn = atomic64_read(&key->conf.tx_pn);
  121. len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
  122. TKIP_PN_TO_IV32(pn),
  123. TKIP_PN_TO_IV16(pn));
  124. break;
  125. case WLAN_CIPHER_SUITE_CCMP:
  126. case WLAN_CIPHER_SUITE_CCMP_256:
  127. case WLAN_CIPHER_SUITE_AES_CMAC:
  128. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  129. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  130. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  131. case WLAN_CIPHER_SUITE_GCMP:
  132. case WLAN_CIPHER_SUITE_GCMP_256:
  133. pn = atomic64_read(&key->conf.tx_pn);
  134. len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  135. (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
  136. (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
  137. break;
  138. default:
  139. return 0;
  140. }
  141. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  142. }
  143. KEY_OPS_W(tx_spec);
  144. static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
  145. size_t count, loff_t *ppos)
  146. {
  147. struct ieee80211_key *key = file->private_data;
  148. char buf[14*IEEE80211_NUM_TIDS+1], *p = buf;
  149. int i, len;
  150. const u8 *rpn;
  151. switch (key->conf.cipher) {
  152. case WLAN_CIPHER_SUITE_WEP40:
  153. case WLAN_CIPHER_SUITE_WEP104:
  154. len = scnprintf(buf, sizeof(buf), "\n");
  155. break;
  156. case WLAN_CIPHER_SUITE_TKIP:
  157. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  158. p += scnprintf(p, sizeof(buf)+buf-p,
  159. "%08x %04x\n",
  160. key->u.tkip.rx[i].iv32,
  161. key->u.tkip.rx[i].iv16);
  162. len = p - buf;
  163. break;
  164. case WLAN_CIPHER_SUITE_CCMP:
  165. case WLAN_CIPHER_SUITE_CCMP_256:
  166. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  167. rpn = key->u.ccmp.rx_pn[i];
  168. p += scnprintf(p, sizeof(buf)+buf-p,
  169. "%02x%02x%02x%02x%02x%02x\n",
  170. rpn[0], rpn[1], rpn[2],
  171. rpn[3], rpn[4], rpn[5]);
  172. }
  173. len = p - buf;
  174. break;
  175. case WLAN_CIPHER_SUITE_AES_CMAC:
  176. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  177. rpn = key->u.aes_cmac.rx_pn;
  178. p += scnprintf(p, sizeof(buf)+buf-p,
  179. "%02x%02x%02x%02x%02x%02x\n",
  180. rpn[0], rpn[1], rpn[2],
  181. rpn[3], rpn[4], rpn[5]);
  182. len = p - buf;
  183. break;
  184. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  185. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  186. rpn = key->u.aes_gmac.rx_pn;
  187. p += scnprintf(p, sizeof(buf)+buf-p,
  188. "%02x%02x%02x%02x%02x%02x\n",
  189. rpn[0], rpn[1], rpn[2],
  190. rpn[3], rpn[4], rpn[5]);
  191. len = p - buf;
  192. break;
  193. case WLAN_CIPHER_SUITE_GCMP:
  194. case WLAN_CIPHER_SUITE_GCMP_256:
  195. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  196. rpn = key->u.gcmp.rx_pn[i];
  197. p += scnprintf(p, sizeof(buf)+buf-p,
  198. "%02x%02x%02x%02x%02x%02x\n",
  199. rpn[0], rpn[1], rpn[2],
  200. rpn[3], rpn[4], rpn[5]);
  201. }
  202. len = p - buf;
  203. break;
  204. default:
  205. return 0;
  206. }
  207. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  208. }
  209. KEY_OPS(rx_spec);
  210. static ssize_t key_replays_read(struct file *file, char __user *userbuf,
  211. size_t count, loff_t *ppos)
  212. {
  213. struct ieee80211_key *key = file->private_data;
  214. char buf[20];
  215. int len;
  216. switch (key->conf.cipher) {
  217. case WLAN_CIPHER_SUITE_CCMP:
  218. case WLAN_CIPHER_SUITE_CCMP_256:
  219. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
  220. break;
  221. case WLAN_CIPHER_SUITE_AES_CMAC:
  222. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  223. len = scnprintf(buf, sizeof(buf), "%u\n",
  224. key->u.aes_cmac.replays);
  225. break;
  226. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  227. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  228. len = scnprintf(buf, sizeof(buf), "%u\n",
  229. key->u.aes_gmac.replays);
  230. break;
  231. case WLAN_CIPHER_SUITE_GCMP:
  232. case WLAN_CIPHER_SUITE_GCMP_256:
  233. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.gcmp.replays);
  234. break;
  235. default:
  236. return 0;
  237. }
  238. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  239. }
  240. KEY_OPS(replays);
  241. static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
  242. size_t count, loff_t *ppos)
  243. {
  244. struct ieee80211_key *key = file->private_data;
  245. char buf[20];
  246. int len;
  247. switch (key->conf.cipher) {
  248. case WLAN_CIPHER_SUITE_AES_CMAC:
  249. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  250. len = scnprintf(buf, sizeof(buf), "%u\n",
  251. key->u.aes_cmac.icverrors);
  252. break;
  253. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  254. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  255. len = scnprintf(buf, sizeof(buf), "%u\n",
  256. key->u.aes_gmac.icverrors);
  257. break;
  258. default:
  259. return 0;
  260. }
  261. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  262. }
  263. KEY_OPS(icverrors);
  264. static ssize_t key_mic_failures_read(struct file *file, char __user *userbuf,
  265. size_t count, loff_t *ppos)
  266. {
  267. struct ieee80211_key *key = file->private_data;
  268. char buf[20];
  269. int len;
  270. if (key->conf.cipher != WLAN_CIPHER_SUITE_TKIP)
  271. return -EINVAL;
  272. len = scnprintf(buf, sizeof(buf), "%u\n", key->u.tkip.mic_failures);
  273. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  274. }
  275. KEY_OPS(mic_failures);
  276. static ssize_t key_key_read(struct file *file, char __user *userbuf,
  277. size_t count, loff_t *ppos)
  278. {
  279. struct ieee80211_key *key = file->private_data;
  280. int i, bufsize = 2 * key->conf.keylen + 2;
  281. char *buf = kmalloc(bufsize, GFP_KERNEL);
  282. char *p = buf;
  283. ssize_t res;
  284. if (!buf)
  285. return -ENOMEM;
  286. for (i = 0; i < key->conf.keylen; i++)
  287. p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
  288. p += scnprintf(p, bufsize+buf-p, "\n");
  289. res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
  290. kfree(buf);
  291. return res;
  292. }
  293. KEY_OPS(key);
  294. #define DEBUGFS_ADD(name) \
  295. debugfs_create_file(#name, 0400, key->debugfs.dir, \
  296. key, &key_##name##_ops);
  297. #define DEBUGFS_ADD_W(name) \
  298. debugfs_create_file(#name, 0600, key->debugfs.dir, \
  299. key, &key_##name##_ops);
  300. void ieee80211_debugfs_key_add(struct ieee80211_key *key)
  301. {
  302. static int keycount;
  303. char buf[100];
  304. struct sta_info *sta;
  305. if (!key->local->debugfs.keys)
  306. return;
  307. sprintf(buf, "%d", keycount);
  308. key->debugfs.cnt = keycount;
  309. keycount++;
  310. key->debugfs.dir = debugfs_create_dir(buf,
  311. key->local->debugfs.keys);
  312. if (!key->debugfs.dir)
  313. return;
  314. sta = key->sta;
  315. if (sta) {
  316. sprintf(buf, "../../netdev:%s/stations/%pM",
  317. sta->sdata->name, sta->sta.addr);
  318. key->debugfs.stalink =
  319. debugfs_create_symlink("station", key->debugfs.dir, buf);
  320. }
  321. DEBUGFS_ADD(keylen);
  322. DEBUGFS_ADD(flags);
  323. DEBUGFS_ADD(keyidx);
  324. DEBUGFS_ADD(hw_key_idx);
  325. DEBUGFS_ADD(algorithm);
  326. DEBUGFS_ADD_W(tx_spec);
  327. DEBUGFS_ADD(rx_spec);
  328. DEBUGFS_ADD(replays);
  329. DEBUGFS_ADD(icverrors);
  330. DEBUGFS_ADD(mic_failures);
  331. DEBUGFS_ADD(key);
  332. DEBUGFS_ADD(ifindex);
  333. };
  334. void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
  335. {
  336. if (!key)
  337. return;
  338. debugfs_remove_recursive(key->debugfs.dir);
  339. key->debugfs.dir = NULL;
  340. }
  341. void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
  342. {
  343. char buf[50];
  344. struct ieee80211_key *key;
  345. if (!sdata->vif.debugfs_dir)
  346. return;
  347. lockdep_assert_held(&sdata->local->key_mtx);
  348. debugfs_remove(sdata->debugfs.default_unicast_key);
  349. sdata->debugfs.default_unicast_key = NULL;
  350. if (sdata->default_unicast_key) {
  351. key = key_mtx_dereference(sdata->local,
  352. sdata->default_unicast_key);
  353. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  354. sdata->debugfs.default_unicast_key =
  355. debugfs_create_symlink("default_unicast_key",
  356. sdata->vif.debugfs_dir, buf);
  357. }
  358. debugfs_remove(sdata->debugfs.default_multicast_key);
  359. sdata->debugfs.default_multicast_key = NULL;
  360. if (sdata->default_multicast_key) {
  361. key = key_mtx_dereference(sdata->local,
  362. sdata->default_multicast_key);
  363. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  364. sdata->debugfs.default_multicast_key =
  365. debugfs_create_symlink("default_multicast_key",
  366. sdata->vif.debugfs_dir, buf);
  367. }
  368. }
  369. void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
  370. {
  371. char buf[50];
  372. struct ieee80211_key *key;
  373. if (!sdata->vif.debugfs_dir)
  374. return;
  375. key = key_mtx_dereference(sdata->local,
  376. sdata->default_mgmt_key);
  377. if (key) {
  378. sprintf(buf, "../keys/%d", key->debugfs.cnt);
  379. sdata->debugfs.default_mgmt_key =
  380. debugfs_create_symlink("default_mgmt_key",
  381. sdata->vif.debugfs_dir, buf);
  382. } else
  383. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  384. }
  385. void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
  386. {
  387. if (!sdata)
  388. return;
  389. debugfs_remove(sdata->debugfs.default_mgmt_key);
  390. sdata->debugfs.default_mgmt_key = NULL;
  391. }
  392. void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
  393. struct sta_info *sta)
  394. {
  395. debugfs_remove(key->debugfs.stalink);
  396. key->debugfs.stalink = NULL;
  397. }