xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 50339a67)
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 
17e9f207f0SJiri Benc #define KEY_READ(name, 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;			\
24e9f207f0SJiri Benc 	int res = scnprintf(buf, buflen, format_string, key->name);	\
25e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, res);	\
26e9f207f0SJiri Benc }
27e9f207f0SJiri Benc #define KEY_READ_D(name) KEY_READ(name, 20, "%d\n")
28e9f207f0SJiri Benc 
29e9f207f0SJiri Benc #define KEY_OPS(name)							\
30e9f207f0SJiri Benc static const struct file_operations key_ ##name## _ops = {		\
31e9f207f0SJiri Benc 	.read = key_##name##_read,					\
32e9f207f0SJiri Benc 	.open = mac80211_open_file_generic,				\
33e9f207f0SJiri Benc }
34e9f207f0SJiri Benc 
35e9f207f0SJiri Benc #define KEY_FILE(name, format)						\
36e9f207f0SJiri Benc 		 KEY_READ_##format(name)				\
37e9f207f0SJiri Benc 		 KEY_OPS(name)
38e9f207f0SJiri Benc 
39e9f207f0SJiri Benc KEY_FILE(keylen, D);
40e9f207f0SJiri Benc KEY_FILE(force_sw_encrypt, D);
41e9f207f0SJiri Benc KEY_FILE(keyidx, D);
42e9f207f0SJiri Benc KEY_FILE(hw_key_idx, D);
43e9f207f0SJiri Benc KEY_FILE(tx_rx_count, D);
44e9f207f0SJiri Benc 
45e9f207f0SJiri Benc static ssize_t key_algorithm_read(struct file *file,
46e9f207f0SJiri Benc 				  char __user *userbuf,
47e9f207f0SJiri Benc 				  size_t count, loff_t *ppos)
48e9f207f0SJiri Benc {
49e9f207f0SJiri Benc 	char *alg;
50e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
51e9f207f0SJiri Benc 
52e9f207f0SJiri Benc 	switch (key->alg) {
53e9f207f0SJiri Benc 	case ALG_WEP:
54e9f207f0SJiri Benc 		alg = "WEP\n";
55e9f207f0SJiri Benc 		break;
56e9f207f0SJiri Benc 	case ALG_TKIP:
57e9f207f0SJiri Benc 		alg = "TKIP\n";
58e9f207f0SJiri Benc 		break;
59e9f207f0SJiri Benc 	case ALG_CCMP:
60e9f207f0SJiri Benc 		alg = "CCMP\n";
61e9f207f0SJiri Benc 		break;
62e9f207f0SJiri Benc 	default:
63e9f207f0SJiri Benc 		return 0;
64e9f207f0SJiri Benc 	}
65e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
66e9f207f0SJiri Benc }
67e9f207f0SJiri Benc KEY_OPS(algorithm);
68e9f207f0SJiri Benc 
69e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
70e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
71e9f207f0SJiri Benc {
72e9f207f0SJiri Benc 	const u8 *tpn;
73e9f207f0SJiri Benc 	char buf[20];
74e9f207f0SJiri Benc 	int len;
75e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
76e9f207f0SJiri Benc 
77e9f207f0SJiri Benc 	switch (key->alg) {
78e9f207f0SJiri Benc 	case ALG_WEP:
79e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
8050339a67SJohannes Berg 		break;
81e9f207f0SJiri Benc 	case ALG_TKIP:
82e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
83e9f207f0SJiri Benc 				key->u.tkip.iv32,
84e9f207f0SJiri Benc 				key->u.tkip.iv16);
8550339a67SJohannes Berg 		break;
86e9f207f0SJiri Benc 	case ALG_CCMP:
87e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
88e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
89e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
9050339a67SJohannes Berg 		break;
91e9f207f0SJiri Benc 	default:
92e9f207f0SJiri Benc 		return 0;
93e9f207f0SJiri Benc 	}
94e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
95e9f207f0SJiri Benc }
96e9f207f0SJiri Benc KEY_OPS(tx_spec);
97e9f207f0SJiri Benc 
98e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
99e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
100e9f207f0SJiri Benc {
101e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
102e9f207f0SJiri Benc 	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
103e9f207f0SJiri Benc 	int i, len;
104e9f207f0SJiri Benc 	const u8 *rpn;
105e9f207f0SJiri Benc 
106e9f207f0SJiri Benc 	switch (key->alg) {
107e9f207f0SJiri Benc 	case ALG_WEP:
108e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
10950339a67SJohannes Berg 		break;
110e9f207f0SJiri Benc 	case ALG_TKIP:
111e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
112e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
113e9f207f0SJiri Benc 				       "%08x %04x\n",
114e9f207f0SJiri Benc 				       key->u.tkip.iv32_rx[i],
115e9f207f0SJiri Benc 				       key->u.tkip.iv16_rx[i]);
116e9f207f0SJiri Benc 		len = p - buf;
11750339a67SJohannes Berg 		break;
118e9f207f0SJiri Benc 	case ALG_CCMP:
119e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
120e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
121e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
122e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
123e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
124e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
125e9f207f0SJiri Benc 		}
126e9f207f0SJiri Benc 		len = p - buf;
12750339a67SJohannes Berg 		break;
128e9f207f0SJiri Benc 	default:
129e9f207f0SJiri Benc 		return 0;
130e9f207f0SJiri Benc 	}
131e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
132e9f207f0SJiri Benc }
133e9f207f0SJiri Benc KEY_OPS(rx_spec);
134e9f207f0SJiri Benc 
135e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
136e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
137e9f207f0SJiri Benc {
138e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
139e9f207f0SJiri Benc 	char buf[20];
140e9f207f0SJiri Benc 	int len;
141e9f207f0SJiri Benc 
142e9f207f0SJiri Benc 	if (key->alg != ALG_CCMP)
143e9f207f0SJiri Benc 		return 0;
144e9f207f0SJiri Benc 	len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
145e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
146e9f207f0SJiri Benc }
147e9f207f0SJiri Benc KEY_OPS(replays);
148e9f207f0SJiri Benc 
149e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
150e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
151e9f207f0SJiri Benc {
152e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
153e9f207f0SJiri Benc 	int i, res, bufsize = 2*key->keylen+2;
154e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
155e9f207f0SJiri Benc 	char *p = buf;
156e9f207f0SJiri Benc 
157e9f207f0SJiri Benc 	for (i = 0; i < key->keylen; i++)
158e9f207f0SJiri Benc 		p += scnprintf(p, bufsize+buf-p, "%02x", key->key[i]);
159e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
160e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
161e9f207f0SJiri Benc 	kfree(buf);
162e9f207f0SJiri Benc 	return res;
163e9f207f0SJiri Benc }
164e9f207f0SJiri Benc KEY_OPS(key);
165e9f207f0SJiri Benc 
166e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
167e9f207f0SJiri Benc 	key->debugfs.name = debugfs_create_file(#name, 0400,\
168e9f207f0SJiri Benc 				key->debugfs.dir, key, &key_##name##_ops);
169e9f207f0SJiri Benc 
170e9f207f0SJiri Benc void ieee80211_debugfs_key_add(struct ieee80211_local *local,
171e9f207f0SJiri Benc 			       struct ieee80211_key *key)
172e9f207f0SJiri Benc {
17350339a67SJohannes Berg 	static int keycount;
174e9f207f0SJiri Benc 	char buf[20];
175e9f207f0SJiri Benc 
176e9f207f0SJiri Benc 	if (!local->debugfs.keys)
177e9f207f0SJiri Benc 		return;
178e9f207f0SJiri Benc 
17950339a67SJohannes Berg 	sprintf(buf, "%d", keycount);
18050339a67SJohannes Berg 	keycount++;
181e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
182e9f207f0SJiri Benc 					local->debugfs.keys);
183e9f207f0SJiri Benc 
184e9f207f0SJiri Benc 	if (!key->debugfs.dir)
185e9f207f0SJiri Benc 		return;
186e9f207f0SJiri Benc 
187e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
188e9f207f0SJiri Benc 	DEBUGFS_ADD(force_sw_encrypt);
189e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
190e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
191e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
192e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
193e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
194e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
195e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
196e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
197e9f207f0SJiri Benc };
198e9f207f0SJiri Benc 
199e9f207f0SJiri Benc #define DEBUGFS_DEL(name) \
200e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
201e9f207f0SJiri Benc 
202e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
203e9f207f0SJiri Benc {
204e9f207f0SJiri Benc 	if (!key)
205e9f207f0SJiri Benc 		return;
206e9f207f0SJiri Benc 
207e9f207f0SJiri Benc 	DEBUGFS_DEL(keylen);
208e9f207f0SJiri Benc 	DEBUGFS_DEL(force_sw_encrypt);
209e9f207f0SJiri Benc 	DEBUGFS_DEL(keyidx);
210e9f207f0SJiri Benc 	DEBUGFS_DEL(hw_key_idx);
211e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_rx_count);
212e9f207f0SJiri Benc 	DEBUGFS_DEL(algorithm);
213e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_spec);
214e9f207f0SJiri Benc 	DEBUGFS_DEL(rx_spec);
215e9f207f0SJiri Benc 	DEBUGFS_DEL(replays);
216e9f207f0SJiri Benc 	DEBUGFS_DEL(key);
217e9f207f0SJiri Benc 
218e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
219e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
220e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.dir);
221e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
222e9f207f0SJiri Benc }
223e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
224e9f207f0SJiri Benc {
225e9f207f0SJiri Benc 	char buf[50];
226e9f207f0SJiri Benc 
227e9f207f0SJiri Benc 	if (!sdata->debugfsdir)
228e9f207f0SJiri Benc 		return;
229e9f207f0SJiri Benc 
230e9f207f0SJiri Benc 	sprintf(buf, "../keys/%d", sdata->default_key->keyidx);
231e9f207f0SJiri Benc 	sdata->debugfs.default_key =
232e9f207f0SJiri Benc 		debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
233e9f207f0SJiri Benc }
234e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
235e9f207f0SJiri Benc {
236e9f207f0SJiri Benc 	if (!sdata)
237e9f207f0SJiri Benc 		return;
238e9f207f0SJiri Benc 
239e9f207f0SJiri Benc 	debugfs_remove(sdata->debugfs.default_key);
240e9f207f0SJiri Benc 	sdata->debugfs.default_key = NULL;
241e9f207f0SJiri Benc }
242e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_link(struct ieee80211_key *key,
243e9f207f0SJiri Benc 				    struct sta_info *sta)
244e9f207f0SJiri Benc {
245e9f207f0SJiri Benc 	char buf[50];
246e9f207f0SJiri Benc 
247e9f207f0SJiri Benc 	if (!key->debugfs.dir)
248e9f207f0SJiri Benc 		return;
249e9f207f0SJiri Benc 
25050339a67SJohannes Berg 	sprintf(buf, "../../stations/" MAC_FMT, MAC_ARG(sta->addr));
251e9f207f0SJiri Benc 	key->debugfs.stalink =
252e9f207f0SJiri Benc 		debugfs_create_symlink("station", key->debugfs.dir, buf);
253e9f207f0SJiri Benc }
254e9f207f0SJiri Benc 
255e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
256e9f207f0SJiri Benc 				   struct sta_info *sta)
257e9f207f0SJiri Benc {
258e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
259e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
260e9f207f0SJiri Benc }
261