debugfs_key.c 8.6 KB
Newer Older
J
Jiri Benc 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright 2003-2005	Devicescape Software, Inc.
 * Copyright (c) 2006	Jiri Benc <jbenc@suse.cz>
 * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kobject.h>
12
#include <linux/slab.h>
J
Jiri Benc 已提交
13
#include "ieee80211_i.h"
J
Johannes Berg 已提交
14
#include "key.h"
J
Jiri Benc 已提交
15 16 17
#include "debugfs.h"
#include "debugfs_key.h"

18
#define KEY_READ(name, prop, format_string)				\
J
Jiri Benc 已提交
19 20 21 22 23
static ssize_t key_##name##_read(struct file *file,			\
				 char __user *userbuf,			\
				 size_t count, loff_t *ppos)		\
{									\
	struct ieee80211_key *key = file->private_data;			\
24 25
	return mac80211_format_buffer(userbuf, count, ppos, 		\
				      format_string, key->prop);	\
J
Jiri Benc 已提交
26
}
27 28
#define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
#define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
J
Jiri Benc 已提交
29 30 31 32 33

#define KEY_OPS(name)							\
static const struct file_operations key_ ##name## _ops = {		\
	.read = key_##name##_read,					\
	.open = mac80211_open_file_generic,				\
34
	.llseek = generic_file_llseek,					\
J
Jiri Benc 已提交
35 36 37 38 39 40
}

#define KEY_FILE(name, format)						\
		 KEY_READ_##format(name)				\
		 KEY_OPS(name)

41 42 43
#define KEY_CONF_READ(name, format_string)				\
	KEY_READ(conf_##name, conf.name, format_string)
#define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
44 45 46 47 48

#define KEY_CONF_OPS(name)						\
static const struct file_operations key_ ##name## _ops = {		\
	.read = key_conf_##name##_read,					\
	.open = mac80211_open_file_generic,				\
49
	.llseek = generic_file_llseek,					\
50 51 52 53 54 55 56 57 58
}

#define KEY_CONF_FILE(name, format)					\
		 KEY_CONF_READ_##format(name)				\
		 KEY_CONF_OPS(name)

KEY_CONF_FILE(keylen, D);
KEY_CONF_FILE(keyidx, D);
KEY_CONF_FILE(hw_key_idx, D);
J
Johannes Berg 已提交
59
KEY_FILE(flags, X);
J
Jiri Benc 已提交
60
KEY_FILE(tx_rx_count, D);
61
KEY_READ(ifindex, sdata->name, "%s\n");
62
KEY_OPS(ifindex);
J
Jiri Benc 已提交
63 64 65 66 67

static ssize_t key_algorithm_read(struct file *file,
				  char __user *userbuf,
				  size_t count, loff_t *ppos)
{
68
	char buf[15];
J
Jiri Benc 已提交
69
	struct ieee80211_key *key = file->private_data;
70
	u32 c = key->conf.cipher;
J
Jiri Benc 已提交
71

72 73 74
	sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
		c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
J
Jiri Benc 已提交
75 76 77 78 79 80 81 82 83 84 85
}
KEY_OPS(algorithm);

static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
				size_t count, loff_t *ppos)
{
	const u8 *tpn;
	char buf[20];
	int len;
	struct ieee80211_key *key = file->private_data;

86 87 88
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
J
Jiri Benc 已提交
89
		len = scnprintf(buf, sizeof(buf), "\n");
J
Johannes Berg 已提交
90
		break;
91
	case WLAN_CIPHER_SUITE_TKIP:
J
Jiri Benc 已提交
92
		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
93 94
				key->u.tkip.tx.iv32,
				key->u.tkip.tx.iv16);
J
Johannes Berg 已提交
95
		break;
96
	case WLAN_CIPHER_SUITE_CCMP:
J
Jiri Benc 已提交
97 98 99
		tpn = key->u.ccmp.tx_pn;
		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
J
Johannes Berg 已提交
100
		break;
101
	case WLAN_CIPHER_SUITE_AES_CMAC:
102 103 104 105 106
		tpn = key->u.aes_cmac.tx_pn;
		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
				tpn[5]);
		break;
J
Jiri Benc 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	default:
		return 0;
	}
	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}
KEY_OPS(tx_spec);

static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
				size_t count, loff_t *ppos)
{
	struct ieee80211_key *key = file->private_data;
	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
	int i, len;
	const u8 *rpn;

122 123 124
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
J
Jiri Benc 已提交
125
		len = scnprintf(buf, sizeof(buf), "\n");
J
Johannes Berg 已提交
126
		break;
127
	case WLAN_CIPHER_SUITE_TKIP:
J
Jiri Benc 已提交
128 129 130
		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
			p += scnprintf(p, sizeof(buf)+buf-p,
				       "%08x %04x\n",
131 132
				       key->u.tkip.rx[i].iv32,
				       key->u.tkip.rx[i].iv16);
J
Jiri Benc 已提交
133
		len = p - buf;
J
Johannes Berg 已提交
134
		break;
135
	case WLAN_CIPHER_SUITE_CCMP:
136
		for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
J
Jiri Benc 已提交
137 138 139 140 141 142 143
			rpn = key->u.ccmp.rx_pn[i];
			p += scnprintf(p, sizeof(buf)+buf-p,
				       "%02x%02x%02x%02x%02x%02x\n",
				       rpn[0], rpn[1], rpn[2],
				       rpn[3], rpn[4], rpn[5]);
		}
		len = p - buf;
J
Johannes Berg 已提交
144
		break;
145
	case WLAN_CIPHER_SUITE_AES_CMAC:
146 147 148 149 150 151 152
		rpn = key->u.aes_cmac.rx_pn;
		p += scnprintf(p, sizeof(buf)+buf-p,
			       "%02x%02x%02x%02x%02x%02x\n",
			       rpn[0], rpn[1], rpn[2],
			       rpn[3], rpn[4], rpn[5]);
		len = p - buf;
		break;
J
Jiri Benc 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166
	default:
		return 0;
	}
	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}
KEY_OPS(rx_spec);

static ssize_t key_replays_read(struct file *file, char __user *userbuf,
				size_t count, loff_t *ppos)
{
	struct ieee80211_key *key = file->private_data;
	char buf[20];
	int len;

167 168
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
169 170
		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
		break;
171
	case WLAN_CIPHER_SUITE_AES_CMAC:
172 173 174 175
		len = scnprintf(buf, sizeof(buf), "%u\n",
				key->u.aes_cmac.replays);
		break;
	default:
J
Jiri Benc 已提交
176
		return 0;
177
	}
J
Jiri Benc 已提交
178 179 180 181
	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}
KEY_OPS(replays);

182 183 184 185 186 187 188
static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
				  size_t count, loff_t *ppos)
{
	struct ieee80211_key *key = file->private_data;
	char buf[20];
	int len;

189 190
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_AES_CMAC:
191 192 193 194 195 196 197 198 199 200
		len = scnprintf(buf, sizeof(buf), "%u\n",
				key->u.aes_cmac.icverrors);
		break;
	default:
		return 0;
	}
	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}
KEY_OPS(icverrors);

J
Jiri Benc 已提交
201 202 203 204
static ssize_t key_key_read(struct file *file, char __user *userbuf,
			    size_t count, loff_t *ppos)
{
	struct ieee80211_key *key = file->private_data;
205
	int i, bufsize = 2 * key->conf.keylen + 2;
J
Jiri Benc 已提交
206 207
	char *buf = kmalloc(bufsize, GFP_KERNEL);
	char *p = buf;
208 209 210 211
	ssize_t res;

	if (!buf)
		return -ENOMEM;
J
Jiri Benc 已提交
212

213 214
	for (i = 0; i < key->conf.keylen; i++)
		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
J
Jiri Benc 已提交
215 216 217 218 219 220 221 222
	p += scnprintf(p, bufsize+buf-p, "\n");
	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
	kfree(buf);
	return res;
}
KEY_OPS(key);

#define DEBUGFS_ADD(name) \
223 224
	debugfs_create_file(#name, 0400, key->debugfs.dir, \
			    key, &key_##name##_ops);
J
Jiri Benc 已提交
225

226 227
void ieee80211_debugfs_key_add(struct ieee80211_key *key)
  {
J
Johannes Berg 已提交
228
	static int keycount;
229 230
	char buf[50];
	struct sta_info *sta;
J
Jiri Benc 已提交
231

232
	if (!key->local->debugfs.keys)
J
Jiri Benc 已提交
233 234
		return;

J
Johannes Berg 已提交
235
	sprintf(buf, "%d", keycount);
236
	key->debugfs.cnt = keycount;
J
Johannes Berg 已提交
237
	keycount++;
J
Jiri Benc 已提交
238
	key->debugfs.dir = debugfs_create_dir(buf,
239
					key->local->debugfs.keys);
J
Jiri Benc 已提交
240 241 242 243

	if (!key->debugfs.dir)
		return;

244 245 246
	rcu_read_lock();
	sta = rcu_dereference(key->sta);
	if (sta)
247
		sprintf(buf, "../../stations/%pM", sta->sta.addr);
248 249 250 251 252 253 254
	rcu_read_unlock();

	/* using sta as a boolean is fine outside RCU lock */
	if (sta)
		key->debugfs.stalink =
			debugfs_create_symlink("station", key->debugfs.dir, buf);

J
Jiri Benc 已提交
255
	DEBUGFS_ADD(keylen);
256
	DEBUGFS_ADD(flags);
J
Jiri Benc 已提交
257 258 259 260 261 262 263
	DEBUGFS_ADD(keyidx);
	DEBUGFS_ADD(hw_key_idx);
	DEBUGFS_ADD(tx_rx_count);
	DEBUGFS_ADD(algorithm);
	DEBUGFS_ADD(tx_spec);
	DEBUGFS_ADD(rx_spec);
	DEBUGFS_ADD(replays);
264
	DEBUGFS_ADD(icverrors);
J
Jiri Benc 已提交
265
	DEBUGFS_ADD(key);
266
	DEBUGFS_ADD(ifindex);
J
Jiri Benc 已提交
267 268 269 270 271 272 273
};

void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
{
	if (!key)
		return;

274
	debugfs_remove_recursive(key->debugfs.dir);
J
Jiri Benc 已提交
275 276 277 278 279
	key->debugfs.dir = NULL;
}
void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
{
	char buf[50];
280
	struct ieee80211_key *key;
J
Jiri Benc 已提交
281

282
	if (!sdata->debugfs.dir)
J
Jiri Benc 已提交
283 284
		return;

285 286 287 288 289
	/* this is running under the key lock */

	key = sdata->default_key;
	if (key) {
		sprintf(buf, "../keys/%d", key->debugfs.cnt);
290
		sdata->debugfs.default_key =
291
			debugfs_create_symlink("default_key",
292
					       sdata->debugfs.dir, buf);
293 294
	} else
		ieee80211_debugfs_key_remove_default(sdata);
J
Jiri Benc 已提交
295
}
296

J
Jiri Benc 已提交
297 298 299 300 301
void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
{
	if (!sdata)
		return;

302 303
	debugfs_remove(sdata->debugfs.default_key);
	sdata->debugfs.default_key = NULL;
J
Jiri Benc 已提交
304 305
}

306 307 308 309 310
void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
{
	char buf[50];
	struct ieee80211_key *key;

311
	if (!sdata->debugfs.dir)
312 313 314 315 316 317 318
		return;

	/* this is running under the key lock */

	key = sdata->default_mgmt_key;
	if (key) {
		sprintf(buf, "../keys/%d", key->debugfs.cnt);
319
		sdata->debugfs.default_mgmt_key =
320
			debugfs_create_symlink("default_mgmt_key",
321
					       sdata->debugfs.dir, buf);
322 323 324 325 326 327 328 329 330
	} else
		ieee80211_debugfs_key_remove_mgmt_default(sdata);
}

void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
{
	if (!sdata)
		return;

331 332
	debugfs_remove(sdata->debugfs.default_mgmt_key);
	sdata->debugfs.default_mgmt_key = NULL;
333 334
}

J
Jiri Benc 已提交
335 336 337 338 339 340
void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
				   struct sta_info *sta)
{
	debugfs_remove(key->debugfs.stalink);
	key->debugfs.stalink = NULL;
}