xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 2b18ab36)
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 {
69e9f207f0SJiri Benc 	char *alg;
70e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
71e9f207f0SJiri Benc 
728f20fc24SJohannes Berg 	switch (key->conf.alg) {
73e9f207f0SJiri Benc 	case ALG_WEP:
74e9f207f0SJiri Benc 		alg = "WEP\n";
75e9f207f0SJiri Benc 		break;
76e9f207f0SJiri Benc 	case ALG_TKIP:
77e9f207f0SJiri Benc 		alg = "TKIP\n";
78e9f207f0SJiri Benc 		break;
79e9f207f0SJiri Benc 	case ALG_CCMP:
80e9f207f0SJiri Benc 		alg = "CCMP\n";
81e9f207f0SJiri Benc 		break;
823cfcf6acSJouni Malinen 	case ALG_AES_CMAC:
833cfcf6acSJouni Malinen 		alg = "AES-128-CMAC\n";
843cfcf6acSJouni Malinen 		break;
85e9f207f0SJiri Benc 	default:
86e9f207f0SJiri Benc 		return 0;
87e9f207f0SJiri Benc 	}
88e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
89e9f207f0SJiri Benc }
90e9f207f0SJiri Benc KEY_OPS(algorithm);
91e9f207f0SJiri Benc 
92e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
93e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
94e9f207f0SJiri Benc {
95e9f207f0SJiri Benc 	const u8 *tpn;
96e9f207f0SJiri Benc 	char buf[20];
97e9f207f0SJiri Benc 	int len;
98e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
99e9f207f0SJiri Benc 
1008f20fc24SJohannes Berg 	switch (key->conf.alg) {
101e9f207f0SJiri Benc 	case ALG_WEP:
102e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
10350339a67SJohannes Berg 		break;
104e9f207f0SJiri Benc 	case ALG_TKIP:
105e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
106b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv32,
107b0f76b33SHarvey Harrison 				key->u.tkip.tx.iv16);
10850339a67SJohannes Berg 		break;
109e9f207f0SJiri Benc 	case ALG_CCMP:
110e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
111e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
112e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
11350339a67SJohannes Berg 		break;
1143cfcf6acSJouni Malinen 	case ALG_AES_CMAC:
1153cfcf6acSJouni Malinen 		tpn = key->u.aes_cmac.tx_pn;
1163cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
1173cfcf6acSJouni Malinen 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4],
1183cfcf6acSJouni Malinen 				tpn[5]);
1193cfcf6acSJouni Malinen 		break;
120e9f207f0SJiri Benc 	default:
121e9f207f0SJiri Benc 		return 0;
122e9f207f0SJiri Benc 	}
123e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
124e9f207f0SJiri Benc }
125e9f207f0SJiri Benc KEY_OPS(tx_spec);
126e9f207f0SJiri Benc 
127e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
128e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
129e9f207f0SJiri Benc {
130e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
131e9f207f0SJiri Benc 	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
132e9f207f0SJiri Benc 	int i, len;
133e9f207f0SJiri Benc 	const u8 *rpn;
134e9f207f0SJiri Benc 
1358f20fc24SJohannes Berg 	switch (key->conf.alg) {
136e9f207f0SJiri Benc 	case ALG_WEP:
137e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
13850339a67SJohannes Berg 		break;
139e9f207f0SJiri Benc 	case ALG_TKIP:
140e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
141e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
142e9f207f0SJiri Benc 				       "%08x %04x\n",
143b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv32,
144b0f76b33SHarvey Harrison 				       key->u.tkip.rx[i].iv16);
145e9f207f0SJiri Benc 		len = p - buf;
14650339a67SJohannes Berg 		break;
147e9f207f0SJiri Benc 	case ALG_CCMP:
1489190252cSJouni Malinen 		for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
149e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
150e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
151e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
152e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
153e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
154e9f207f0SJiri Benc 		}
155e9f207f0SJiri Benc 		len = p - buf;
15650339a67SJohannes Berg 		break;
1573cfcf6acSJouni Malinen 	case ALG_AES_CMAC:
1583cfcf6acSJouni Malinen 		rpn = key->u.aes_cmac.rx_pn;
1593cfcf6acSJouni Malinen 		p += scnprintf(p, sizeof(buf)+buf-p,
1603cfcf6acSJouni Malinen 			       "%02x%02x%02x%02x%02x%02x\n",
1613cfcf6acSJouni Malinen 			       rpn[0], rpn[1], rpn[2],
1623cfcf6acSJouni Malinen 			       rpn[3], rpn[4], rpn[5]);
1633cfcf6acSJouni Malinen 		len = p - buf;
1643cfcf6acSJouni Malinen 		break;
165e9f207f0SJiri Benc 	default:
166e9f207f0SJiri Benc 		return 0;
167e9f207f0SJiri Benc 	}
168e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
169e9f207f0SJiri Benc }
170e9f207f0SJiri Benc KEY_OPS(rx_spec);
171e9f207f0SJiri Benc 
172e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
173e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
174e9f207f0SJiri Benc {
175e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
176e9f207f0SJiri Benc 	char buf[20];
177e9f207f0SJiri Benc 	int len;
178e9f207f0SJiri Benc 
1793cfcf6acSJouni Malinen 	switch (key->conf.alg) {
1803cfcf6acSJouni Malinen 	case ALG_CCMP:
181e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
1823cfcf6acSJouni Malinen 		break;
1833cfcf6acSJouni Malinen 	case ALG_AES_CMAC:
1843cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
1853cfcf6acSJouni Malinen 				key->u.aes_cmac.replays);
1863cfcf6acSJouni Malinen 		break;
1873cfcf6acSJouni Malinen 	default:
1883cfcf6acSJouni Malinen 		return 0;
1893cfcf6acSJouni Malinen 	}
190e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
191e9f207f0SJiri Benc }
192e9f207f0SJiri Benc KEY_OPS(replays);
193e9f207f0SJiri Benc 
1943cfcf6acSJouni Malinen static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
1953cfcf6acSJouni Malinen 				  size_t count, loff_t *ppos)
1963cfcf6acSJouni Malinen {
1973cfcf6acSJouni Malinen 	struct ieee80211_key *key = file->private_data;
1983cfcf6acSJouni Malinen 	char buf[20];
1993cfcf6acSJouni Malinen 	int len;
2003cfcf6acSJouni Malinen 
2013cfcf6acSJouni Malinen 	switch (key->conf.alg) {
2023cfcf6acSJouni Malinen 	case ALG_AES_CMAC:
2033cfcf6acSJouni Malinen 		len = scnprintf(buf, sizeof(buf), "%u\n",
2043cfcf6acSJouni Malinen 				key->u.aes_cmac.icverrors);
2053cfcf6acSJouni Malinen 		break;
2063cfcf6acSJouni Malinen 	default:
2073cfcf6acSJouni Malinen 		return 0;
2083cfcf6acSJouni Malinen 	}
2093cfcf6acSJouni Malinen 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
2103cfcf6acSJouni Malinen }
2113cfcf6acSJouni Malinen KEY_OPS(icverrors);
2123cfcf6acSJouni Malinen 
213e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
214e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
215e9f207f0SJiri Benc {
216e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
2178f20fc24SJohannes Berg 	int i, res, bufsize = 2 * key->conf.keylen + 2;
218e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
219e9f207f0SJiri Benc 	char *p = buf;
220e9f207f0SJiri Benc 
2218f20fc24SJohannes Berg 	for (i = 0; i < key->conf.keylen; i++)
2228f20fc24SJohannes Berg 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
223e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
224e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
225e9f207f0SJiri Benc 	kfree(buf);
226e9f207f0SJiri Benc 	return res;
227e9f207f0SJiri Benc }
228e9f207f0SJiri Benc KEY_OPS(key);
229e9f207f0SJiri Benc 
230e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
2317bcfaf2fSJohannes Berg 	debugfs_create_file(#name, 0400, key->debugfs.dir, \
2327bcfaf2fSJohannes Berg 			    key, &key_##name##_ops);
233e9f207f0SJiri Benc 
2343b96766fSJohannes Berg void ieee80211_debugfs_key_add(struct ieee80211_key *key)
235e9f207f0SJiri Benc   {
23650339a67SJohannes Berg 	static int keycount;
2373b96766fSJohannes Berg 	char buf[50];
2383b96766fSJohannes Berg 	struct sta_info *sta;
239e9f207f0SJiri Benc 
2403b96766fSJohannes Berg 	if (!key->local->debugfs.keys)
241e9f207f0SJiri Benc 		return;
242e9f207f0SJiri Benc 
24350339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
244d9c58f30SJohannes Berg 	key->debugfs.cnt = keycount;
24550339a67SJohannes Berg 	keycount++;
246e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
2473b96766fSJohannes Berg 					key->local->debugfs.keys);
248e9f207f0SJiri Benc 
249e9f207f0SJiri Benc 	if (!key->debugfs.dir)
250e9f207f0SJiri Benc 		return;
251e9f207f0SJiri Benc 
2523b96766fSJohannes Berg 	rcu_read_lock();
2533b96766fSJohannes Berg 	sta = rcu_dereference(key->sta);
2543b96766fSJohannes Berg 	if (sta)
2550c68ae26SJohannes Berg 		sprintf(buf, "../../stations/%pM", sta->sta.addr);
2563b96766fSJohannes Berg 	rcu_read_unlock();
2573b96766fSJohannes Berg 
2583b96766fSJohannes Berg 	/* using sta as a boolean is fine outside RCU lock */
2593b96766fSJohannes Berg 	if (sta)
2603b96766fSJohannes Berg 		key->debugfs.stalink =
2613b96766fSJohannes Berg 			debugfs_create_symlink("station", key->debugfs.dir, buf);
2623b96766fSJohannes Berg 
263e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
2648f20fc24SJohannes Berg 	DEBUGFS_ADD(flags);
265e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
266e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
267e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
268e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
269e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
270e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
271e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
2723cfcf6acSJouni Malinen 	DEBUGFS_ADD(icverrors);
273e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
274e7a64f12SJohannes Berg 	DEBUGFS_ADD(ifindex);
275e9f207f0SJiri Benc };
276e9f207f0SJiri Benc 
277e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
278e9f207f0SJiri Benc {
279e9f207f0SJiri Benc 	if (!key)
280e9f207f0SJiri Benc 		return;
281e9f207f0SJiri Benc 
2827bcfaf2fSJohannes Berg 	debugfs_remove_recursive(key->debugfs.dir);
283e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
284e9f207f0SJiri Benc }
285e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
286e9f207f0SJiri Benc {
287e9f207f0SJiri Benc 	char buf[50];
28878520cadSJohannes Berg 	struct ieee80211_key *key;
289e9f207f0SJiri Benc 
2907bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
291e9f207f0SJiri Benc 		return;
292e9f207f0SJiri Benc 
29378520cadSJohannes Berg 	/* this is running under the key lock */
29478520cadSJohannes Berg 
29578520cadSJohannes Berg 	key = sdata->default_key;
29678520cadSJohannes Berg 	if (key) {
29778520cadSJohannes Berg 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
2987bcfaf2fSJohannes Berg 		sdata->debugfs.default_key =
29978520cadSJohannes Berg 			debugfs_create_symlink("default_key",
3007bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
30178520cadSJohannes Berg 	} else
30278520cadSJohannes Berg 		ieee80211_debugfs_key_remove_default(sdata);
303e9f207f0SJiri Benc }
30478520cadSJohannes Berg 
305e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
306e9f207f0SJiri Benc {
307e9f207f0SJiri Benc 	if (!sdata)
308e9f207f0SJiri Benc 		return;
309e9f207f0SJiri Benc 
3107bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_key);
3117bcfaf2fSJohannes Berg 	sdata->debugfs.default_key = NULL;
312e9f207f0SJiri Benc }
313e9f207f0SJiri Benc 
3143cfcf6acSJouni Malinen void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
3153cfcf6acSJouni Malinen {
3163cfcf6acSJouni Malinen 	char buf[50];
3173cfcf6acSJouni Malinen 	struct ieee80211_key *key;
3183cfcf6acSJouni Malinen 
3197bcfaf2fSJohannes Berg 	if (!sdata->debugfs.dir)
3203cfcf6acSJouni Malinen 		return;
3213cfcf6acSJouni Malinen 
3223cfcf6acSJouni Malinen 	/* this is running under the key lock */
3233cfcf6acSJouni Malinen 
3243cfcf6acSJouni Malinen 	key = sdata->default_mgmt_key;
3253cfcf6acSJouni Malinen 	if (key) {
3263cfcf6acSJouni Malinen 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
3277bcfaf2fSJohannes Berg 		sdata->debugfs.default_mgmt_key =
3283cfcf6acSJouni Malinen 			debugfs_create_symlink("default_mgmt_key",
3297bcfaf2fSJohannes Berg 					       sdata->debugfs.dir, buf);
3303cfcf6acSJouni Malinen 	} else
3313cfcf6acSJouni Malinen 		ieee80211_debugfs_key_remove_mgmt_default(sdata);
3323cfcf6acSJouni Malinen }
3333cfcf6acSJouni Malinen 
3343cfcf6acSJouni Malinen void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
3353cfcf6acSJouni Malinen {
3363cfcf6acSJouni Malinen 	if (!sdata)
3373cfcf6acSJouni Malinen 		return;
3383cfcf6acSJouni Malinen 
3397bcfaf2fSJohannes Berg 	debugfs_remove(sdata->debugfs.default_mgmt_key);
3407bcfaf2fSJohannes Berg 	sdata->debugfs.default_mgmt_key = NULL;
3413cfcf6acSJouni Malinen }
3423cfcf6acSJouni Malinen 
343e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
344e9f207f0SJiri Benc 				   struct sta_info *sta)
345e9f207f0SJiri Benc {
346e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
347e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
348e9f207f0SJiri Benc }
349