blob: a3294d109322d61583afe0a967260e2cc1236d62 [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>
12#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "key.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070014#include "debugfs.h"
15#include "debugfs_key.h"
16
Johannes Berg8f20fc22007-08-28 17:01:54 -040017#define KEY_READ(name, prop, buflen, format_string) \
Jiri Bence9f207f2007-05-05 11:46:38 -070018static ssize_t key_##name##_read(struct file *file, \
19 char __user *userbuf, \
20 size_t count, loff_t *ppos) \
21{ \
22 char buf[buflen]; \
23 struct ieee80211_key *key = file->private_data; \
Johannes Berg8f20fc22007-08-28 17:01:54 -040024 int res = scnprintf(buf, buflen, format_string, key->prop); \
Jiri Bence9f207f2007-05-05 11:46:38 -070025 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
26}
Johannes Berg8f20fc22007-08-28 17:01:54 -040027#define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n")
Johannes Berg11a843b2007-08-28 17:01:55 -040028#define KEY_READ_X(name) KEY_READ(name, name, 20, "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, \
34}
35
36#define KEY_FILE(name, format) \
37 KEY_READ_##format(name) \
38 KEY_OPS(name)
39
Johannes Berg8f20fc22007-08-28 17:01:54 -040040#define KEY_CONF_READ(name, buflen, format_string) \
41 KEY_READ(conf_##name, conf.name, buflen, format_string)
42#define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n")
Johannes Berg8f20fc22007-08-28 17:01:54 -040043
44#define KEY_CONF_OPS(name) \
45static const struct file_operations key_ ##name## _ops = { \
46 .read = key_conf_##name##_read, \
47 .open = mac80211_open_file_generic, \
48}
49
50#define KEY_CONF_FILE(name, format) \
51 KEY_CONF_READ_##format(name) \
52 KEY_CONF_OPS(name)
53
54KEY_CONF_FILE(keylen, D);
55KEY_CONF_FILE(keyidx, D);
56KEY_CONF_FILE(hw_key_idx, D);
Johannes Berg11a843b2007-08-28 17:01:55 -040057KEY_FILE(flags, X);
Jiri Bence9f207f2007-05-05 11:46:38 -070058KEY_FILE(tx_rx_count, D);
Johannes Berge7a64f12007-08-28 17:01:55 -040059KEY_READ(ifindex, sdata->dev->ifindex, 20, "%d\n");
60KEY_OPS(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -070061
62static ssize_t key_algorithm_read(struct file *file,
63 char __user *userbuf,
64 size_t count, loff_t *ppos)
65{
66 char *alg;
67 struct ieee80211_key *key = file->private_data;
68
Johannes Berg8f20fc22007-08-28 17:01:54 -040069 switch (key->conf.alg) {
Jiri Bence9f207f2007-05-05 11:46:38 -070070 case ALG_WEP:
71 alg = "WEP\n";
72 break;
73 case ALG_TKIP:
74 alg = "TKIP\n";
75 break;
76 case ALG_CCMP:
77 alg = "CCMP\n";
78 break;
79 default:
80 return 0;
81 }
82 return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
83}
84KEY_OPS(algorithm);
85
86static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
87 size_t count, loff_t *ppos)
88{
89 const u8 *tpn;
90 char buf[20];
91 int len;
92 struct ieee80211_key *key = file->private_data;
93
Johannes Berg8f20fc22007-08-28 17:01:54 -040094 switch (key->conf.alg) {
Jiri Bence9f207f2007-05-05 11:46:38 -070095 case ALG_WEP:
96 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -040097 break;
Jiri Bence9f207f2007-05-05 11:46:38 -070098 case ALG_TKIP:
99 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700100 key->u.tkip.tx.iv32,
101 key->u.tkip.tx.iv16);
Johannes Berg50339a62007-08-28 17:01:52 -0400102 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700103 case ALG_CCMP:
104 tpn = key->u.ccmp.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], tpn[5]);
Johannes Berg50339a62007-08-28 17:01:52 -0400107 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700108 default:
109 return 0;
110 }
111 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
112}
113KEY_OPS(tx_spec);
114
115static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
116 size_t count, loff_t *ppos)
117{
118 struct ieee80211_key *key = file->private_data;
119 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
120 int i, len;
121 const u8 *rpn;
122
Johannes Berg8f20fc22007-08-28 17:01:54 -0400123 switch (key->conf.alg) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700124 case ALG_WEP:
125 len = scnprintf(buf, sizeof(buf), "\n");
Johannes Berg50339a62007-08-28 17:01:52 -0400126 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700127 case ALG_TKIP:
128 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
129 p += scnprintf(p, sizeof(buf)+buf-p,
130 "%08x %04x\n",
Harvey Harrisonb0f76b32008-05-14 16:26:19 -0700131 key->u.tkip.rx[i].iv32,
132 key->u.tkip.rx[i].iv16);
Jiri Bence9f207f2007-05-05 11:46:38 -0700133 len = p - buf;
Johannes Berg50339a62007-08-28 17:01:52 -0400134 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700135 case ALG_CCMP:
136 for (i = 0; i < NUM_RX_DATA_QUEUES; 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;
Johannes Berg50339a62007-08-28 17:01:52 -0400144 break;
Jiri Bence9f207f2007-05-05 11:46:38 -0700145 default:
146 return 0;
147 }
148 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
149}
150KEY_OPS(rx_spec);
151
152static ssize_t key_replays_read(struct file *file, char __user *userbuf,
153 size_t count, loff_t *ppos)
154{
155 struct ieee80211_key *key = file->private_data;
156 char buf[20];
157 int len;
158
Johannes Berg8f20fc22007-08-28 17:01:54 -0400159 if (key->conf.alg != ALG_CCMP)
Jiri Bence9f207f2007-05-05 11:46:38 -0700160 return 0;
161 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
162 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
163}
164KEY_OPS(replays);
165
166static ssize_t key_key_read(struct file *file, char __user *userbuf,
167 size_t count, loff_t *ppos)
168{
169 struct ieee80211_key *key = file->private_data;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400170 int i, res, bufsize = 2 * key->conf.keylen + 2;
Jiri Bence9f207f2007-05-05 11:46:38 -0700171 char *buf = kmalloc(bufsize, GFP_KERNEL);
172 char *p = buf;
173
Johannes Berg8f20fc22007-08-28 17:01:54 -0400174 for (i = 0; i < key->conf.keylen; i++)
175 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
Jiri Bence9f207f2007-05-05 11:46:38 -0700176 p += scnprintf(p, bufsize+buf-p, "\n");
177 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
178 kfree(buf);
179 return res;
180}
181KEY_OPS(key);
182
183#define DEBUGFS_ADD(name) \
184 key->debugfs.name = debugfs_create_file(#name, 0400,\
185 key->debugfs.dir, key, &key_##name##_ops);
186
Johannes Berg3b967662008-04-08 17:56:52 +0200187void ieee80211_debugfs_key_add(struct ieee80211_key *key)
188 {
Johannes Berg50339a62007-08-28 17:01:52 -0400189 static int keycount;
Johannes Berg3b967662008-04-08 17:56:52 +0200190 char buf[50];
191 DECLARE_MAC_BUF(mac);
192 struct sta_info *sta;
Jiri Bence9f207f2007-05-05 11:46:38 -0700193
Johannes Berg3b967662008-04-08 17:56:52 +0200194 if (!key->local->debugfs.keys)
Jiri Bence9f207f2007-05-05 11:46:38 -0700195 return;
196
Johannes Berg50339a62007-08-28 17:01:52 -0400197 sprintf(buf, "%d", keycount);
Johannes Bergd9c58f32008-04-08 16:46:36 -0400198 key->debugfs.cnt = keycount;
Johannes Berg50339a62007-08-28 17:01:52 -0400199 keycount++;
Jiri Bence9f207f2007-05-05 11:46:38 -0700200 key->debugfs.dir = debugfs_create_dir(buf,
Johannes Berg3b967662008-04-08 17:56:52 +0200201 key->local->debugfs.keys);
Jiri Bence9f207f2007-05-05 11:46:38 -0700202
203 if (!key->debugfs.dir)
204 return;
205
Johannes Berg3b967662008-04-08 17:56:52 +0200206 rcu_read_lock();
207 sta = rcu_dereference(key->sta);
208 if (sta)
Johannes Berg17741cd2008-09-11 00:02:02 +0200209 sprintf(buf, "../../stations/%s",
210 print_mac(mac, sta->sta.addr));
Johannes Berg3b967662008-04-08 17:56:52 +0200211 rcu_read_unlock();
212
213 /* using sta as a boolean is fine outside RCU lock */
214 if (sta)
215 key->debugfs.stalink =
216 debugfs_create_symlink("station", key->debugfs.dir, buf);
217
Jiri Bence9f207f2007-05-05 11:46:38 -0700218 DEBUGFS_ADD(keylen);
Johannes Berg8f20fc22007-08-28 17:01:54 -0400219 DEBUGFS_ADD(flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700220 DEBUGFS_ADD(keyidx);
221 DEBUGFS_ADD(hw_key_idx);
222 DEBUGFS_ADD(tx_rx_count);
223 DEBUGFS_ADD(algorithm);
224 DEBUGFS_ADD(tx_spec);
225 DEBUGFS_ADD(rx_spec);
226 DEBUGFS_ADD(replays);
227 DEBUGFS_ADD(key);
Johannes Berge7a64f12007-08-28 17:01:55 -0400228 DEBUGFS_ADD(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -0700229};
230
231#define DEBUGFS_DEL(name) \
232 debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
233
234void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
235{
236 if (!key)
237 return;
238
239 DEBUGFS_DEL(keylen);
Johannes Berg8f20fc22007-08-28 17:01:54 -0400240 DEBUGFS_DEL(flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700241 DEBUGFS_DEL(keyidx);
242 DEBUGFS_DEL(hw_key_idx);
243 DEBUGFS_DEL(tx_rx_count);
244 DEBUGFS_DEL(algorithm);
245 DEBUGFS_DEL(tx_spec);
246 DEBUGFS_DEL(rx_spec);
247 DEBUGFS_DEL(replays);
248 DEBUGFS_DEL(key);
Johannes Berge7a64f12007-08-28 17:01:55 -0400249 DEBUGFS_DEL(ifindex);
Jiri Bence9f207f2007-05-05 11:46:38 -0700250
251 debugfs_remove(key->debugfs.stalink);
252 key->debugfs.stalink = NULL;
253 debugfs_remove(key->debugfs.dir);
254 key->debugfs.dir = NULL;
255}
256void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
257{
258 char buf[50];
Johannes Berg78520ca2008-05-03 01:04:47 +0200259 struct ieee80211_key *key;
Jiri Bence9f207f2007-05-05 11:46:38 -0700260
261 if (!sdata->debugfsdir)
262 return;
263
Johannes Berg78520ca2008-05-03 01:04:47 +0200264 /* this is running under the key lock */
265
266 key = sdata->default_key;
267 if (key) {
268 sprintf(buf, "../keys/%d", key->debugfs.cnt);
Jouni Malinen2b58b202008-08-28 15:12:06 +0300269 sdata->common_debugfs.default_key =
Johannes Berg78520ca2008-05-03 01:04:47 +0200270 debugfs_create_symlink("default_key",
271 sdata->debugfsdir, buf);
272 } else
273 ieee80211_debugfs_key_remove_default(sdata);
Jiri Bence9f207f2007-05-05 11:46:38 -0700274}
Johannes Berg78520ca2008-05-03 01:04:47 +0200275
Jiri Bence9f207f2007-05-05 11:46:38 -0700276void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
277{
278 if (!sdata)
279 return;
280
Jouni Malinen2b58b202008-08-28 15:12:06 +0300281 debugfs_remove(sdata->common_debugfs.default_key);
282 sdata->common_debugfs.default_key = NULL;
Jiri Bence9f207f2007-05-05 11:46:38 -0700283}
Jiri Bence9f207f2007-05-05 11:46:38 -0700284
285void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
286 struct sta_info *sta)
287{
288 debugfs_remove(key->debugfs.stalink);
289 key->debugfs.stalink = NULL;
290}