xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 520efd1a)
1e9f207f0SJiri Benc /*
2e9f207f0SJiri Benc  * Copyright 2003-2005	Devicescape Software, Inc.
3e9f207f0SJiri Benc  * Copyright (c) 2006	Jiri Benc <jbenc@suse.cz>
4e9f207f0SJiri Benc  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5e9f207f0SJiri Benc  *
6e9f207f0SJiri Benc  * This program is free software; you can redistribute it and/or modify
7e9f207f0SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8e9f207f0SJiri Benc  * published by the Free Software Foundation.
9e9f207f0SJiri Benc  */
10e9f207f0SJiri Benc 
11e9f207f0SJiri Benc #include <linux/kobject.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13e9f207f0SJiri Benc #include "ieee80211_i.h"
142c8dccc7SJohannes Berg #include "key.h"
15e9f207f0SJiri Benc #include "debugfs.h"
16e9f207f0SJiri Benc #include "debugfs_key.h"
17e9f207f0SJiri Benc 
188f20fc24SJohannes Berg #define KEY_READ(name, prop, buflen, format_string)			\
19e9f207f0SJiri Benc static ssize_t key_##name##_read(struct file *file,			\
20e9f207f0SJiri Benc 				 char __user *userbuf,			\
21e9f207f0SJiri Benc 				 size_t count, loff_t *ppos)		\
22e9f207f0SJiri Benc {									\
23e9f207f0SJiri Benc 	char buf[buflen];						\
24e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;			\
258f20fc24SJohannes Berg 	int res = scnprintf(buf, buflen, format_string, key->prop);	\
26e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, res);	\
27e9f207f0SJiri Benc }
288f20fc24SJohannes Berg #define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n")
2911a843b7SJohannes Berg #define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n")
30e9f207f0SJiri Benc 
31e9f207f0SJiri Benc #define KEY_OPS(name)							\
32e9f207f0SJiri Benc static const struct file_operations key_ ##name## _ops = {		\
33e9f207f0SJiri Benc 	.read = key_##name##_read,					\
34e9f207f0SJiri Benc 	.open = mac80211_open_file_generic,				\
352b18ab36SArnd Bergmann 	.llseek = generic_file_llseek,					\
36e9f207f0SJiri Benc }
37e9f207f0SJiri Benc 
38e9f207f0SJiri Benc #define KEY_FILE(name, format)						\
39e9f207f0SJiri Benc 		 KEY_READ_##format(name)				\
40e9f207f0SJiri Benc 		 KEY_OPS(name)
41e9f207f0SJiri Benc 
428f20fc24SJohannes Berg #define KEY_CONF_READ(name, buflen, format_string)			\
438f20fc24SJohannes Berg 	KEY_READ(conf_##name, conf.name, buflen, format_string)
448f20fc24SJohannes Berg #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n")
458f20fc24SJohannes Berg 
468f20fc24SJohannes Berg #define KEY_CONF_OPS(name)						\
478f20fc24SJohannes Berg static const struct file_operations key_ ##name## _ops = {		\
488f20fc24SJohannes Berg 	.read = key_conf_##name##_read,					\
498f20fc24SJohannes Berg 	.open = mac80211_open_file_generic,				\
502b18ab36SArnd Bergmann 	.llseek = generic_file_llseek,					\
518f20fc24SJohannes Berg }
528f20fc24SJohannes Berg 
538f20fc24SJohannes Berg #define KEY_CONF_FILE(name, format)					\
548f20fc24SJohannes Berg 		 KEY_CONF_READ_##format(name)				\
558f20fc24SJohannes Berg 		 KEY_CONF_OPS(name)
568f20fc24SJohannes Berg 
578f20fc24SJohannes Berg KEY_CONF_FILE(keylen, D);
588f20fc24SJohannes Berg KEY_CONF_FILE(keyidx, D);
598f20fc24SJohannes Berg KEY_CONF_FILE(hw_key_idx, D);
6011a843b7SJohannes Berg KEY_FILE(flags, X);
61e9f207f0SJiri Benc KEY_FILE(tx_rx_count, D);
6247846c9bSJohannes Berg KEY_READ(ifindex, sdata->name, IFNAMSIZ + 2, "%s\n");
63e7a64f12SJohannes Berg KEY_OPS(ifindex);
64e9f207f0SJiri Benc 
65e9f207f0SJiri Benc static ssize_t key_algorithm_read(struct file *file,
66e9f207f0SJiri Benc 				  char __user *userbuf,
67e9f207f0SJiri Benc 				  size_t count, loff_t *ppos)
68e9f207f0SJiri Benc {
6997359d12SJohannes Berg 	char buf[15];
70e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
7197359d12SJohannes Berg 	u32 c = key->conf.cipher;
72e9f207f0SJiri Benc 
7397359d12SJohannes Berg 	sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
7497359d12SJohannes Berg 		c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
7597359d12SJohannes Berg 	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
76e9f207f0SJiri Benc }
77e9f207f0SJiri Benc KEY_OPS(algorithm);
78e9f207f0SJiri Benc 
79e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
80e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
81e9f207f0SJiri Benc {
82e9f207f0SJiri Benc 	const u8 *tpn;
83e9f207f0SJiri Benc 	char buf[20];
84e9f207f0SJiri Benc 	int len;
85e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
86e9f207f0SJiri Benc 
8797359d12SJohannes Berg 	switch (key->conf.cipher) {
8897359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP40:
8997359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP104:
90e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
9150339a67SJohannes Berg 		break;
9297359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_TKIP:
93e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
94b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv32,
95b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv16);
9650339a67SJohannes Berg 		break;
9797359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
98e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
99e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
100e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
10150339a67SJohannes Berg 		break;
10297359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1033cfcf6acSJouni Malinen 		tpn = key->u.aes_cmac.tx_pn;
1043cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
1053cfcf6acSJouni Malinen 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
1063cfcf6acSJouni Malinen 				tpn[5]);
1073cfcf6acSJouni Malinen 		break;
108e9f207f0SJiri Benc 	default:
109e9f207f0SJiri Benc 		return 0;
110e9f207f0SJiri Benc 	}
111e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
112e9f207f0SJiri Benc }
113e9f207f0SJiri Benc KEY_OPS(tx_spec);
114e9f207f0SJiri Benc 
115e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
116e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
117e9f207f0SJiri Benc {
118e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
119e9f207f0SJiri Benc 	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
120e9f207f0SJiri Benc 	int i, len;
121e9f207f0SJiri Benc 	const u8 *rpn;
122e9f207f0SJiri Benc 
12397359d12SJohannes Berg 	switch (key->conf.cipher) {
12497359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP40:
12597359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP104:
126e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
12750339a67SJohannes Berg 		break;
12897359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_TKIP:
129e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
130e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
131e9f207f0SJiri Benc 				       "%08x %04x\n",
132b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv32,
133b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv16);
134e9f207f0SJiri Benc 		len = p - buf;
13550339a67SJohannes Berg 		break;
13697359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
1379190252cSJouni Malinen 		for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
138e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
139e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
140e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
141e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
142e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
143e9f207f0SJiri Benc 		}
144e9f207f0SJiri Benc 		len = p - buf;
14550339a67SJohannes Berg 		break;
14697359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1473cfcf6acSJouni Malinen 		rpn = key->u.aes_cmac.rx_pn;
1483cfcf6acSJouni Malinen 		p += scnprintf(p, sizeof(buf)+buf-p,
1493cfcf6acSJouni Malinen 			       "%02x%02x%02x%02x%02x%02x\n",
1503cfcf6acSJouni Malinen 			       rpn[0], rpn[1], rpn[2],
1513cfcf6acSJouni Malinen 			       rpn[3], rpn[4], rpn[5]);
1523cfcf6acSJouni Malinen 		len = p - buf;
1533cfcf6acSJouni Malinen 		break;
154e9f207f0SJiri Benc 	default:
155e9f207f0SJiri Benc 		return 0;
156e9f207f0SJiri Benc 	}
157e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
158e9f207f0SJiri Benc }
159e9f207f0SJiri Benc KEY_OPS(rx_spec);
160e9f207f0SJiri Benc 
161e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
162e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
163e9f207f0SJiri Benc {
164e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
165e9f207f0SJiri Benc 	char buf[20];
166e9f207f0SJiri Benc 	int len;
167e9f207f0SJiri Benc 
16897359d12SJohannes Berg 	switch (key->conf.cipher) {
16997359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
170e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
1713cfcf6acSJouni Malinen 		break;
17297359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1733cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
1743cfcf6acSJouni Malinen 				key->u.aes_cmac.replays);
1753cfcf6acSJouni Malinen 		break;
1763cfcf6acSJouni Malinen 	default:
1773cfcf6acSJouni Malinen 		return 0;
1783cfcf6acSJouni Malinen 	}
179e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180e9f207f0SJiri Benc }
181e9f207f0SJiri Benc KEY_OPS(replays);
182e9f207f0SJiri Benc 
1833cfcf6acSJouni Malinen static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
1843cfcf6acSJouni Malinen 				  size_t count, loff_t *ppos)
1853cfcf6acSJouni Malinen {
1863cfcf6acSJouni Malinen 	struct ieee80211_key *key = file->private_data;
1873cfcf6acSJouni Malinen 	char buf[20];
1883cfcf6acSJouni Malinen 	int len;
1893cfcf6acSJouni Malinen 
19097359d12SJohannes Berg 	switch (key->conf.cipher) {
19197359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1923cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
1933cfcf6acSJouni Malinen 				key->u.aes_cmac.icverrors);
1943cfcf6acSJouni Malinen 		break;
1953cfcf6acSJouni Malinen 	default:
1963cfcf6acSJouni Malinen 		return 0;
1973cfcf6acSJouni Malinen 	}
1983cfcf6acSJouni Malinen 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
1993cfcf6acSJouni Malinen }
2003cfcf6acSJouni Malinen KEY_OPS(icverrors);
2013cfcf6acSJouni Malinen 
202e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
203e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
204e9f207f0SJiri Benc {
205e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
206520efd1aSJesper Juhl 	int i, bufsize = 2 * key->conf.keylen + 2;
207e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
208e9f207f0SJiri Benc 	char *p = buf;
209520efd1aSJesper Juhl 	ssize_t res;
210520efd1aSJesper Juhl 
211520efd1aSJesper Juhl 	if (!buf)
212520efd1aSJesper Juhl 		return -ENOMEM;
213e9f207f0SJiri Benc 
2148f20fc24SJohannes Berg 	for (i = 0; i < key->conf.keylen; i++)
2158f20fc24SJohannes Berg 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
216e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
217e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
218e9f207f0SJiri Benc 	kfree(buf);
219e9f207f0SJiri Benc 	return res;
220e9f207f0SJiri Benc }
221e9f207f0SJiri Benc KEY_OPS(key);
222e9f207f0SJiri Benc 
223e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
2247bcfaf2fSJohannes Berg 	debugfs_create_file(#name, 0400, key->debugfs.dir, \
2257bcfaf2fSJohannes Berg 			    key, &key_##name##_ops);
226e9f207f0SJiri Benc 
2273b96766fSJohannes Berg void ieee80211_debugfs_key_add(struct ieee80211_key *key)
228e9f207f0SJiri Benc   {
22950339a67SJohannes Berg 	static int keycount;
2303b96766fSJohannes Berg 	char buf[50];
2313b96766fSJohannes Berg 	struct sta_info *sta;
232e9f207f0SJiri Benc 
2333b96766fSJohannes Berg 	if (!key->local->debugfs.keys)
234e9f207f0SJiri Benc 		return;
235e9f207f0SJiri Benc 
23650339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
237d9c58f30SJohannes Berg 	key->debugfs.cnt = keycount;
23850339a67SJohannes Berg 	keycount++;
239e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
2403b96766fSJohannes Berg 					key->local->debugfs.keys);
241e9f207f0SJiri Benc 
242e9f207f0SJiri Benc 	if (!key->debugfs.dir)
243e9f207f0SJiri Benc 		return;
244e9f207f0SJiri Benc 
2453b96766fSJohannes Berg 	rcu_read_lock();
2463b96766fSJohannes Berg 	sta = rcu_dereference(key->sta);
2473b96766fSJohannes Berg 	if (sta)
2480c68ae26SJohannes Berg 		sprintf(buf, "../../stations/%pM", sta->sta.addr);
2493b96766fSJohannes Berg 	rcu_read_unlock();
2503b96766fSJohannes Berg 
2513b96766fSJohannes Berg 	/* using sta as a boolean is fine outside RCU lock */
2523b96766fSJohannes Berg 	if (sta)
2533b96766fSJohannes Berg 		key->debugfs.stalink =
2543b96766fSJohannes Berg 			debugfs_create_symlink("station", key->debugfs.dir, buf);
2553b96766fSJohannes Berg 
256e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
2578f20fc24SJohannes Berg 	DEBUGFS_ADD(flags);
258e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
259e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
260e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
261e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
262e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
263e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
264e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
2653cfcf6acSJouni Malinen 	DEBUGFS_ADD(icverrors);
266e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
267e7a64f12SJohannes Berg 	DEBUGFS_ADD(ifindex);
268e9f207f0SJiri Benc };
269e9f207f0SJiri Benc 
270e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
271e9f207f0SJiri Benc {
272e9f207f0SJiri Benc 	if (!key)
273e9f207f0SJiri Benc 		return;
274e9f207f0SJiri Benc 
2757bcfaf2fSJohannes Berg 	debugfs_remove_recursive(key->debugfs.dir);
276e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
277e9f207f0SJiri Benc }
278e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
279e9f207f0SJiri Benc {
280e9f207f0SJiri Benc 	char buf[50];
28178520cadSJohannes Berg 	struct ieee80211_key *key;
282e9f207f0SJiri Benc 
2837bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
284e9f207f0SJiri Benc 		return;
285e9f207f0SJiri Benc 
28678520cadSJohannes Berg 	/* this is running under the key lock */
28778520cadSJohannes Berg 
28878520cadSJohannes Berg 	key = sdata->default_key;
28978520cadSJohannes Berg 	if (key) {
29078520cadSJohannes Berg 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
2917bcfaf2fSJohannes Berg 		sdata->debugfs.default_key =
29278520cadSJohannes Berg 			debugfs_create_symlink("default_key",
2937bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
29478520cadSJohannes Berg 	} else
29578520cadSJohannes Berg 		ieee80211_debugfs_key_remove_default(sdata);
296e9f207f0SJiri Benc }
29778520cadSJohannes Berg 
298e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
299e9f207f0SJiri Benc {
300e9f207f0SJiri Benc 	if (!sdata)
301e9f207f0SJiri Benc 		return;
302e9f207f0SJiri Benc 
3037bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_key);
3047bcfaf2fSJohannes Berg 	sdata->debugfs.default_key = NULL;
305e9f207f0SJiri Benc }
306e9f207f0SJiri Benc 
3073cfcf6acSJouni Malinen void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
3083cfcf6acSJouni Malinen {
3093cfcf6acSJouni Malinen 	char buf[50];
3103cfcf6acSJouni Malinen 	struct ieee80211_key *key;
3113cfcf6acSJouni Malinen 
3127bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
3133cfcf6acSJouni Malinen 		return;
3143cfcf6acSJouni Malinen 
3153cfcf6acSJouni Malinen 	/* this is running under the key lock */
3163cfcf6acSJouni Malinen 
3173cfcf6acSJouni Malinen 	key = sdata->default_mgmt_key;
3183cfcf6acSJouni Malinen 	if (key) {
3193cfcf6acSJouni Malinen 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
3207bcfaf2fSJohannes Berg 		sdata->debugfs.default_mgmt_key =
3213cfcf6acSJouni Malinen 			debugfs_create_symlink("default_mgmt_key",
3227bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
3233cfcf6acSJouni Malinen 	} else
3243cfcf6acSJouni Malinen 		ieee80211_debugfs_key_remove_mgmt_default(sdata);
3253cfcf6acSJouni Malinen }
3263cfcf6acSJouni Malinen 
3273cfcf6acSJouni Malinen void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
3283cfcf6acSJouni Malinen {
3293cfcf6acSJouni Malinen 	if (!sdata)
3303cfcf6acSJouni Malinen 		return;
3313cfcf6acSJouni Malinen 
3327bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_mgmt_key);
3337bcfaf2fSJohannes Berg 	sdata->debugfs.default_mgmt_key = NULL;
3343cfcf6acSJouni Malinen }
3353cfcf6acSJouni Malinen 
336e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
337e9f207f0SJiri Benc 				   struct sta_info *sta)
338e9f207f0SJiri Benc {
339e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
340e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
341e9f207f0SJiri Benc }
342