blob: 4433760db4c783e0c88256a897a7ea29cc2341e7 [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, \
33 .open = mac80211_open_file_generic, \
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, \
48 .open = mac80211_open_file_generic, \
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{
81 const u8 *tpn;
Johannes Bergaba83a0b2011-07-06 21:59:39 +020082 u64 pn;
Jiri Bence9f207f2007-05-05 11:46:38 -070083 char buf[20];
84 int len;
85 struct ieee80211_key *key = file->private_data;
86
Johannes Berg97359d12010-08-10 09:46:38 +020087 switch (key->conf.cipher) {
88 case WLAN_CIPHER_SUITE_WEP40:
89 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -070090 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -040091 break;
Johannes Berg97359d12010-08-10 09:46:38 +020092 case WLAN_CIPHER_SUITE_TKIP:
Jiri Bence9f207f2007-05-05 11:46:38 -070093 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -070094 key->u.tkip.tx.iv32,
95 key->u.tkip.tx.iv16);
Johannes Berg50339a62007-08-28 17:01:52 -040096 break;
Johannes Berg97359d12010-08-10 09:46:38 +020097 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergaba83a0b2011-07-06 21:59:39 +020098 pn = atomic64_read(&key->u.ccmp.tx_pn);
Jiri Bence9f207f2007-05-05 11:46:38 -070099 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
Johannes Bergaba83a0b2011-07-06 21:59:39 +0200100 (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
101 (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
Johannes Berg50339a62007-08-28 17:01:52 -0400102 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200103 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200104 tpn = key->u.aes_cmac.tx_pn;
105 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
106 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
107 tpn[5]);
108 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700109 default:
110 return 0;
111 }
112 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
113}
114KEY_OPS(tx_spec);
115
116static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
117 size_t count, loff_t *ppos)
118{
119 struct ieee80211_key *key = file->private_data;
120 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
121 int i, len;
122 const u8 *rpn;
123
Johannes Berg97359d12010-08-10 09:46:38 +0200124 switch (key->conf.cipher) {
125 case WLAN_CIPHER_SUITE_WEP40:
126 case WLAN_CIPHER_SUITE_WEP104:
Jiri Bence9f207f2007-05-05 11:46:38 -0700127 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -0400128 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200129 case WLAN_CIPHER_SUITE_TKIP:
Jiri Bence9f207f2007-05-05 11:46:38 -0700130 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
131 p += scnprintf(p, sizeof(buf)+buf-p,
132 "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700133 key->u.tkip.rx[i].iv32,
134 key->u.tkip.rx[i].iv16);
Jiri Bence9f207f2007-05-05 11:46:38 -0700135 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400136 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200137 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen91902522010-06-11 10:27:33 -0700138 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700139 rpn = key->u.ccmp.rx_pn[i];
140 p += scnprintf(p, sizeof(buf)+buf-p,
141 "%02x%02x%02x%02x%02x%02x\n",
142 rpn[0], rpn[1], rpn[2],
143 rpn[3], rpn[4], rpn[5]);
144 }
145 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400146 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200147 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200148 rpn = key->u.aes_cmac.rx_pn;
149 p += scnprintf(p, sizeof(buf)+buf-p,
150 "%02x%02x%02x%02x%02x%02x\n",
151 rpn[0], rpn[1], rpn[2],
152 rpn[3], rpn[4], rpn[5]);
153 len = p - buf;
154 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700155 default:
156 return 0;
157 }
158 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
159}
160KEY_OPS(rx_spec);
161
162static ssize_t key_replays_read(struct file *file, char __user *userbuf,
163 size_t count, loff_t *ppos)
164{
165 struct ieee80211_key *key = file->private_data;
166 char buf[20];
167 int len;
168
Johannes Berg97359d12010-08-10 09:46:38 +0200169 switch (key->conf.cipher) {
170 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200171 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
172 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200173 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200174 len = scnprintf(buf, sizeof(buf), "%u\n",
175 key->u.aes_cmac.replays);
176 break;
177 default:
Jiri Bence9f207f2007-05-05 11:46:38 -0700178 return 0;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200179 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700180 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
181}
182KEY_OPS(replays);
183
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200184static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
185 size_t count, loff_t *ppos)
186{
187 struct ieee80211_key *key = file->private_data;
188 char buf[20];
189 int len;
190
Johannes Berg97359d12010-08-10 09:46:38 +0200191 switch (key->conf.cipher) {
192 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200193 len = scnprintf(buf, sizeof(buf), "%u\n",
194 key->u.aes_cmac.icverrors);
195 break;
196 default:
197 return 0;
198 }
199 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
200}
201KEY_OPS(icverrors);
202
Jiri Bence9f207f2007-05-05 11:46:38 -0700203static ssize_t key_key_read(struct file *file, char __user *userbuf,
204 size_t count, loff_t *ppos)
205{
206 struct ieee80211_key *key = file->private_data;
Jesper Juhl520efd12010-10-29 16:10:26 +0200207 int i, bufsize = 2 * key->conf.keylen + 2;
Jiri Bence9f207f2007-05-05 11:46:38 -0700208 char *buf = kmalloc(bufsize, GFP_KERNEL);
209 char *p = buf;
Jesper Juhl520efd12010-10-29 16:10:26 +0200210 ssize_t res;
211
212 if (!buf)
213 return -ENOMEM;
Jiri Bence9f207f2007-05-05 11:46:38 -0700214
Johannes Berg8f20fc22007-08-28 17:01:54 -0400215 for (i = 0; i < key->conf.keylen; i++)
216 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
Jiri Bence9f207f2007-05-05 11:46:38 -0700217 p += scnprintf(p, bufsize+buf-p, "\n");
218 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
219 kfree(buf);
220 return res;
221}
222KEY_OPS(key);
223
224#define DEBUGFS_ADD(name) \
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100225 debugfs_create_file(#name, 0400, key->debugfs.dir, \
226 key, &key_##name##_ops);
Jiri Bence9f207f2007-05-05 11:46:38 -0700227
Johannes Berg3b967662008-04-08 17:56:52 +0200228void ieee80211_debugfs_key_add(struct ieee80211_key *key)
229 {
Johannes Berg50339a62007-08-28 17:01:52 -0400230 static int keycount;
Johannes Berg3b967662008-04-08 17:56:52 +0200231 char buf[50];
Johannes Berg3b967662008-04-08 17:56:52 +0200232 struct sta_info *sta;
Jiri Bence9f207f2007-05-05 11:46:38 -0700233
Johannes Berg3b967662008-04-08 17:56:52 +0200234 if (!key->local->debugfs.keys)
Jiri Bence9f207f2007-05-05 11:46:38 -0700235 return;
236
Johannes Berg50339a62007-08-28 17:01:52 -0400237 sprintf(buf, "%d", keycount);
Johannes Bergd9c58f32008-04-08 16:46:36 -0400238 key->debugfs.cnt = keycount;
Johannes Berg50339a62007-08-28 17:01:52 -0400239 keycount++;
Jiri Bence9f207f2007-05-05 11:46:38 -0700240 key->debugfs.dir = debugfs_create_dir(buf,
Johannes Berg3b967662008-04-08 17:56:52 +0200241 key->local->debugfs.keys);
Jiri Bence9f207f2007-05-05 11:46:38 -0700242
243 if (!key->debugfs.dir)
244 return;
245
Johannes Berg40b275b2011-05-13 14:15:49 +0200246 sta = key->sta;
247 if (sta) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700248 sprintf(buf, "../../stations/%pM", sta->sta.addr);
Johannes Berg3b967662008-04-08 17:56:52 +0200249 key->debugfs.stalink =
250 debugfs_create_symlink("station", key->debugfs.dir, buf);
Johannes Berg40b275b2011-05-13 14:15:49 +0200251 }
Johannes Berg3b967662008-04-08 17:56:52 +0200252
Jiri Bence9f207f2007-05-05 11:46:38 -0700253 DEBUGFS_ADD(keylen);
Johannes Berg8f20fc22007-08-28 17:01:54 -0400254 DEBUGFS_ADD(flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700255 DEBUGFS_ADD(keyidx);
256 DEBUGFS_ADD(hw_key_idx);
257 DEBUGFS_ADD(tx_rx_count);
258 DEBUGFS_ADD(algorithm);
259 DEBUGFS_ADD(tx_spec);
260 DEBUGFS_ADD(rx_spec);
261 DEBUGFS_ADD(replays);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200262 DEBUGFS_ADD(icverrors);
Jiri Bence9f207f2007-05-05 11:46:38 -0700263 DEBUGFS_ADD(key);
Johannes Berge7a64f12007-08-28 17:01:55 -0400264 DEBUGFS_ADD(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -0700265};
266
Jiri Bence9f207f2007-05-05 11:46:38 -0700267void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
268{
269 if (!key)
270 return;
271
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100272 debugfs_remove_recursive(key->debugfs.dir);
Jiri Bence9f207f2007-05-05 11:46:38 -0700273 key->debugfs.dir = NULL;
274}
Johannes Bergf7e01042010-12-09 19:49:02 +0100275
276void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
Jiri Bence9f207f2007-05-05 11:46:38 -0700277{
278 char buf[50];
Johannes Berg78520ca2008-05-03 01:04:47 +0200279 struct ieee80211_key *key;
Jiri Bence9f207f2007-05-05 11:46:38 -0700280
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100281 if (!sdata->debugfs.dir)
Jiri Bence9f207f2007-05-05 11:46:38 -0700282 return;
283
Johannes Bergf7e01042010-12-09 19:49:02 +0100284 lockdep_assert_held(&sdata->local->key_mtx);
Johannes Berg78520ca2008-05-03 01:04:47 +0200285
Johannes Bergf7e01042010-12-09 19:49:02 +0100286 if (sdata->default_unicast_key) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200287 key = key_mtx_dereference(sdata->local,
288 sdata->default_unicast_key);
Johannes Berg78520ca2008-05-03 01:04:47 +0200289 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Bergf7e01042010-12-09 19:49:02 +0100290 sdata->debugfs.default_unicast_key =
291 debugfs_create_symlink("default_unicast_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100292 sdata->debugfs.dir, buf);
Johannes Bergf7e01042010-12-09 19:49:02 +0100293 } else {
294 debugfs_remove(sdata->debugfs.default_unicast_key);
295 sdata->debugfs.default_unicast_key = NULL;
296 }
Johannes Berg78520ca2008-05-03 01:04:47 +0200297
Johannes Bergf7e01042010-12-09 19:49:02 +0100298 if (sdata->default_multicast_key) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200299 key = key_mtx_dereference(sdata->local,
300 sdata->default_multicast_key);
Johannes Bergf7e01042010-12-09 19:49:02 +0100301 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 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700309}
Jiri Bence9f207f2007-05-05 11:46:38 -0700310
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200311void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
312{
313 char buf[50];
314 struct ieee80211_key *key;
315
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100316 if (!sdata->debugfs.dir)
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200317 return;
318
Johannes Berg40b275b2011-05-13 14:15:49 +0200319 key = key_mtx_dereference(sdata->local,
320 sdata->default_mgmt_key);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200321 if (key) {
322 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100323 sdata->debugfs.default_mgmt_key =
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200324 debugfs_create_symlink("default_mgmt_key",
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100325 sdata->debugfs.dir, buf);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200326 } else
327 ieee80211_debugfs_key_remove_mgmt_default(sdata);
328}
329
330void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
331{
332 if (!sdata)
333 return;
334
Johannes Berg7bcfaf22009-10-27 12:59:03 +0100335 debugfs_remove(sdata->debugfs.default_mgmt_key);
336 sdata->debugfs.default_mgmt_key = NULL;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200337}
338
Jiri Bence9f207f2007-05-05 11:46:38 -0700339void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
340 struct sta_info *sta)
341{
342 debugfs_remove(key->debugfs.stalink);
343 key->debugfs.stalink = NULL;
344}