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
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
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) \
22 { \
23 struct ieee80211_key *key = file->private_data; \
24 return mac80211_format_buffer(userbuf, count, ppos, \
25 format_string, key->prop); \
26 }
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")
29
30 #define KEY_OPS(name) \
31 static const struct file_operations key_ ##name## _ops = { \
32 .read = key_##name##_read, \
33 .open = mac80211_open_file_generic, \
34 .llseek = generic_file_llseek, \
35 }
36
37 #define KEY_FILE(name, format) \
38 KEY_READ_##format(name) \
39 KEY_OPS(name)
40
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")
44
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, \
49 .llseek = generic_file_llseek, \
50 }
51
52 #define KEY_CONF_FILE(name, format) \
53 KEY_CONF_READ_##format(name) \
54 KEY_CONF_OPS(name)
55
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_FILE(tx_rx_count, D);
61 KEY_READ(ifindex, sdata->name, "%s\n");
62 KEY_OPS(ifindex);
63
key_algorithm_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)64 static ssize_t key_algorithm_read(struct file *file,
65 char __user *userbuf,
66 size_t count, loff_t *ppos)
67 {
68 char buf[15];
69 struct ieee80211_key *key = file->private_data;
70 u32 c = key->conf.cipher;
71
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));
75 }
76 KEY_OPS(algorithm);
77
key_tx_spec_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)78 static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
79 size_t count, loff_t *ppos)
80 {
81 const u8 *tpn;
82 char buf[20];
83 int len;
84 struct ieee80211_key *key = file->private_data;
85
86 switch (key->conf.cipher) {
87 case WLAN_CIPHER_SUITE_WEP40:
88 case WLAN_CIPHER_SUITE_WEP104:
89 len = scnprintf(buf, sizeof(buf), "\n");
90 break;
91 case WLAN_CIPHER_SUITE_TKIP:
92 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
93 key->u.tkip.tx.iv32,
94 key->u.tkip.tx.iv16);
95 break;
96 case WLAN_CIPHER_SUITE_CCMP:
97 tpn = key->u.ccmp.tx_pn;
98 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
99 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
100 break;
101 case WLAN_CIPHER_SUITE_AES_CMAC:
102 tpn = key->u.aes_cmac.tx_pn;
103 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
104 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
105 tpn[5]);
106 break;
107 default:
108 return 0;
109 }
110 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
111 }
112 KEY_OPS(tx_spec);
113
key_rx_spec_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)114 static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
115 size_t count, loff_t *ppos)
116 {
117 struct ieee80211_key *key = file->private_data;
118 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
119 int i, len;
120 const u8 *rpn;
121
122 switch (key->conf.cipher) {
123 case WLAN_CIPHER_SUITE_WEP40:
124 case WLAN_CIPHER_SUITE_WEP104:
125 len = scnprintf(buf, sizeof(buf), "\n");
126 break;
127 case WLAN_CIPHER_SUITE_TKIP:
128 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
129 p += scnprintf(p, sizeof(buf)+buf-p,
130 "%08x %04x\n",
131 key->u.tkip.rx[i].iv32,
132 key->u.tkip.rx[i].iv16);
133 len = p - buf;
134 break;
135 case WLAN_CIPHER_SUITE_CCMP:
136 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
137 rpn = key->u.ccmp.rx_pn[i];
138 p += scnprintf(p, sizeof(buf)+buf-p,
139 "%02x%02x%02x%02x%02x%02x\n",
140 rpn[0], rpn[1], rpn[2],
141 rpn[3], rpn[4], rpn[5]);
142 }
143 len = p - buf;
144 break;
145 case WLAN_CIPHER_SUITE_AES_CMAC:
146 rpn = key->u.aes_cmac.rx_pn;
147 p += scnprintf(p, sizeof(buf)+buf-p,
148 "%02x%02x%02x%02x%02x%02x\n",
149 rpn[0], rpn[1], rpn[2],
150 rpn[3], rpn[4], rpn[5]);
151 len = p - buf;
152 break;
153 default:
154 return 0;
155 }
156 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
157 }
158 KEY_OPS(rx_spec);
159
key_replays_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)160 static ssize_t key_replays_read(struct file *file, char __user *userbuf,
161 size_t count, loff_t *ppos)
162 {
163 struct ieee80211_key *key = file->private_data;
164 char buf[20];
165 int len;
166
167 switch (key->conf.cipher) {
168 case WLAN_CIPHER_SUITE_CCMP:
169 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
170 break;
171 case WLAN_CIPHER_SUITE_AES_CMAC:
172 len = scnprintf(buf, sizeof(buf), "%u\n",
173 key->u.aes_cmac.replays);
174 break;
175 default:
176 return 0;
177 }
178 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
179 }
180 KEY_OPS(replays);
181
key_icverrors_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)182 static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
183 size_t count, loff_t *ppos)
184 {
185 struct ieee80211_key *key = file->private_data;
186 char buf[20];
187 int len;
188
189 switch (key->conf.cipher) {
190 case WLAN_CIPHER_SUITE_AES_CMAC:
191 len = scnprintf(buf, sizeof(buf), "%u\n",
192 key->u.aes_cmac.icverrors);
193 break;
194 default:
195 return 0;
196 }
197 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
198 }
199 KEY_OPS(icverrors);
200
key_key_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)201 static ssize_t key_key_read(struct file *file, char __user *userbuf,
202 size_t count, loff_t *ppos)
203 {
204 struct ieee80211_key *key = file->private_data;
205 int i, bufsize = 2 * key->conf.keylen + 2;
206 char *buf = kmalloc(bufsize, GFP_KERNEL);
207 char *p = buf;
208 ssize_t res;
209
210 if (!buf)
211 return -ENOMEM;
212
213 for (i = 0; i < key->conf.keylen; i++)
214 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
215 p += scnprintf(p, bufsize+buf-p, "\n");
216 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
217 kfree(buf);
218 return res;
219 }
220 KEY_OPS(key);
221
222 #define DEBUGFS_ADD(name) \
223 debugfs_create_file(#name, 0400, key->debugfs.dir, \
224 key, &key_##name##_ops);
225
ieee80211_debugfs_key_add(struct ieee80211_key * key)226 void ieee80211_debugfs_key_add(struct ieee80211_key *key)
227 {
228 static int keycount;
229 char buf[50];
230 struct sta_info *sta;
231
232 if (!key->local->debugfs.keys)
233 return;
234
235 sprintf(buf, "%d", keycount);
236 key->debugfs.cnt = keycount;
237 keycount++;
238 key->debugfs.dir = debugfs_create_dir(buf,
239 key->local->debugfs.keys);
240
241 if (!key->debugfs.dir)
242 return;
243
244 rcu_read_lock();
245 sta = rcu_dereference(key->sta);
246 if (sta)
247 sprintf(buf, "../../stations/%pM", sta->sta.addr);
248 rcu_read_unlock();
249
250 /* using sta as a boolean is fine outside RCU lock */
251 if (sta)
252 key->debugfs.stalink =
253 debugfs_create_symlink("station", key->debugfs.dir, buf);
254
255 DEBUGFS_ADD(keylen);
256 DEBUGFS_ADD(flags);
257 DEBUGFS_ADD(keyidx);
258 DEBUGFS_ADD(hw_key_idx);
259 DEBUGFS_ADD(tx_rx_count);
260 DEBUGFS_ADD(algorithm);
261 DEBUGFS_ADD(tx_spec);
262 DEBUGFS_ADD(rx_spec);
263 DEBUGFS_ADD(replays);
264 DEBUGFS_ADD(icverrors);
265 DEBUGFS_ADD(key);
266 DEBUGFS_ADD(ifindex);
267 };
268
ieee80211_debugfs_key_remove(struct ieee80211_key * key)269 void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
270 {
271 if (!key)
272 return;
273
274 debugfs_remove_recursive(key->debugfs.dir);
275 key->debugfs.dir = NULL;
276 }
277
ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data * sdata)278 void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
279 {
280 char buf[50];
281 struct ieee80211_key *key;
282
283 if (!sdata->debugfs.dir)
284 return;
285
286 lockdep_assert_held(&sdata->local->key_mtx);
287
288 if (sdata->default_unicast_key) {
289 key = sdata->default_unicast_key;
290 sprintf(buf, "../keys/%d", key->debugfs.cnt);
291 sdata->debugfs.default_unicast_key =
292 debugfs_create_symlink("default_unicast_key",
293 sdata->debugfs.dir, buf);
294 } else {
295 debugfs_remove(sdata->debugfs.default_unicast_key);
296 sdata->debugfs.default_unicast_key = NULL;
297 }
298
299 if (sdata->default_multicast_key) {
300 key = sdata->default_multicast_key;
301 sprintf(buf, "../keys/%d", key->debugfs.cnt);
302 sdata->debugfs.default_multicast_key =
303 debugfs_create_symlink("default_multicast_key",
304 sdata->debugfs.dir, buf);
305 } else {
306 debugfs_remove(sdata->debugfs.default_multicast_key);
307 sdata->debugfs.default_multicast_key = NULL;
308 }
309 }
310
ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data * sdata)311 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
312 {
313 char buf[50];
314 struct ieee80211_key *key;
315
316 if (!sdata->debugfs.dir)
317 return;
318
319 /* this is running under the key lock */
320
321 key = sdata->default_mgmt_key;
322 if (key) {
323 sprintf(buf, "../keys/%d", key->debugfs.cnt);
324 sdata->debugfs.default_mgmt_key =
325 debugfs_create_symlink("default_mgmt_key",
326 sdata->debugfs.dir, buf);
327 } else
328 ieee80211_debugfs_key_remove_mgmt_default(sdata);
329 }
330
ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data * sdata)331 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
332 {
333 if (!sdata)
334 return;
335
336 debugfs_remove(sdata->debugfs.default_mgmt_key);
337 sdata->debugfs.default_mgmt_key = NULL;
338 }
339
ieee80211_debugfs_key_sta_del(struct ieee80211_key * key,struct sta_info * sta)340 void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
341 struct sta_info *sta)
342 {
343 debugfs_remove(key->debugfs.stalink);
344 key->debugfs.stalink = NULL;
345 }
346