xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision e7a64f12)
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"
13e9f207f0SJiri Benc #include "ieee80211_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 
187e9f207f0SJiri Benc void ieee80211_debugfs_key_add(struct ieee80211_local *local,
188e9f207f0SJiri Benc 			       struct ieee80211_key *key)
189e9f207f0SJiri Benc {
19050339a67SJohannes Berg 	static int keycount;
191e9f207f0SJiri Benc 	char buf[20];
192e9f207f0SJiri Benc 
193e9f207f0SJiri Benc 	if (!local->debugfs.keys)
194e9f207f0SJiri Benc 		return;
195e9f207f0SJiri Benc 
19650339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
19750339a67SJohannes Berg 	keycount++;
198e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
199e9f207f0SJiri Benc 					local->debugfs.keys);
200e9f207f0SJiri Benc 
201e9f207f0SJiri Benc 	if (!key->debugfs.dir)
202e9f207f0SJiri Benc 		return;
203e9f207f0SJiri Benc 
204e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
2058f20fc24SJohannes Berg 	DEBUGFS_ADD(flags);
206e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
207e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
208e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
209e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
210e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
211e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
212e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
213e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
214e7a64f12SJohannes Berg 	DEBUGFS_ADD(ifindex);
215e9f207f0SJiri Benc };
216e9f207f0SJiri Benc 
217e9f207f0SJiri Benc #define DEBUGFS_DEL(name) \
218e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
219e9f207f0SJiri Benc 
220e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
221e9f207f0SJiri Benc {
222e9f207f0SJiri Benc 	if (!key)
223e9f207f0SJiri Benc 		return;
224e9f207f0SJiri Benc 
225e9f207f0SJiri Benc 	DEBUGFS_DEL(keylen);
2268f20fc24SJohannes Berg 	DEBUGFS_DEL(flags);
227e9f207f0SJiri Benc 	DEBUGFS_DEL(keyidx);
228e9f207f0SJiri Benc 	DEBUGFS_DEL(hw_key_idx);
229e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_rx_count);
230e9f207f0SJiri Benc 	DEBUGFS_DEL(algorithm);
231e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_spec);
232e9f207f0SJiri Benc 	DEBUGFS_DEL(rx_spec);
233e9f207f0SJiri Benc 	DEBUGFS_DEL(replays);
234e9f207f0SJiri Benc 	DEBUGFS_DEL(key);
235e7a64f12SJohannes Berg 	DEBUGFS_DEL(ifindex);
236e9f207f0SJiri Benc 
237e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
238e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
239e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.dir);
240e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
241e9f207f0SJiri Benc }
242e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
243e9f207f0SJiri Benc {
244e9f207f0SJiri Benc 	char buf[50];
245e9f207f0SJiri Benc 
246e9f207f0SJiri Benc 	if (!sdata->debugfsdir)
247e9f207f0SJiri Benc 		return;
248e9f207f0SJiri Benc 
2498f20fc24SJohannes Berg 	sprintf(buf, "../keys/%d", sdata->default_key->conf.keyidx);
250e9f207f0SJiri Benc 	sdata->debugfs.default_key =
251e9f207f0SJiri Benc 		debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
252e9f207f0SJiri Benc }
253e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
254e9f207f0SJiri Benc {
255e9f207f0SJiri Benc 	if (!sdata)
256e9f207f0SJiri Benc 		return;
257e9f207f0SJiri Benc 
258e9f207f0SJiri Benc 	debugfs_remove(sdata->debugfs.default_key);
259e9f207f0SJiri Benc 	sdata->debugfs.default_key = NULL;
260e9f207f0SJiri Benc }
261e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_link(struct ieee80211_key *key,
262e9f207f0SJiri Benc 				    struct sta_info *sta)
263e9f207f0SJiri Benc {
264e9f207f0SJiri Benc 	char buf[50];
265e9f207f0SJiri Benc 
266e9f207f0SJiri Benc 	if (!key->debugfs.dir)
267e9f207f0SJiri Benc 		return;
268e9f207f0SJiri Benc 
26950339a67SJohannes Berg 	sprintf(buf, "../../stations/" MAC_FMT, MAC_ARG(sta->addr));
270e9f207f0SJiri Benc 	key->debugfs.stalink =
271e9f207f0SJiri Benc 		debugfs_create_symlink("station", key->debugfs.dir, buf);
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