xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 97359d12)
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,				\
35e9f207f0SJiri Benc }
36e9f207f0SJiri Benc 
37e9f207f0SJiri Benc #define KEY_FILE(name, format)						\
38e9f207f0SJiri Benc 		 KEY_READ_##format(name)				\
39e9f207f0SJiri Benc 		 KEY_OPS(name)
40e9f207f0SJiri Benc 
418f20fc24SJohannes Berg #define KEY_CONF_READ(name, buflen, format_string)			\
428f20fc24SJohannes Berg 	KEY_READ(conf_##name, conf.name, buflen, format_string)
438f20fc24SJohannes Berg #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n")
448f20fc24SJohannes Berg 
458f20fc24SJohannes Berg #define KEY_CONF_OPS(name)						\
468f20fc24SJohannes Berg static const struct file_operations key_ ##name## _ops = {		\
478f20fc24SJohannes Berg 	.read = key_conf_##name##_read,					\
488f20fc24SJohannes Berg 	.open = mac80211_open_file_generic,				\
498f20fc24SJohannes Berg }
508f20fc24SJohannes Berg 
518f20fc24SJohannes Berg #define KEY_CONF_FILE(name, format)					\
528f20fc24SJohannes Berg 		 KEY_CONF_READ_##format(name)				\
538f20fc24SJohannes Berg 		 KEY_CONF_OPS(name)
548f20fc24SJohannes Berg 
558f20fc24SJohannes Berg KEY_CONF_FILE(keylen, D);
568f20fc24SJohannes Berg KEY_CONF_FILE(keyidx, D);
578f20fc24SJohannes Berg KEY_CONF_FILE(hw_key_idx, D);
5811a843b7SJohannes Berg KEY_FILE(flags, X);
59e9f207f0SJiri Benc KEY_FILE(tx_rx_count, D);
6047846c9bSJohannes Berg KEY_READ(ifindex, sdata->name, IFNAMSIZ + 2, "%s\n");
61e7a64f12SJohannes Berg KEY_OPS(ifindex);
62e9f207f0SJiri Benc 
63e9f207f0SJiri Benc static ssize_t key_algorithm_read(struct file *file,
64e9f207f0SJiri Benc 				  char __user *userbuf,
65e9f207f0SJiri Benc 				  size_t count, loff_t *ppos)
66e9f207f0SJiri Benc {
6797359d12SJohannes Berg 	char buf[15];
68e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
6997359d12SJohannes Berg 	u32 c = key->conf.cipher;
70e9f207f0SJiri Benc 
7197359d12SJohannes Berg 	sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
7297359d12SJohannes Berg 		c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
7397359d12SJohannes Berg 	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
74e9f207f0SJiri Benc }
75e9f207f0SJiri Benc KEY_OPS(algorithm);
76e9f207f0SJiri Benc 
77e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
78e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
79e9f207f0SJiri Benc {
80e9f207f0SJiri Benc 	const u8 *tpn;
81e9f207f0SJiri Benc 	char buf[20];
82e9f207f0SJiri Benc 	int len;
83e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
84e9f207f0SJiri Benc 
8597359d12SJohannes Berg 	switch (key->conf.cipher) {
8697359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP40:
8797359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP104:
88e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
8950339a67SJohannes Berg 		break;
9097359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_TKIP:
91e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
92b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv32,
93b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv16);
9450339a67SJohannes Berg 		break;
9597359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
96e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
97e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
98e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
9950339a67SJohannes Berg 		break;
10097359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1013cfcf6acSJouni Malinen 		tpn = key->u.aes_cmac.tx_pn;
1023cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
1033cfcf6acSJouni Malinen 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
1043cfcf6acSJouni Malinen 				tpn[5]);
1053cfcf6acSJouni Malinen 		break;
106e9f207f0SJiri Benc 	default:
107e9f207f0SJiri Benc 		return 0;
108e9f207f0SJiri Benc 	}
109e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
110e9f207f0SJiri Benc }
111e9f207f0SJiri Benc KEY_OPS(tx_spec);
112e9f207f0SJiri Benc 
113e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
114e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
115e9f207f0SJiri Benc {
116e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
117e9f207f0SJiri Benc 	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
118e9f207f0SJiri Benc 	int i, len;
119e9f207f0SJiri Benc 	const u8 *rpn;
120e9f207f0SJiri Benc 
12197359d12SJohannes Berg 	switch (key->conf.cipher) {
12297359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP40:
12397359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_WEP104:
124e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
12550339a67SJohannes Berg 		break;
12697359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_TKIP:
127e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
128e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
129e9f207f0SJiri Benc 				       "%08x %04x\n",
130b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv32,
131b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv16);
132e9f207f0SJiri Benc 		len = p - buf;
13350339a67SJohannes Berg 		break;
13497359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
1359190252cSJouni Malinen 		for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
136e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
137e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
138e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
139e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
140e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
141e9f207f0SJiri Benc 		}
142e9f207f0SJiri Benc 		len = p - buf;
14350339a67SJohannes Berg 		break;
14497359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1453cfcf6acSJouni Malinen 		rpn = key->u.aes_cmac.rx_pn;
1463cfcf6acSJouni Malinen 		p += scnprintf(p, sizeof(buf)+buf-p,
1473cfcf6acSJouni Malinen 			       "%02x%02x%02x%02x%02x%02x\n",
1483cfcf6acSJouni Malinen 			       rpn[0], rpn[1], rpn[2],
1493cfcf6acSJouni Malinen 			       rpn[3], rpn[4], rpn[5]);
1503cfcf6acSJouni Malinen 		len = p - buf;
1513cfcf6acSJouni Malinen 		break;
152e9f207f0SJiri Benc 	default:
153e9f207f0SJiri Benc 		return 0;
154e9f207f0SJiri Benc 	}
155e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
156e9f207f0SJiri Benc }
157e9f207f0SJiri Benc KEY_OPS(rx_spec);
158e9f207f0SJiri Benc 
159e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
160e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
161e9f207f0SJiri Benc {
162e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
163e9f207f0SJiri Benc 	char buf[20];
164e9f207f0SJiri Benc 	int len;
165e9f207f0SJiri Benc 
16697359d12SJohannes Berg 	switch (key->conf.cipher) {
16797359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_CCMP:
168e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
1693cfcf6acSJouni Malinen 		break;
17097359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1713cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
1723cfcf6acSJouni Malinen 				key->u.aes_cmac.replays);
1733cfcf6acSJouni Malinen 		break;
1743cfcf6acSJouni Malinen 	default:
1753cfcf6acSJouni Malinen 		return 0;
1763cfcf6acSJouni Malinen 	}
177e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
178e9f207f0SJiri Benc }
179e9f207f0SJiri Benc KEY_OPS(replays);
180e9f207f0SJiri Benc 
1813cfcf6acSJouni Malinen static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
1823cfcf6acSJouni Malinen 				  size_t count, loff_t *ppos)
1833cfcf6acSJouni Malinen {
1843cfcf6acSJouni Malinen 	struct ieee80211_key *key = file->private_data;
1853cfcf6acSJouni Malinen 	char buf[20];
1863cfcf6acSJouni Malinen 	int len;
1873cfcf6acSJouni Malinen 
18897359d12SJohannes Berg 	switch (key->conf.cipher) {
18997359d12SJohannes Berg 	case WLAN_CIPHER_SUITE_AES_CMAC:
1903cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
1913cfcf6acSJouni Malinen 				key->u.aes_cmac.icverrors);
1923cfcf6acSJouni Malinen 		break;
1933cfcf6acSJouni Malinen 	default:
1943cfcf6acSJouni Malinen 		return 0;
1953cfcf6acSJouni Malinen 	}
1963cfcf6acSJouni Malinen 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
1973cfcf6acSJouni Malinen }
1983cfcf6acSJouni Malinen KEY_OPS(icverrors);
1993cfcf6acSJouni Malinen 
200e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
201e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
202e9f207f0SJiri Benc {
203e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
2048f20fc24SJohannes Berg 	int i, res, bufsize = 2 * key->conf.keylen + 2;
205e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
206e9f207f0SJiri Benc 	char *p = buf;
207e9f207f0SJiri Benc 
2088f20fc24SJohannes Berg 	for (i = 0; i < key->conf.keylen; i++)
2098f20fc24SJohannes Berg 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
210e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
211e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
212e9f207f0SJiri Benc 	kfree(buf);
213e9f207f0SJiri Benc 	return res;
214e9f207f0SJiri Benc }
215e9f207f0SJiri Benc KEY_OPS(key);
216e9f207f0SJiri Benc 
217e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
2187bcfaf2fSJohannes Berg 	debugfs_create_file(#name, 0400, key->debugfs.dir, \
2197bcfaf2fSJohannes Berg 			    key, &key_##name##_ops);
220e9f207f0SJiri Benc 
2213b96766fSJohannes Berg void ieee80211_debugfs_key_add(struct ieee80211_key *key)
222e9f207f0SJiri Benc   {
22350339a67SJohannes Berg 	static int keycount;
2243b96766fSJohannes Berg 	char buf[50];
2253b96766fSJohannes Berg 	struct sta_info *sta;
226e9f207f0SJiri Benc 
2273b96766fSJohannes Berg 	if (!key->local->debugfs.keys)
228e9f207f0SJiri Benc 		return;
229e9f207f0SJiri Benc 
23050339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
231d9c58f30SJohannes Berg 	key->debugfs.cnt = keycount;
23250339a67SJohannes Berg 	keycount++;
233e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
2343b96766fSJohannes Berg 					key->local->debugfs.keys);
235e9f207f0SJiri Benc 
236e9f207f0SJiri Benc 	if (!key->debugfs.dir)
237e9f207f0SJiri Benc 		return;
238e9f207f0SJiri Benc 
2393b96766fSJohannes Berg 	rcu_read_lock();
2403b96766fSJohannes Berg 	sta = rcu_dereference(key->sta);
2413b96766fSJohannes Berg 	if (sta)
2420c68ae26SJohannes Berg 		sprintf(buf, "../../stations/%pM", sta->sta.addr);
2433b96766fSJohannes Berg 	rcu_read_unlock();
2443b96766fSJohannes Berg 
2453b96766fSJohannes Berg 	/* using sta as a boolean is fine outside RCU lock */
2463b96766fSJohannes Berg 	if (sta)
2473b96766fSJohannes Berg 		key->debugfs.stalink =
2483b96766fSJohannes Berg 			debugfs_create_symlink("station", key->debugfs.dir, buf);
2493b96766fSJohannes Berg 
250e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
2518f20fc24SJohannes Berg 	DEBUGFS_ADD(flags);
252e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
253e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
254e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
255e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
256e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
257e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
258e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
2593cfcf6acSJouni Malinen 	DEBUGFS_ADD(icverrors);
260e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
261e7a64f12SJohannes Berg 	DEBUGFS_ADD(ifindex);
262e9f207f0SJiri Benc };
263e9f207f0SJiri Benc 
264e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
265e9f207f0SJiri Benc {
266e9f207f0SJiri Benc 	if (!key)
267e9f207f0SJiri Benc 		return;
268e9f207f0SJiri Benc 
2697bcfaf2fSJohannes Berg 	debugfs_remove_recursive(key->debugfs.dir);
270e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
271e9f207f0SJiri Benc }
272e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
273e9f207f0SJiri Benc {
274e9f207f0SJiri Benc 	char buf[50];
27578520cadSJohannes Berg 	struct ieee80211_key *key;
276e9f207f0SJiri Benc 
2777bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
278e9f207f0SJiri Benc 		return;
279e9f207f0SJiri Benc 
28078520cadSJohannes Berg 	/* this is running under the key lock */
28178520cadSJohannes Berg 
28278520cadSJohannes Berg 	key = sdata->default_key;
28378520cadSJohannes Berg 	if (key) {
28478520cadSJohannes Berg 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
2857bcfaf2fSJohannes Berg 		sdata->debugfs.default_key =
28678520cadSJohannes Berg 			debugfs_create_symlink("default_key",
2877bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
28878520cadSJohannes Berg 	} else
28978520cadSJohannes Berg 		ieee80211_debugfs_key_remove_default(sdata);
290e9f207f0SJiri Benc }
29178520cadSJohannes Berg 
292e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
293e9f207f0SJiri Benc {
294e9f207f0SJiri Benc 	if (!sdata)
295e9f207f0SJiri Benc 		return;
296e9f207f0SJiri Benc 
2977bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_key);
2987bcfaf2fSJohannes Berg 	sdata->debugfs.default_key = NULL;
299e9f207f0SJiri Benc }
300e9f207f0SJiri Benc 
3013cfcf6acSJouni Malinen void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
3023cfcf6acSJouni Malinen {
3033cfcf6acSJouni Malinen 	char buf[50];
3043cfcf6acSJouni Malinen 	struct ieee80211_key *key;
3053cfcf6acSJouni Malinen 
3067bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
3073cfcf6acSJouni Malinen 		return;
3083cfcf6acSJouni Malinen 
3093cfcf6acSJouni Malinen 	/* this is running under the key lock */
3103cfcf6acSJouni Malinen 
3113cfcf6acSJouni Malinen 	key = sdata->default_mgmt_key;
3123cfcf6acSJouni Malinen 	if (key) {
3133cfcf6acSJouni Malinen 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
3147bcfaf2fSJohannes Berg 		sdata->debugfs.default_mgmt_key =
3153cfcf6acSJouni Malinen 			debugfs_create_symlink("default_mgmt_key",
3167bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
3173cfcf6acSJouni Malinen 	} else
3183cfcf6acSJouni Malinen 		ieee80211_debugfs_key_remove_mgmt_default(sdata);
3193cfcf6acSJouni Malinen }
3203cfcf6acSJouni Malinen 
3213cfcf6acSJouni Malinen void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
3223cfcf6acSJouni Malinen {
3233cfcf6acSJouni Malinen 	if (!sdata)
3243cfcf6acSJouni Malinen 		return;
3253cfcf6acSJouni Malinen 
3267bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_mgmt_key);
3277bcfaf2fSJohannes Berg 	sdata->debugfs.default_mgmt_key = NULL;
3283cfcf6acSJouni Malinen }
3293cfcf6acSJouni Malinen 
330e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
331e9f207f0SJiri Benc 				   struct sta_info *sta)
332e9f207f0SJiri Benc {
333e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
334e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
335e9f207f0SJiri Benc }
336