blob: c3a3082b72e5cde611b5fb89a63829c7fb4eee27 [file] [log] [blame]
Jiri Bence9f207f2007-05-05 11:46:38 -07001/*
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
11#include <linux/kobject.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jiri Bence9f207f2007-05-05 11:46:38 -070013#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040014#include "key.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070015#include "debugfs.h"
16#include "debugfs_key.h"
17
Eliad Peller07caf9d2010-10-27 14:58:29 +020018#define KEY_READ(name, prop, format_string) \
Jiri Bence9f207f2007-05-05 11:46:38 -070019static ssize_t key_##name##_read(struct file *file, \
20 char __user *userbuf, \
21 size_t count, loff_t *ppos) \
22{ \
Jiri Bence9f207f2007-05-05 11:46:38 -070023 struct ieee80211_key *key = file->private_data; \
Eliad Peller07caf9d2010-10-27 14:58:29 +020024 return mac80211_format_buffer(userbuf, count, ppos, \
25 format_string, key->prop); \
Jiri Bence9f207f2007-05-05 11:46:38 -070026}
Eliad Peller07caf9d2010-10-27 14:58:29 +020027#define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
28#define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
Jiri Bence9f207f2007-05-05 11:46:38 -070029
30#define KEY_OPS(name) \
31static const struct file_operations key_ ##name## _ops = { \
32 .read = key_##name##_read, \
Stephen Boyd234e3402012-04-05 14:25:11 -070033 .open = simple_open, \
Arnd Bergmann2b18ab362010-07-06 19:05:31 +020034 .llseek = generic_file_llseek, \
Jiri Bence9f207f2007-05-05 11:46:38 -070035}
36
37#define KEY_FILE(name, format) \
38 KEY_READ_##format(name) \
39 KEY_OPS(name)
40
Eliad Peller07caf9d2010-10-27 14:58:29 +020041#define KEY_CONF_READ(name, format_string) \
42 KEY_READ(conf_##name, conf.name, format_string)
43#define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
Johannes Berg8f20fc22007-08-28 17:01:54 -040044
45#define KEY_CONF_OPS(name) \
46static const struct file_operations key_ ##name## _ops = { \
47 .read = key_conf_##name##_read, \
Stephen Boyd234e3402012-04-05 14:25:11 -070048 .open = simple_open, \
Arnd Bergmann2b18ab362010-07-06 19:05:31 +020049 .llseek = generic_file_llseek, \
Johannes Berg8f20fc22007-08-28 17:01:54 -040050}
51
52#define KEY_CONF_FILE(name, format) \
53 KEY_CONF_READ_##format(name) \
54 KEY_CONF_OPS(name)
55
56KEY_CONF_FILE(keylen, D);
57KEY_CONF_FILE(keyidx, D);
58KEY_CONF_FILE(hw_key_idx, D);
Johannes Berg11a843b2007-08-28 17:01:55 -040059KEY_FILE(flags, X);
Jiri Bence9f207f2007-05-05 11:46:38 -070060KEY_FILE(tx_rx_count, D);
Eliad Peller07caf9d2010-10-27 14:58:29 +020061KEY_READ(ifindex, sdata->name, "%s\n");
Johannes Berge7a64f12007-08-28 17:01:55 -040062KEY_OPS(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -070063
64static ssize_t key_algorithm_read(struct file *file,
65 char __user *userbuf,
66 size_t count, loff_t *ppos)
67{
Johannes Berg97359d12010-08-10 09:46:38 +020068 char buf[15];
Jiri Bence9f207f2007-05-05 11:46:38 -070069 struct ieee80211_key *key = file->private_data;
Johannes Berg97359d12010-08-10 09:46:38 +020070 u32 c = key->conf.cipher;
Jiri Bence9f207f2007-05-05 11:46:38 -070071
Johannes Berg97359d12010-08-10 09:46:38 +020072 sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
73 c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
74 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
Jiri Bence9f207f2007-05-05 11:46:38 -070075}
76KEY_OPS(algorithm);
77
78static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
79 size_t count, loff_t *ppos)
80{
Johannes Bergaba83a0b2011-07-06 21:59:39 +020081 u64 pn;
Jiri Bence9f207f2007-05-05 11:46:38 -070082 char buf[20];
83 int len;
84 struct ieee80211_key *key = file->private_data;
85
Johannes Berg97359d12010-08-10 09:46:38 +020086 switch (key->conf.cipher) {
87 case WLAN_CIPHER_SUITE_WEP40:
88 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -070089 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -040090 break;
Johannes Berg97359d12010-08-10 09:46:38 +020091 case WLAN_CIPHER_SUITE_TKIP:
Jiri Bence9f207f2007-05-05 11:46:38 -070092 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -070093 key->u.tkip.tx.iv32,
94 key->u.tkip.tx.iv16);
Johannes Berg50339a62007-08-28 17:01:52 -040095 break;
Johannes Berg97359d12010-08-10 09:46:38 +020096 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergaba83a0b2011-07-06 21:59:39 +020097 pn = atomic64_read(&key->u.ccmp.tx_pn);
Jiri Bence9f207f2007-05-05 11:46:38 -070098 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
Johannes Bergaba83a0b2011-07-06 21:59:39 +020099 (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
100 (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
Johannes Berg50339a62007-08-28 17:01:52 -0400101 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200102 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg75396ae2011-07-06 22:00:35 +0200103 pn = atomic64_read(&key->u.aes_cmac.tx_pn);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200104 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
Johannes Berg75396ae2011-07-06 22:00:35 +0200105 (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
106 (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200107 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700108 default:
109 return 0;
110 }
111 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
112}
113KEY_OPS(tx_spec);
114
115static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
116 size_t count, loff_t *ppos)
117{
118 struct ieee80211_key *key = file->private_data;
Johannes Berg5a306f52012-11-14 23:22:21 +0100119 char buf[14*IEEE80211_NUM_TIDS+1], *p = buf;
Jiri Bence9f207f2007-05-05 11:46:38 -0700120 int i, len;
121 const u8 *rpn;
122
Johannes Berg97359d12010-08-10 09:46:38 +0200123 switch (key->conf.cipher) {
124 case WLAN_CIPHER_SUITE_WEP40:
125 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -0700126 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -0400127 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200128 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100129 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Jiri Bence9f207f2007-05-05 11:46:38 -0700130 p += scnprintf(p, sizeof(buf)+buf-p,
131 "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700132 key->u.tkip.rx[i].iv32,
133 key->u.tkip.rx[i].iv16);
Jiri Bence9f207f2007-05-05 11:46:38 -0700134 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400135 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200136 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100137 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700138 rpn = key->u.ccmp.rx_pn[i];
139 p += scnprintf(p, sizeof(buf)+buf-p,
140 "%02x%02x%02x%02x%02x%02x\n",
141 rpn[0], rpn[1], rpn[2],
142 rpn[3], rpn[4], rpn[5]);
143 }
144 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400145 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200146 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200147 rpn = key->u.aes_cmac.rx_pn;
148 p += scnprintf(p, sizeof(buf)+buf-p,
149 "%02x%02x%02x%02x%02x%02x\n",
150 rpn[0], rpn[1], rpn[2],
151 rpn[3], rpn[4], rpn[5]);
152 len = p - buf;
153 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700154 default:
155 return 0;
156 }
157 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
158}
159KEY_OPS(rx_spec);
160
161static ssize_t key_replays_read(struct file *file, char __user *userbuf,
162 size_t count, loff_t *ppos)
163{
164 struct ieee80211_key *key = file->private_data;
165 char buf[20];
166 int len;
167
Johannes Berg97359d12010-08-10 09:46:38 +0200168 switch (key->conf.cipher) {
169 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200170 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
171 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200172 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200173 len = scnprintf(buf, sizeof(buf), "%u\n",
174 key->u.aes_cmac.replays);
175 break;
176 default:
Jiri Bence9f207f2007-05-05 11:46:38 -0700177 return 0;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200178 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700179 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180}
181KEY_OPS(replays);
182
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200183static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
184 size_t count, loff_t *ppos)
185{
186 struct ieee80211_key *key = file->private_data;
187 char buf[20];
188 int len;
189
Johannes Berg97359d12010-08-10 09:46:38 +0200190 switch (key->conf.cipher) {
191 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200192 len = scnprintf(buf, sizeof(buf), "%u\n",
193 key->u.aes_cmac.icverrors);
194 break;
195 default:
196 return 0;
197 }
198 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
199}
200KEY_OPS(icverrors);
201
Saravanab98ea052012-12-04 19:47:42 +0530202static ssize_t key_mic_failures_read(struct file *file, char __user *userbuf,
203 size_t count, loff_t *ppos)
204{
205 struct ieee80211_key *key = file->private_data;
206 char buf[20];
207 int len;
208
209 if (key->conf.cipher != WLAN_CIPHER_SUITE_TKIP)
210 return -EINVAL;
211
212 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.tkip.mic_failures);
213
214 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
215}
216KEY_OPS(mic_failures);
217
Jiri Bence9f207f2007-05-05 11:46:38 -0700218static ssize_t key_key_read(struct file *file, char __user *userbuf,
219 size_t count, loff_t *ppos)
220{
221 struct ieee80211_key *key = file->private_data;
Jesper Juhl520efd12010-10-29 16:10:26 +0200222 int i, bufsize = 2 * key->conf.keylen + 2;
Jiri Bence9f207f2007-05-05 11:46:38 -0700223 char *buf = kmalloc(bufsize, GFP_KERNEL);
224 char *p = buf;
Jesper Juhl520efd12010-10-29 16:10:26 +0200225 ssize_t res;
226
227 if (!buf)
228 return -ENOMEM;
Jiri Bence9f207f2007-05-05 11:46:38 -0700229
Johannes Berg8f20fc22007-08-28 17:01:54 -0400230 for (i = 0; i < key->conf.keylen; i++)
231 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
Jiri Bence9f207f2007-05-05 11:46:38 -0700232 p += scnprintf(p, bufsize+buf-p, "\n");
233 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
234 kfree(buf);
235 return res;
236}
237KEY_OPS(key);
238
239#define DEBUGFS_ADD(name) \
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100240 debugfs_create_file(#name, 0400, key->debugfs.dir, \
241 key, &key_##name##_ops);
Jiri Bence9f207f2007-05-05 11:46:38 -0700242
Johannes Berg3b967662008-04-08 17:56:52 +0200243void ieee80211_debugfs_key_add(struct ieee80211_key *key)
Johannes Berg9446f3e2012-01-17 10:32:01 +0100244{
Johannes Berg50339a62007-08-28 17:01:52 -0400245 static int keycount;
Johannes Berg9446f3e2012-01-17 10:32:01 +0100246 char buf[100];
Johannes Berg3b967662008-04-08 17:56:52 +0200247 struct sta_info *sta;
Jiri Bence9f207f2007-05-05 11:46:38 -0700248
Johannes Berg3b967662008-04-08 17:56:52 +0200249 if (!key->local->debugfs.keys)
Jiri Bence9f207f2007-05-05 11:46:38 -0700250 return;
251
Johannes Berg50339a62007-08-28 17:01:52 -0400252 sprintf(buf, "%d", keycount);
Johannes Bergd9c58f32008-04-08 16:46:36 -0400253 key->debugfs.cnt = keycount;
Johannes Berg50339a62007-08-28 17:01:52 -0400254 keycount++;
Jiri Bence9f207f2007-05-05 11:46:38 -0700255 key->debugfs.dir = debugfs_create_dir(buf,
Johannes Berg3b967662008-04-08 17:56:52 +0200256 key->local->debugfs.keys);
Jiri Bence9f207f2007-05-05 11:46:38 -0700257
258 if (!key->debugfs.dir)
259 return;
260
Johannes Berg40b275b2011-05-13 14:15:49 +0200261 sta = key->sta;
262 if (sta) {
Johannes Berg9446f3e2012-01-17 10:32:01 +0100263 sprintf(buf, "../../netdev:%s/stations/%pM",
264 sta->sdata->name, sta->sta.addr);
Johannes Berg3b967662008-04-08 17:56:52 +0200265 key->debugfs.stalink =
266 debugfs_create_symlink("station", key->debugfs.dir, buf);
Johannes Berg40b275b2011-05-13 14:15:49 +0200267 }
Johannes Berg3b967662008-04-08 17:56:52 +0200268
Jiri Bence9f207f2007-05-05 11:46:38 -0700269 DEBUGFS_ADD(keylen);
Johannes Berg8f20fc22007-08-28 17:01:54 -0400270 DEBUGFS_ADD(flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700271 DEBUGFS_ADD(keyidx);
272 DEBUGFS_ADD(hw_key_idx);
273 DEBUGFS_ADD(tx_rx_count);
274 DEBUGFS_ADD(algorithm);
275 DEBUGFS_ADD(tx_spec);
276 DEBUGFS_ADD(rx_spec);
277 DEBUGFS_ADD(replays);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200278 DEBUGFS_ADD(icverrors);
Saravanab98ea052012-12-04 19:47:42 +0530279 DEBUGFS_ADD(mic_failures);
Jiri Bence9f207f2007-05-05 11:46:38 -0700280 DEBUGFS_ADD(key);
Johannes Berge7a64f12007-08-28 17:01:55 -0400281 DEBUGFS_ADD(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -0700282};
283
Jiri Bence9f207f2007-05-05 11:46:38 -0700284void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
285{
286 if (!key)
287 return;
288
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100289 debugfs_remove_recursive(key->debugfs.dir);
Jiri Bence9f207f2007-05-05 11:46:38 -0700290 key->debugfs.dir = NULL;
291}
Johannes Bergf7e01042010-12-09 19:49:02 +0100292
293void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
Jiri Bence9f207f2007-05-05 11:46:38 -0700294{
295 char buf[50];
Johannes Berg78520ca2008-05-03 01:04:47 +0200296 struct ieee80211_key *key;
Jiri Bence9f207f2007-05-05 11:46:38 -0700297
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100298 if (!sdata->debugfs.dir)
Jiri Bence9f207f2007-05-05 11:46:38 -0700299 return;
300
Johannes Bergf7e01042010-12-09 19:49:02 +0100301 lockdep_assert_held(&sdata->local->key_mtx);
Johannes Berg78520ca2008-05-03 01:04:47 +0200302
Johannes Berg135792e2012-07-05 21:37:05 +0200303 if (sdata->debugfs.default_unicast_key) {
304 debugfs_remove(sdata->debugfs.default_unicast_key);
305 sdata->debugfs.default_unicast_key = NULL;
306 }
307
Johannes Bergf7e01042010-12-09 19:49:02 +0100308 if (sdata->default_unicast_key) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200309 key = key_mtx_dereference(sdata->local,
310 sdata->default_unicast_key);
Johannes Berg78520ca2008-05-03 01:04:47 +0200311 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Bergf7e01042010-12-09 19:49:02 +0100312 sdata->debugfs.default_unicast_key =
313 debugfs_create_symlink("default_unicast_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100314 sdata->debugfs.dir, buf);
Johannes Berg135792e2012-07-05 21:37:05 +0200315 }
316
317 if (sdata->debugfs.default_multicast_key) {
318 debugfs_remove(sdata->debugfs.default_multicast_key);
319 sdata->debugfs.default_multicast_key = NULL;
Johannes Bergf7e01042010-12-09 19:49:02 +0100320 }
Johannes Berg78520ca2008-05-03 01:04:47 +0200321
Johannes Bergf7e01042010-12-09 19:49:02 +0100322 if (sdata->default_multicast_key) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200323 key = key_mtx_dereference(sdata->local,
324 sdata->default_multicast_key);
Johannes Bergf7e01042010-12-09 19:49:02 +0100325 sprintf(buf, "../keys/%d", key->debugfs.cnt);
326 sdata->debugfs.default_multicast_key =
327 debugfs_create_symlink("default_multicast_key",
328 sdata->debugfs.dir, buf);
Johannes Bergf7e01042010-12-09 19:49:02 +0100329 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700330}
Jiri Bence9f207f2007-05-05 11:46:38 -0700331
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200332void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
333{
334 char buf[50];
335 struct ieee80211_key *key;
336
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100337 if (!sdata->debugfs.dir)
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200338 return;
339
Johannes Berg40b275b2011-05-13 14:15:49 +0200340 key = key_mtx_dereference(sdata->local,
341 sdata->default_mgmt_key);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200342 if (key) {
343 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100344 sdata->debugfs.default_mgmt_key =
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200345 debugfs_create_symlink("default_mgmt_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100346 sdata->debugfs.dir, buf);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200347 } else
348 ieee80211_debugfs_key_remove_mgmt_default(sdata);
349}
350
351void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
352{
353 if (!sdata)
354 return;
355
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100356 debugfs_remove(sdata->debugfs.default_mgmt_key);
357 sdata->debugfs.default_mgmt_key = NULL;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200358}
359
Jiri Bence9f207f2007-05-05 11:46:38 -0700360void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
361 struct sta_info *sta)
362{
363 debugfs_remove(key->debugfs.stalink);
364 key->debugfs.stalink = NULL;
365}