blob: 1647f8dc5cdab475d727c49060c1db0985aaa76d [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
Johannes Berg8f20fc22007-08-28 17:01:54 -040018#define KEY_READ(name, prop, buflen, 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{ \
23 char buf[buflen]; \
24 struct ieee80211_key *key = file->private_data; \
Johannes Berg8f20fc22007-08-28 17:01:54 -040025 int res = scnprintf(buf, buflen, format_string, key->prop); \
Jiri Bence9f207f2007-05-05 11:46:38 -070026 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
27}
Johannes Berg8f20fc22007-08-28 17:01:54 -040028#define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n")
Johannes Berg11a843b2007-08-28 17:01:55 -040029#define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n")
Jiri Bence9f207f2007-05-05 11:46:38 -070030
31#define KEY_OPS(name) \
32static const struct file_operations key_ ##name## _ops = { \
33 .read = key_##name##_read, \
34 .open = mac80211_open_file_generic, \
35}
36
37#define KEY_FILE(name, format) \
38 KEY_READ_##format(name) \
39 KEY_OPS(name)
40
Johannes Berg8f20fc22007-08-28 17:01:54 -040041#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")
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, \
48 .open = mac80211_open_file_generic, \
49}
50
51#define KEY_CONF_FILE(name, format) \
52 KEY_CONF_READ_##format(name) \
53 KEY_CONF_OPS(name)
54
55KEY_CONF_FILE(keylen, D);
56KEY_CONF_FILE(keyidx, D);
57KEY_CONF_FILE(hw_key_idx, D);
Johannes Berg11a843b2007-08-28 17:01:55 -040058KEY_FILE(flags, X);
Jiri Bence9f207f2007-05-05 11:46:38 -070059KEY_FILE(tx_rx_count, D);
Johannes Berg47846c92009-11-25 17:46:19 +010060KEY_READ(ifindex, sdata->name, IFNAMSIZ + 2, "%s\n");
Johannes Berge7a64f12007-08-28 17:01:55 -040061KEY_OPS(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -070062
63static ssize_t key_algorithm_read(struct file *file,
64 char __user *userbuf,
65 size_t count, loff_t *ppos)
66{
Johannes Berg97359d12010-08-10 09:46:38 +020067 char buf[15];
Jiri Bence9f207f2007-05-05 11:46:38 -070068 struct ieee80211_key *key = file->private_data;
Johannes Berg97359d12010-08-10 09:46:38 +020069 u32 c = key->conf.cipher;
Jiri Bence9f207f2007-05-05 11:46:38 -070070
Johannes Berg97359d12010-08-10 09:46:38 +020071 sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
72 c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
73 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
Jiri Bence9f207f2007-05-05 11:46:38 -070074}
75KEY_OPS(algorithm);
76
77static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
78 size_t count, loff_t *ppos)
79{
80 const u8 *tpn;
81 char buf[20];
82 int len;
83 struct ieee80211_key *key = file->private_data;
84
Johannes Berg97359d12010-08-10 09:46:38 +020085 switch (key->conf.cipher) {
86 case WLAN_CIPHER_SUITE_WEP40:
87 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -070088 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -040089 break;
Johannes Berg97359d12010-08-10 09:46:38 +020090 case WLAN_CIPHER_SUITE_TKIP:
Jiri Bence9f207f2007-05-05 11:46:38 -070091 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -070092 key->u.tkip.tx.iv32,
93 key->u.tkip.tx.iv16);
Johannes Berg50339a62007-08-28 17:01:52 -040094 break;
Johannes Berg97359d12010-08-10 09:46:38 +020095 case WLAN_CIPHER_SUITE_CCMP:
Jiri Bence9f207f2007-05-05 11:46:38 -070096 tpn = key->u.ccmp.tx_pn;
97 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
98 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
Johannes Berg50339a62007-08-28 17:01:52 -040099 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200100 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200101 tpn = key->u.aes_cmac.tx_pn;
102 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
103 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
104 tpn[5]);
105 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700106 default:
107 return 0;
108 }
109 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
110}
111KEY_OPS(tx_spec);
112
113static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
114 size_t count, loff_t *ppos)
115{
116 struct ieee80211_key *key = file->private_data;
117 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
118 int i, len;
119 const u8 *rpn;
120
Johannes Berg97359d12010-08-10 09:46:38 +0200121 switch (key->conf.cipher) {
122 case WLAN_CIPHER_SUITE_WEP40:
123 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -0700124 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -0400125 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200126 case WLAN_CIPHER_SUITE_TKIP:
Jiri Bence9f207f2007-05-05 11:46:38 -0700127 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
128 p += scnprintf(p, sizeof(buf)+buf-p,
129 "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700130 key->u.tkip.rx[i].iv32,
131 key->u.tkip.rx[i].iv16);
Jiri Bence9f207f2007-05-05 11:46:38 -0700132 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400133 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200134 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen91902522010-06-11 10:27:33 -0700135 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700136 rpn = key->u.ccmp.rx_pn[i];
137 p += scnprintf(p, sizeof(buf)+buf-p,
138 "%02x%02x%02x%02x%02x%02x\n",
139 rpn[0], rpn[1], rpn[2],
140 rpn[3], rpn[4], rpn[5]);
141 }
142 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400143 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200144 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200145 rpn = key->u.aes_cmac.rx_pn;
146 p += scnprintf(p, sizeof(buf)+buf-p,
147 "%02x%02x%02x%02x%02x%02x\n",
148 rpn[0], rpn[1], rpn[2],
149 rpn[3], rpn[4], rpn[5]);
150 len = p - buf;
151 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700152 default:
153 return 0;
154 }
155 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
156}
157KEY_OPS(rx_spec);
158
159static ssize_t key_replays_read(struct file *file, char __user *userbuf,
160 size_t count, loff_t *ppos)
161{
162 struct ieee80211_key *key = file->private_data;
163 char buf[20];
164 int len;
165
Johannes Berg97359d12010-08-10 09:46:38 +0200166 switch (key->conf.cipher) {
167 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200168 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
169 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200170 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200171 len = scnprintf(buf, sizeof(buf), "%u\n",
172 key->u.aes_cmac.replays);
173 break;
174 default:
Jiri Bence9f207f2007-05-05 11:46:38 -0700175 return 0;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200176 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700177 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
178}
179KEY_OPS(replays);
180
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200181static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
182 size_t count, loff_t *ppos)
183{
184 struct ieee80211_key *key = file->private_data;
185 char buf[20];
186 int len;
187
Johannes Berg97359d12010-08-10 09:46:38 +0200188 switch (key->conf.cipher) {
189 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200190 len = scnprintf(buf, sizeof(buf), "%u\n",
191 key->u.aes_cmac.icverrors);
192 break;
193 default:
194 return 0;
195 }
196 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
197}
198KEY_OPS(icverrors);
199
Jiri Bence9f207f2007-05-05 11:46:38 -0700200static ssize_t key_key_read(struct file *file, char __user *userbuf,
201 size_t count, loff_t *ppos)
202{
203 struct ieee80211_key *key = file->private_data;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400204 int i, res, bufsize = 2 * key->conf.keylen + 2;
Jiri Bence9f207f2007-05-05 11:46:38 -0700205 char *buf = kmalloc(bufsize, GFP_KERNEL);
206 char *p = buf;
207
Johannes Berg8f20fc22007-08-28 17:01:54 -0400208 for (i = 0; i < key->conf.keylen; i++)
209 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
Jiri Bence9f207f2007-05-05 11:46:38 -0700210 p += scnprintf(p, bufsize+buf-p, "\n");
211 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
212 kfree(buf);
213 return res;
214}
215KEY_OPS(key);
216
217#define DEBUGFS_ADD(name) \
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100218 debugfs_create_file(#name, 0400, key->debugfs.dir, \
219 key, &key_##name##_ops);
Jiri Bence9f207f2007-05-05 11:46:38 -0700220
Johannes Berg3b967662008-04-08 17:56:52 +0200221void ieee80211_debugfs_key_add(struct ieee80211_key *key)
222 {
Johannes Berg50339a62007-08-28 17:01:52 -0400223 static int keycount;
Johannes Berg3b967662008-04-08 17:56:52 +0200224 char buf[50];
Johannes Berg3b967662008-04-08 17:56:52 +0200225 struct sta_info *sta;
Jiri Bence9f207f2007-05-05 11:46:38 -0700226
Johannes Berg3b967662008-04-08 17:56:52 +0200227 if (!key->local->debugfs.keys)
Jiri Bence9f207f2007-05-05 11:46:38 -0700228 return;
229
Johannes Berg50339a62007-08-28 17:01:52 -0400230 sprintf(buf, "%d", keycount);
Johannes Bergd9c58f32008-04-08 16:46:36 -0400231 key->debugfs.cnt = keycount;
Johannes Berg50339a62007-08-28 17:01:52 -0400232 keycount++;
Jiri Bence9f207f2007-05-05 11:46:38 -0700233 key->debugfs.dir = debugfs_create_dir(buf,
Johannes Berg3b967662008-04-08 17:56:52 +0200234 key->local->debugfs.keys);
Jiri Bence9f207f2007-05-05 11:46:38 -0700235
236 if (!key->debugfs.dir)
237 return;
238
Johannes Berg3b967662008-04-08 17:56:52 +0200239 rcu_read_lock();
240 sta = rcu_dereference(key->sta);
241 if (sta)
Johannes Berg0c68ae262008-10-27 15:56:10 -0700242 sprintf(buf, "../../stations/%pM", sta->sta.addr);
Johannes Berg3b967662008-04-08 17:56:52 +0200243 rcu_read_unlock();
244
245 /* using sta as a boolean is fine outside RCU lock */
246 if (sta)
247 key->debugfs.stalink =
248 debugfs_create_symlink("station", key->debugfs.dir, buf);
249
Jiri Bence9f207f2007-05-05 11:46:38 -0700250 DEBUGFS_ADD(keylen);
Johannes Berg8f20fc22007-08-28 17:01:54 -0400251 DEBUGFS_ADD(flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700252 DEBUGFS_ADD(keyidx);
253 DEBUGFS_ADD(hw_key_idx);
254 DEBUGFS_ADD(tx_rx_count);
255 DEBUGFS_ADD(algorithm);
256 DEBUGFS_ADD(tx_spec);
257 DEBUGFS_ADD(rx_spec);
258 DEBUGFS_ADD(replays);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200259 DEBUGFS_ADD(icverrors);
Jiri Bence9f207f2007-05-05 11:46:38 -0700260 DEBUGFS_ADD(key);
Johannes Berge7a64f12007-08-28 17:01:55 -0400261 DEBUGFS_ADD(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -0700262};
263
Jiri Bence9f207f2007-05-05 11:46:38 -0700264void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
265{
266 if (!key)
267 return;
268
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100269 debugfs_remove_recursive(key->debugfs.dir);
Jiri Bence9f207f2007-05-05 11:46:38 -0700270 key->debugfs.dir = NULL;
271}
272void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
273{
274 char buf[50];
Johannes Berg78520ca2008-05-03 01:04:47 +0200275 struct ieee80211_key *key;
Jiri Bence9f207f2007-05-05 11:46:38 -0700276
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100277 if (!sdata->debugfs.dir)
Jiri Bence9f207f2007-05-05 11:46:38 -0700278 return;
279
Johannes Berg78520ca2008-05-03 01:04:47 +0200280 /* this is running under the key lock */
281
282 key = sdata->default_key;
283 if (key) {
284 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100285 sdata->debugfs.default_key =
Johannes Berg78520ca2008-05-03 01:04:47 +0200286 debugfs_create_symlink("default_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100287 sdata->debugfs.dir, buf);
Johannes Berg78520ca2008-05-03 01:04:47 +0200288 } else
289 ieee80211_debugfs_key_remove_default(sdata);
Jiri Bence9f207f2007-05-05 11:46:38 -0700290}
Johannes Berg78520ca2008-05-03 01:04:47 +0200291
Jiri Bence9f207f2007-05-05 11:46:38 -0700292void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
293{
294 if (!sdata)
295 return;
296
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100297 debugfs_remove(sdata->debugfs.default_key);
298 sdata->debugfs.default_key = NULL;
Jiri Bence9f207f2007-05-05 11:46:38 -0700299}
Jiri Bence9f207f2007-05-05 11:46:38 -0700300
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200301void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
302{
303 char buf[50];
304 struct ieee80211_key *key;
305
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100306 if (!sdata->debugfs.dir)
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200307 return;
308
309 /* this is running under the key lock */
310
311 key = sdata->default_mgmt_key;
312 if (key) {
313 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100314 sdata->debugfs.default_mgmt_key =
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200315 debugfs_create_symlink("default_mgmt_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100316 sdata->debugfs.dir, buf);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200317 } else
318 ieee80211_debugfs_key_remove_mgmt_default(sdata);
319}
320
321void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
322{
323 if (!sdata)
324 return;
325
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100326 debugfs_remove(sdata->debugfs.default_mgmt_key);
327 sdata->debugfs.default_mgmt_key = NULL;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200328}
329
Jiri Bence9f207f2007-05-05 11:46:38 -0700330void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
331 struct sta_info *sta)
332{
333 debugfs_remove(key->debugfs.stalink);
334 key->debugfs.stalink = NULL;
335}