xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 2c8dccc7)
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>
12e9f207f0SJiri Benc #include "ieee80211_i.h"
132c8dccc7SJohannes Berg #include "key.h"
14e9f207f0SJiri Benc #include "debugfs.h"
15e9f207f0SJiri Benc #include "debugfs_key.h"
16e9f207f0SJiri Benc 
178f20fc24SJohannes Berg #define KEY_READ(name, prop, buflen, format_string)			\
18e9f207f0SJiri Benc static ssize_t key_##name##_read(struct file *file,			\
19e9f207f0SJiri Benc 				 char __user *userbuf,			\
20e9f207f0SJiri Benc 				 size_t count, loff_t *ppos)		\
21e9f207f0SJiri Benc {									\
22e9f207f0SJiri Benc 	char buf[buflen];						\
23e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;			\
248f20fc24SJohannes Berg 	int res = scnprintf(buf, buflen, format_string, key->prop);	\
25e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, res);	\
26e9f207f0SJiri Benc }
278f20fc24SJohannes Berg #define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n")
2811a843b7SJohannes Berg #define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n")
29e9f207f0SJiri Benc 
30e9f207f0SJiri Benc #define KEY_OPS(name)							\
31e9f207f0SJiri Benc static const struct file_operations key_ ##name## _ops = {		\
32e9f207f0SJiri Benc 	.read = key_##name##_read,					\
33e9f207f0SJiri Benc 	.open = mac80211_open_file_generic,				\
34e9f207f0SJiri Benc }
35e9f207f0SJiri Benc 
36e9f207f0SJiri Benc #define KEY_FILE(name, format)						\
37e9f207f0SJiri Benc 		 KEY_READ_##format(name)				\
38e9f207f0SJiri Benc 		 KEY_OPS(name)
39e9f207f0SJiri Benc 
408f20fc24SJohannes Berg #define KEY_CONF_READ(name, buflen, format_string)			\
418f20fc24SJohannes Berg 	KEY_READ(conf_##name, conf.name, buflen, format_string)
428f20fc24SJohannes Berg #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n")
438f20fc24SJohannes Berg 
448f20fc24SJohannes Berg #define KEY_CONF_OPS(name)						\
458f20fc24SJohannes Berg static const struct file_operations key_ ##name## _ops = {		\
468f20fc24SJohannes Berg 	.read = key_conf_##name##_read,					\
478f20fc24SJohannes Berg 	.open = mac80211_open_file_generic,				\
488f20fc24SJohannes Berg }
498f20fc24SJohannes Berg 
508f20fc24SJohannes Berg #define KEY_CONF_FILE(name, format)					\
518f20fc24SJohannes Berg 		 KEY_CONF_READ_##format(name)				\
528f20fc24SJohannes Berg 		 KEY_CONF_OPS(name)
538f20fc24SJohannes Berg 
548f20fc24SJohannes Berg KEY_CONF_FILE(keylen, D);
558f20fc24SJohannes Berg KEY_CONF_FILE(keyidx, D);
568f20fc24SJohannes Berg KEY_CONF_FILE(hw_key_idx, D);
5711a843b7SJohannes Berg KEY_FILE(flags, X);
58e9f207f0SJiri Benc KEY_FILE(tx_rx_count, D);
59e7a64f12SJohannes Berg KEY_READ(ifindex, sdata->dev->ifindex, 20, "%d\n");
60e7a64f12SJohannes Berg KEY_OPS(ifindex);
61e9f207f0SJiri Benc 
62e9f207f0SJiri Benc static ssize_t key_algorithm_read(struct file *file,
63e9f207f0SJiri Benc 				  char __user *userbuf,
64e9f207f0SJiri Benc 				  size_t count, loff_t *ppos)
65e9f207f0SJiri Benc {
66e9f207f0SJiri Benc 	char *alg;
67e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
68e9f207f0SJiri Benc 
698f20fc24SJohannes Berg 	switch (key->conf.alg) {
70e9f207f0SJiri Benc 	case ALG_WEP:
71e9f207f0SJiri Benc 		alg = "WEP\n";
72e9f207f0SJiri Benc 		break;
73e9f207f0SJiri Benc 	case ALG_TKIP:
74e9f207f0SJiri Benc 		alg = "TKIP\n";
75e9f207f0SJiri Benc 		break;
76e9f207f0SJiri Benc 	case ALG_CCMP:
77e9f207f0SJiri Benc 		alg = "CCMP\n";
78e9f207f0SJiri Benc 		break;
79e9f207f0SJiri Benc 	default:
80e9f207f0SJiri Benc 		return 0;
81e9f207f0SJiri Benc 	}
82e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
83e9f207f0SJiri Benc }
84e9f207f0SJiri Benc KEY_OPS(algorithm);
85e9f207f0SJiri Benc 
86e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
87e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
88e9f207f0SJiri Benc {
89e9f207f0SJiri Benc 	const u8 *tpn;
90e9f207f0SJiri Benc 	char buf[20];
91e9f207f0SJiri Benc 	int len;
92e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
93e9f207f0SJiri Benc 
948f20fc24SJohannes Berg 	switch (key->conf.alg) {
95e9f207f0SJiri Benc 	case ALG_WEP:
96e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
9750339a67SJohannes Berg 		break;
98e9f207f0SJiri Benc 	case ALG_TKIP:
99e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
100e9f207f0SJiri Benc 				key->u.tkip.iv32,
101e9f207f0SJiri Benc 				key->u.tkip.iv16);
10250339a67SJohannes Berg 		break;
103e9f207f0SJiri Benc 	case ALG_CCMP:
104e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
105e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
106e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
10750339a67SJohannes Berg 		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 
1238f20fc24SJohannes Berg 	switch (key->conf.alg) {
124e9f207f0SJiri Benc 	case ALG_WEP:
125e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
12650339a67SJohannes Berg 		break;
127e9f207f0SJiri Benc 	case ALG_TKIP:
128e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
129e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
130e9f207f0SJiri Benc 				       "%08x %04x\n",
131e9f207f0SJiri Benc 				       key->u.tkip.iv32_rx[i],
132e9f207f0SJiri Benc 				       key->u.tkip.iv16_rx[i]);
133e9f207f0SJiri Benc 		len = p - buf;
13450339a67SJohannes Berg 		break;
135e9f207f0SJiri Benc 	case ALG_CCMP:
136e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
137e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
138e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
139e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
140e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
141e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
142e9f207f0SJiri Benc 		}
143e9f207f0SJiri Benc 		len = p - buf;
14450339a67SJohannes Berg 		break;
145e9f207f0SJiri Benc 	default:
146e9f207f0SJiri Benc 		return 0;
147e9f207f0SJiri Benc 	}
148e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
149e9f207f0SJiri Benc }
150e9f207f0SJiri Benc KEY_OPS(rx_spec);
151e9f207f0SJiri Benc 
152e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
153e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
154e9f207f0SJiri Benc {
155e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
156e9f207f0SJiri Benc 	char buf[20];
157e9f207f0SJiri Benc 	int len;
158e9f207f0SJiri Benc 
1598f20fc24SJohannes Berg 	if (key->conf.alg != ALG_CCMP)
160e9f207f0SJiri Benc 		return 0;
161e9f207f0SJiri Benc 	len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
162e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
163e9f207f0SJiri Benc }
164e9f207f0SJiri Benc KEY_OPS(replays);
165e9f207f0SJiri Benc 
166e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
167e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
168e9f207f0SJiri Benc {
169e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
1708f20fc24SJohannes Berg 	int i, res, bufsize = 2 * key->conf.keylen + 2;
171e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
172e9f207f0SJiri Benc 	char *p = buf;
173e9f207f0SJiri Benc 
1748f20fc24SJohannes Berg 	for (i = 0; i < key->conf.keylen; i++)
1758f20fc24SJohannes Berg 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
176e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
177e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
178e9f207f0SJiri Benc 	kfree(buf);
179e9f207f0SJiri Benc 	return res;
180e9f207f0SJiri Benc }
181e9f207f0SJiri Benc KEY_OPS(key);
182e9f207f0SJiri Benc 
183e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
184e9f207f0SJiri Benc 	key->debugfs.name = debugfs_create_file(#name, 0400,\
185e9f207f0SJiri Benc 				key->debugfs.dir, key, &key_##name##_ops);
186e9f207f0SJiri Benc 
1873b96766fSJohannes Berg void ieee80211_debugfs_key_add(struct ieee80211_key *key)
188e9f207f0SJiri Benc   {
18950339a67SJohannes Berg 	static int keycount;
1903b96766fSJohannes Berg 	char buf[50];
1913b96766fSJohannes Berg 	DECLARE_MAC_BUF(mac);
1923b96766fSJohannes Berg 	struct sta_info *sta;
193e9f207f0SJiri Benc 
1943b96766fSJohannes Berg 	if (!key->local->debugfs.keys)
195e9f207f0SJiri Benc 		return;
196e9f207f0SJiri Benc 
19750339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
19850339a67SJohannes Berg 	keycount++;
199e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
2003b96766fSJohannes Berg 					key->local->debugfs.keys);
201e9f207f0SJiri Benc 
202e9f207f0SJiri Benc 	if (!key->debugfs.dir)
203e9f207f0SJiri Benc 		return;
204e9f207f0SJiri Benc 
2053b96766fSJohannes Berg 	rcu_read_lock();
2063b96766fSJohannes Berg 	sta = rcu_dereference(key->sta);
2073b96766fSJohannes Berg 	if (sta)
2083b96766fSJohannes Berg 		sprintf(buf, "../../stations/%s", print_mac(mac, sta->addr));
2093b96766fSJohannes Berg 	rcu_read_unlock();
2103b96766fSJohannes Berg 
2113b96766fSJohannes Berg 	/* using sta as a boolean is fine outside RCU lock */
2123b96766fSJohannes Berg 	if (sta)
2133b96766fSJohannes Berg 		key->debugfs.stalink =
2143b96766fSJohannes Berg 			debugfs_create_symlink("station", key->debugfs.dir, buf);
2153b96766fSJohannes Berg 
216e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
2178f20fc24SJohannes Berg 	DEBUGFS_ADD(flags);
218e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
219e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
220e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
221e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
222e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
223e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
224e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
225e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
226e7a64f12SJohannes Berg 	DEBUGFS_ADD(ifindex);
227e9f207f0SJiri Benc };
228e9f207f0SJiri Benc 
229e9f207f0SJiri Benc #define DEBUGFS_DEL(name) \
230e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
231e9f207f0SJiri Benc 
232e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
233e9f207f0SJiri Benc {
234e9f207f0SJiri Benc 	if (!key)
235e9f207f0SJiri Benc 		return;
236e9f207f0SJiri Benc 
237e9f207f0SJiri Benc 	DEBUGFS_DEL(keylen);
2388f20fc24SJohannes Berg 	DEBUGFS_DEL(flags);
239e9f207f0SJiri Benc 	DEBUGFS_DEL(keyidx);
240e9f207f0SJiri Benc 	DEBUGFS_DEL(hw_key_idx);
241e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_rx_count);
242e9f207f0SJiri Benc 	DEBUGFS_DEL(algorithm);
243e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_spec);
244e9f207f0SJiri Benc 	DEBUGFS_DEL(rx_spec);
245e9f207f0SJiri Benc 	DEBUGFS_DEL(replays);
246e9f207f0SJiri Benc 	DEBUGFS_DEL(key);
247e7a64f12SJohannes Berg 	DEBUGFS_DEL(ifindex);
248e9f207f0SJiri Benc 
249e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
250e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
251e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.dir);
252e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
253e9f207f0SJiri Benc }
254e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
255e9f207f0SJiri Benc {
256e9f207f0SJiri Benc 	char buf[50];
257e9f207f0SJiri Benc 
258e9f207f0SJiri Benc 	if (!sdata->debugfsdir)
259e9f207f0SJiri Benc 		return;
260e9f207f0SJiri Benc 
2618f20fc24SJohannes Berg 	sprintf(buf, "../keys/%d", sdata->default_key->conf.keyidx);
262e9f207f0SJiri Benc 	sdata->debugfs.default_key =
263e9f207f0SJiri Benc 		debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
264e9f207f0SJiri Benc }
265e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
266e9f207f0SJiri Benc {
267e9f207f0SJiri Benc 	if (!sdata)
268e9f207f0SJiri Benc 		return;
269e9f207f0SJiri Benc 
270e9f207f0SJiri Benc 	debugfs_remove(sdata->debugfs.default_key);
271e9f207f0SJiri Benc 	sdata->debugfs.default_key = NULL;
272e9f207f0SJiri Benc }
273e9f207f0SJiri Benc 
274e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
275e9f207f0SJiri Benc 				   struct sta_info *sta)
276e9f207f0SJiri Benc {
277e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
278e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
279e9f207f0SJiri Benc }
280