2 * Copyright 2003-2005 Devicescape Software, Inc.
3 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
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.
11 #include <linux/kobject.h>
12 #include <linux/slab.h>
13 #include "ieee80211_i.h"
16 #include "debugfs_key.h"
18 #define KEY_READ(name, prop, buflen, format_string) \
19 static ssize_t key_##name##_read(struct file *file, \
20 char __user *userbuf, \
21 size_t count, loff_t *ppos) \
24 struct ieee80211_key *key = file->private_data; \
25 int res = scnprintf(buf, buflen, format_string, key->prop); \
26 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
28 #define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n")
29 #define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n")
31 #define KEY_OPS(name) \
32 static const struct file_operations key_ ##name## _ops = { \
33 .read = key_##name##_read, \
34 .open = mac80211_open_file_generic, \
37 #define KEY_FILE(name, format) \
38 KEY_READ_##format(name) \
41 #define KEY_CONF_READ(name, buflen, format_string) \
42 KEY_READ(conf_##name, conf.name, buflen, format_string)
43 #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n")
45 #define KEY_CONF_OPS(name) \
46 static const struct file_operations key_ ##name## _ops = { \
47 .read = key_conf_##name##_read, \
48 .open = mac80211_open_file_generic, \
51 #define KEY_CONF_FILE(name, format) \
52 KEY_CONF_READ_##format(name) \
55 KEY_CONF_FILE(keylen
, D
);
56 KEY_CONF_FILE(keyidx
, D
);
57 KEY_CONF_FILE(hw_key_idx
, D
);
59 KEY_FILE(tx_rx_count
, D
);
60 KEY_READ(ifindex
, sdata
->name
, IFNAMSIZ
+ 2, "%s\n");
63 static ssize_t
key_algorithm_read(struct file
*file
,
65 size_t count
, loff_t
*ppos
)
68 struct ieee80211_key
*key
= file
->private_data
;
70 switch (key
->conf
.alg
) {
81 alg
= "AES-128-CMAC\n";
86 return simple_read_from_buffer(userbuf
, count
, ppos
, alg
, strlen(alg
));
90 static ssize_t
key_tx_spec_read(struct file
*file
, char __user
*userbuf
,
91 size_t count
, loff_t
*ppos
)
96 struct ieee80211_key
*key
= file
->private_data
;
98 switch (key
->conf
.alg
) {
100 len
= scnprintf(buf
, sizeof(buf
), "\n");
103 len
= scnprintf(buf
, sizeof(buf
), "%08x %04x\n",
105 key
->u
.tkip
.tx
.iv16
);
108 tpn
= key
->u
.ccmp
.tx_pn
;
109 len
= scnprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x%02x%02x\n",
110 tpn
[0], tpn
[1], tpn
[2], tpn
[3], tpn
[4], tpn
[5]);
113 tpn
= key
->u
.aes_cmac
.tx_pn
;
114 len
= scnprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x%02x%02x\n",
115 tpn
[0], tpn
[1], tpn
[2], tpn
[3], tpn
[4],
121 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
125 static ssize_t
key_rx_spec_read(struct file
*file
, char __user
*userbuf
,
126 size_t count
, loff_t
*ppos
)
128 struct ieee80211_key
*key
= file
->private_data
;
129 char buf
[14*NUM_RX_DATA_QUEUES
+1], *p
= buf
;
133 switch (key
->conf
.alg
) {
135 len
= scnprintf(buf
, sizeof(buf
), "\n");
138 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++)
139 p
+= scnprintf(p
, sizeof(buf
)+buf
-p
,
141 key
->u
.tkip
.rx
[i
].iv32
,
142 key
->u
.tkip
.rx
[i
].iv16
);
146 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++) {
147 rpn
= key
->u
.ccmp
.rx_pn
[i
];
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]);
156 rpn
= key
->u
.aes_cmac
.rx_pn
;
157 p
+= scnprintf(p
, sizeof(buf
)+buf
-p
,
158 "%02x%02x%02x%02x%02x%02x\n",
159 rpn
[0], rpn
[1], rpn
[2],
160 rpn
[3], rpn
[4], rpn
[5]);
166 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
170 static ssize_t
key_replays_read(struct file
*file
, char __user
*userbuf
,
171 size_t count
, loff_t
*ppos
)
173 struct ieee80211_key
*key
= file
->private_data
;
177 switch (key
->conf
.alg
) {
179 len
= scnprintf(buf
, sizeof(buf
), "%u\n", key
->u
.ccmp
.replays
);
182 len
= scnprintf(buf
, sizeof(buf
), "%u\n",
183 key
->u
.aes_cmac
.replays
);
188 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
192 static ssize_t
key_icverrors_read(struct file
*file
, char __user
*userbuf
,
193 size_t count
, loff_t
*ppos
)
195 struct ieee80211_key
*key
= file
->private_data
;
199 switch (key
->conf
.alg
) {
201 len
= scnprintf(buf
, sizeof(buf
), "%u\n",
202 key
->u
.aes_cmac
.icverrors
);
207 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
211 static ssize_t
key_key_read(struct file
*file
, char __user
*userbuf
,
212 size_t count
, loff_t
*ppos
)
214 struct ieee80211_key
*key
= file
->private_data
;
215 int i
, res
, bufsize
= 2 * key
->conf
.keylen
+ 2;
216 char *buf
= kmalloc(bufsize
, GFP_KERNEL
);
219 for (i
= 0; i
< key
->conf
.keylen
; i
++)
220 p
+= scnprintf(p
, bufsize
+ buf
- p
, "%02x", key
->conf
.key
[i
]);
221 p
+= scnprintf(p
, bufsize
+buf
-p
, "\n");
222 res
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
228 #define DEBUGFS_ADD(name) \
229 debugfs_create_file(#name, 0400, key->debugfs.dir, \
230 key, &key_##name##_ops);
232 void ieee80211_debugfs_key_add(struct ieee80211_key
*key
)
236 struct sta_info
*sta
;
238 if (!key
->local
->debugfs
.keys
)
241 sprintf(buf
, "%d", keycount
);
242 key
->debugfs
.cnt
= keycount
;
244 key
->debugfs
.dir
= debugfs_create_dir(buf
,
245 key
->local
->debugfs
.keys
);
247 if (!key
->debugfs
.dir
)
251 sta
= rcu_dereference(key
->sta
);
253 sprintf(buf
, "../../stations/%pM", sta
->sta
.addr
);
256 /* using sta as a boolean is fine outside RCU lock */
258 key
->debugfs
.stalink
=
259 debugfs_create_symlink("station", key
->debugfs
.dir
, buf
);
264 DEBUGFS_ADD(hw_key_idx
);
265 DEBUGFS_ADD(tx_rx_count
);
266 DEBUGFS_ADD(algorithm
);
267 DEBUGFS_ADD(tx_spec
);
268 DEBUGFS_ADD(rx_spec
);
269 DEBUGFS_ADD(replays
);
270 DEBUGFS_ADD(icverrors
);
272 DEBUGFS_ADD(ifindex
);
275 void ieee80211_debugfs_key_remove(struct ieee80211_key
*key
)
280 debugfs_remove_recursive(key
->debugfs
.dir
);
281 key
->debugfs
.dir
= NULL
;
283 void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data
*sdata
)
286 struct ieee80211_key
*key
;
288 if (!sdata
->debugfs
.dir
)
291 /* this is running under the key lock */
293 key
= sdata
->default_key
;
295 sprintf(buf
, "../keys/%d", key
->debugfs
.cnt
);
296 sdata
->debugfs
.default_key
=
297 debugfs_create_symlink("default_key",
298 sdata
->debugfs
.dir
, buf
);
300 ieee80211_debugfs_key_remove_default(sdata
);
303 void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data
*sdata
)
308 debugfs_remove(sdata
->debugfs
.default_key
);
309 sdata
->debugfs
.default_key
= NULL
;
312 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data
*sdata
)
315 struct ieee80211_key
*key
;
317 if (!sdata
->debugfs
.dir
)
320 /* this is running under the key lock */
322 key
= sdata
->default_mgmt_key
;
324 sprintf(buf
, "../keys/%d", key
->debugfs
.cnt
);
325 sdata
->debugfs
.default_mgmt_key
=
326 debugfs_create_symlink("default_mgmt_key",
327 sdata
->debugfs
.dir
, buf
);
329 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
332 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data
*sdata
)
337 debugfs_remove(sdata
->debugfs
.default_mgmt_key
);
338 sdata
->debugfs
.default_mgmt_key
= NULL
;
341 void ieee80211_debugfs_key_sta_del(struct ieee80211_key
*key
,
342 struct sta_info
*sta
)
344 debugfs_remove(key
->debugfs
.stalink
);
345 key
->debugfs
.stalink
= NULL
;