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, format_string) \
19 static ssize_t key_##name##_read(struct file *file, \
20 char __user *userbuf, \
21 size_t count, loff_t *ppos) \
23 struct ieee80211_key *key = file->private_data; \
24 return mac80211_format_buffer(userbuf, count, ppos, \
25 format_string, key->prop); \
27 #define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
28 #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
30 #define KEY_OPS(name) \
31 static const struct file_operations key_ ##name## _ops = { \
32 .read = key_##name##_read, \
33 .open = simple_open, \
34 .llseek = generic_file_llseek, \
37 #define KEY_FILE(name, format) \
38 KEY_READ_##format(name) \
41 #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")
45 #define KEY_CONF_OPS(name) \
46 static const struct file_operations key_ ##name## _ops = { \
47 .read = key_conf_##name##_read, \
48 .open = simple_open, \
49 .llseek = generic_file_llseek, \
52 #define KEY_CONF_FILE(name, format) \
53 KEY_CONF_READ_##format(name) \
56 KEY_CONF_FILE(keylen
, D
);
57 KEY_CONF_FILE(keyidx
, D
);
58 KEY_CONF_FILE(hw_key_idx
, D
);
60 KEY_FILE(tx_rx_count
, D
);
61 KEY_READ(ifindex
, sdata
->name
, "%s\n");
64 static ssize_t
key_algorithm_read(struct file
*file
,
66 size_t count
, loff_t
*ppos
)
69 struct ieee80211_key
*key
= file
->private_data
;
70 u32 c
= key
->conf
.cipher
;
72 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
));
78 static ssize_t
key_tx_spec_read(struct file
*file
, char __user
*userbuf
,
79 size_t count
, loff_t
*ppos
)
84 struct ieee80211_key
*key
= file
->private_data
;
86 switch (key
->conf
.cipher
) {
87 case WLAN_CIPHER_SUITE_WEP40
:
88 case WLAN_CIPHER_SUITE_WEP104
:
89 len
= scnprintf(buf
, sizeof(buf
), "\n");
91 case WLAN_CIPHER_SUITE_TKIP
:
92 len
= scnprintf(buf
, sizeof(buf
), "%08x %04x\n",
96 case WLAN_CIPHER_SUITE_CCMP
:
97 pn
= atomic64_read(&key
->u
.ccmp
.tx_pn
);
98 len
= scnprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x%02x%02x\n",
99 (u8
)(pn
>> 40), (u8
)(pn
>> 32), (u8
)(pn
>> 24),
100 (u8
)(pn
>> 16), (u8
)(pn
>> 8), (u8
)pn
);
102 case WLAN_CIPHER_SUITE_AES_CMAC
:
103 pn
= atomic64_read(&key
->u
.aes_cmac
.tx_pn
);
104 len
= scnprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x%02x%02x\n",
105 (u8
)(pn
>> 40), (u8
)(pn
>> 32), (u8
)(pn
>> 24),
106 (u8
)(pn
>> 16), (u8
)(pn
>> 8), (u8
)pn
);
111 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
115 static ssize_t
key_rx_spec_read(struct file
*file
, char __user
*userbuf
,
116 size_t count
, loff_t
*ppos
)
118 struct ieee80211_key
*key
= file
->private_data
;
119 char buf
[14*IEEE80211_NUM_TIDS
+1], *p
= buf
;
123 switch (key
->conf
.cipher
) {
124 case WLAN_CIPHER_SUITE_WEP40
:
125 case WLAN_CIPHER_SUITE_WEP104
:
126 len
= scnprintf(buf
, sizeof(buf
), "\n");
128 case WLAN_CIPHER_SUITE_TKIP
:
129 for (i
= 0; i
< IEEE80211_NUM_TIDS
; i
++)
130 p
+= scnprintf(p
, sizeof(buf
)+buf
-p
,
132 key
->u
.tkip
.rx
[i
].iv32
,
133 key
->u
.tkip
.rx
[i
].iv16
);
136 case WLAN_CIPHER_SUITE_CCMP
:
137 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++) {
138 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]);
146 case WLAN_CIPHER_SUITE_AES_CMAC
:
147 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]);
157 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
161 static ssize_t
key_replays_read(struct file
*file
, char __user
*userbuf
,
162 size_t count
, loff_t
*ppos
)
164 struct ieee80211_key
*key
= file
->private_data
;
168 switch (key
->conf
.cipher
) {
169 case WLAN_CIPHER_SUITE_CCMP
:
170 len
= scnprintf(buf
, sizeof(buf
), "%u\n", key
->u
.ccmp
.replays
);
172 case WLAN_CIPHER_SUITE_AES_CMAC
:
173 len
= scnprintf(buf
, sizeof(buf
), "%u\n",
174 key
->u
.aes_cmac
.replays
);
179 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
183 static ssize_t
key_icverrors_read(struct file
*file
, char __user
*userbuf
,
184 size_t count
, loff_t
*ppos
)
186 struct ieee80211_key
*key
= file
->private_data
;
190 switch (key
->conf
.cipher
) {
191 case WLAN_CIPHER_SUITE_AES_CMAC
:
192 len
= scnprintf(buf
, sizeof(buf
), "%u\n",
193 key
->u
.aes_cmac
.icverrors
);
198 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
202 static ssize_t
key_mic_failures_read(struct file
*file
, char __user
*userbuf
,
203 size_t count
, loff_t
*ppos
)
205 struct ieee80211_key
*key
= file
->private_data
;
209 if (key
->conf
.cipher
!= WLAN_CIPHER_SUITE_TKIP
)
212 len
= scnprintf(buf
, sizeof(buf
), "%u\n", key
->u
.tkip
.mic_failures
);
214 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
216 KEY_OPS(mic_failures
);
218 static ssize_t
key_key_read(struct file
*file
, char __user
*userbuf
,
219 size_t count
, loff_t
*ppos
)
221 struct ieee80211_key
*key
= file
->private_data
;
222 int i
, bufsize
= 2 * key
->conf
.keylen
+ 2;
223 char *buf
= kmalloc(bufsize
, GFP_KERNEL
);
230 for (i
= 0; i
< key
->conf
.keylen
; i
++)
231 p
+= scnprintf(p
, bufsize
+ buf
- p
, "%02x", key
->conf
.key
[i
]);
232 p
+= scnprintf(p
, bufsize
+buf
-p
, "\n");
233 res
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, p
- buf
);
239 #define DEBUGFS_ADD(name) \
240 debugfs_create_file(#name, 0400, key->debugfs.dir, \
241 key, &key_##name##_ops);
243 void ieee80211_debugfs_key_add(struct ieee80211_key
*key
)
247 struct sta_info
*sta
;
249 if (!key
->local
->debugfs
.keys
)
252 sprintf(buf
, "%d", keycount
);
253 key
->debugfs
.cnt
= keycount
;
255 key
->debugfs
.dir
= debugfs_create_dir(buf
,
256 key
->local
->debugfs
.keys
);
258 if (!key
->debugfs
.dir
)
263 sprintf(buf
, "../../netdev:%s/stations/%pM",
264 sta
->sdata
->name
, sta
->sta
.addr
);
265 key
->debugfs
.stalink
=
266 debugfs_create_symlink("station", key
->debugfs
.dir
, buf
);
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
);
278 DEBUGFS_ADD(icverrors
);
279 DEBUGFS_ADD(mic_failures
);
281 DEBUGFS_ADD(ifindex
);
284 void ieee80211_debugfs_key_remove(struct ieee80211_key
*key
)
289 debugfs_remove_recursive(key
->debugfs
.dir
);
290 key
->debugfs
.dir
= NULL
;
293 void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data
*sdata
)
296 struct ieee80211_key
*key
;
298 if (!sdata
->vif
.debugfs_dir
)
301 lockdep_assert_held(&sdata
->local
->key_mtx
);
303 if (sdata
->debugfs
.default_unicast_key
) {
304 debugfs_remove(sdata
->debugfs
.default_unicast_key
);
305 sdata
->debugfs
.default_unicast_key
= NULL
;
308 if (sdata
->default_unicast_key
) {
309 key
= key_mtx_dereference(sdata
->local
,
310 sdata
->default_unicast_key
);
311 sprintf(buf
, "../keys/%d", key
->debugfs
.cnt
);
312 sdata
->debugfs
.default_unicast_key
=
313 debugfs_create_symlink("default_unicast_key",
314 sdata
->vif
.debugfs_dir
, buf
);
317 if (sdata
->debugfs
.default_multicast_key
) {
318 debugfs_remove(sdata
->debugfs
.default_multicast_key
);
319 sdata
->debugfs
.default_multicast_key
= NULL
;
322 if (sdata
->default_multicast_key
) {
323 key
= key_mtx_dereference(sdata
->local
,
324 sdata
->default_multicast_key
);
325 sprintf(buf
, "../keys/%d", key
->debugfs
.cnt
);
326 sdata
->debugfs
.default_multicast_key
=
327 debugfs_create_symlink("default_multicast_key",
328 sdata
->vif
.debugfs_dir
, buf
);
332 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data
*sdata
)
335 struct ieee80211_key
*key
;
337 if (!sdata
->vif
.debugfs_dir
)
340 key
= key_mtx_dereference(sdata
->local
,
341 sdata
->default_mgmt_key
);
343 sprintf(buf
, "../keys/%d", key
->debugfs
.cnt
);
344 sdata
->debugfs
.default_mgmt_key
=
345 debugfs_create_symlink("default_mgmt_key",
346 sdata
->vif
.debugfs_dir
, buf
);
348 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
351 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data
*sdata
)
356 debugfs_remove(sdata
->debugfs
.default_mgmt_key
);
357 sdata
->debugfs
.default_mgmt_key
= NULL
;
360 void ieee80211_debugfs_key_sta_del(struct ieee80211_key
*key
,
361 struct sta_info
*sta
)
363 debugfs_remove(key
->debugfs
.stalink
);
364 key
->debugfs
.stalink
= NULL
;