xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision e9f207f0)
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");
80e9f207f0SJiri Benc 	case ALG_TKIP:
81e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
82e9f207f0SJiri Benc 				key->u.tkip.iv32,
83e9f207f0SJiri Benc 				key->u.tkip.iv16);
84e9f207f0SJiri Benc 	case ALG_CCMP:
85e9f207f0SJiri Benc 		tpn = key->u.ccmp.tx_pn;
86e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
87e9f207f0SJiri Benc 				tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
88e9f207f0SJiri Benc 	default:
89e9f207f0SJiri Benc 		return 0;
90e9f207f0SJiri Benc 	}
91e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
92e9f207f0SJiri Benc }
93e9f207f0SJiri Benc KEY_OPS(tx_spec);
94e9f207f0SJiri Benc 
95e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
96e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
97e9f207f0SJiri Benc {
98e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
99e9f207f0SJiri Benc 	char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
100e9f207f0SJiri Benc 	int i, len;
101e9f207f0SJiri Benc 	const u8 *rpn;
102e9f207f0SJiri Benc 
103e9f207f0SJiri Benc 	switch (key->alg) {
104e9f207f0SJiri Benc 	case ALG_WEP:
105e9f207f0SJiri Benc 		len = scnprintf(buf, sizeof(buf), "\n");
106e9f207f0SJiri Benc 	case ALG_TKIP:
107e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
108e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
109e9f207f0SJiri Benc 				       "%08x %04x\n",
110e9f207f0SJiri Benc 				       key->u.tkip.iv32_rx[i],
111e9f207f0SJiri Benc 				       key->u.tkip.iv16_rx[i]);
112e9f207f0SJiri Benc 		len = p - buf;
113e9f207f0SJiri Benc 	case ALG_CCMP:
114e9f207f0SJiri Benc 		for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
115e9f207f0SJiri Benc 			rpn = key->u.ccmp.rx_pn[i];
116e9f207f0SJiri Benc 			p += scnprintf(p, sizeof(buf)+buf-p,
117e9f207f0SJiri Benc 				       "%02x%02x%02x%02x%02x%02x\n",
118e9f207f0SJiri Benc 				       rpn[0], rpn[1], rpn[2],
119e9f207f0SJiri Benc 				       rpn[3], rpn[4], rpn[5]);
120e9f207f0SJiri Benc 		}
121e9f207f0SJiri Benc 		len = p - buf;
122e9f207f0SJiri Benc 	default:
123e9f207f0SJiri Benc 		return 0;
124e9f207f0SJiri Benc 	}
125e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
126e9f207f0SJiri Benc }
127e9f207f0SJiri Benc KEY_OPS(rx_spec);
128e9f207f0SJiri Benc 
129e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf,
130e9f207f0SJiri Benc 				size_t count, loff_t *ppos)
131e9f207f0SJiri Benc {
132e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
133e9f207f0SJiri Benc 	char buf[20];
134e9f207f0SJiri Benc 	int len;
135e9f207f0SJiri Benc 
136e9f207f0SJiri Benc 	if (key->alg != ALG_CCMP)
137e9f207f0SJiri Benc 		return 0;
138e9f207f0SJiri Benc 	len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
139e9f207f0SJiri Benc 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
140e9f207f0SJiri Benc }
141e9f207f0SJiri Benc KEY_OPS(replays);
142e9f207f0SJiri Benc 
143e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf,
144e9f207f0SJiri Benc 			    size_t count, loff_t *ppos)
145e9f207f0SJiri Benc {
146e9f207f0SJiri Benc 	struct ieee80211_key *key = file->private_data;
147e9f207f0SJiri Benc 	int i, res, bufsize = 2*key->keylen+2;
148e9f207f0SJiri Benc 	char *buf = kmalloc(bufsize, GFP_KERNEL);
149e9f207f0SJiri Benc 	char *p = buf;
150e9f207f0SJiri Benc 
151e9f207f0SJiri Benc 	for (i = 0; i < key->keylen; i++)
152e9f207f0SJiri Benc 		p += scnprintf(p, bufsize+buf-p, "%02x", key->key[i]);
153e9f207f0SJiri Benc 	p += scnprintf(p, bufsize+buf-p, "\n");
154e9f207f0SJiri Benc 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
155e9f207f0SJiri Benc 	kfree(buf);
156e9f207f0SJiri Benc 	return res;
157e9f207f0SJiri Benc }
158e9f207f0SJiri Benc KEY_OPS(key);
159e9f207f0SJiri Benc 
160e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \
161e9f207f0SJiri Benc 	key->debugfs.name = debugfs_create_file(#name, 0400,\
162e9f207f0SJiri Benc 				key->debugfs.dir, key, &key_##name##_ops);
163e9f207f0SJiri Benc 
164e9f207f0SJiri Benc void ieee80211_debugfs_key_add(struct ieee80211_local *local,
165e9f207f0SJiri Benc 			       struct ieee80211_key *key)
166e9f207f0SJiri Benc {
167e9f207f0SJiri Benc 	char buf[20];
168e9f207f0SJiri Benc 
169e9f207f0SJiri Benc 	if (!local->debugfs.keys)
170e9f207f0SJiri Benc 		return;
171e9f207f0SJiri Benc 
172e9f207f0SJiri Benc 	sprintf(buf, "%d", key->keyidx);
173e9f207f0SJiri Benc 	key->debugfs.dir = debugfs_create_dir(buf,
174e9f207f0SJiri Benc 					local->debugfs.keys);
175e9f207f0SJiri Benc 
176e9f207f0SJiri Benc 	if (!key->debugfs.dir)
177e9f207f0SJiri Benc 		return;
178e9f207f0SJiri Benc 
179e9f207f0SJiri Benc 	DEBUGFS_ADD(keylen);
180e9f207f0SJiri Benc 	DEBUGFS_ADD(force_sw_encrypt);
181e9f207f0SJiri Benc 	DEBUGFS_ADD(keyidx);
182e9f207f0SJiri Benc 	DEBUGFS_ADD(hw_key_idx);
183e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_rx_count);
184e9f207f0SJiri Benc 	DEBUGFS_ADD(algorithm);
185e9f207f0SJiri Benc 	DEBUGFS_ADD(tx_spec);
186e9f207f0SJiri Benc 	DEBUGFS_ADD(rx_spec);
187e9f207f0SJiri Benc 	DEBUGFS_ADD(replays);
188e9f207f0SJiri Benc 	DEBUGFS_ADD(key);
189e9f207f0SJiri Benc };
190e9f207f0SJiri Benc 
191e9f207f0SJiri Benc #define DEBUGFS_DEL(name) \
192e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
193e9f207f0SJiri Benc 
194e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
195e9f207f0SJiri Benc {
196e9f207f0SJiri Benc 	if (!key)
197e9f207f0SJiri Benc 		return;
198e9f207f0SJiri Benc 
199e9f207f0SJiri Benc 	DEBUGFS_DEL(keylen);
200e9f207f0SJiri Benc 	DEBUGFS_DEL(force_sw_encrypt);
201e9f207f0SJiri Benc 	DEBUGFS_DEL(keyidx);
202e9f207f0SJiri Benc 	DEBUGFS_DEL(hw_key_idx);
203e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_rx_count);
204e9f207f0SJiri Benc 	DEBUGFS_DEL(algorithm);
205e9f207f0SJiri Benc 	DEBUGFS_DEL(tx_spec);
206e9f207f0SJiri Benc 	DEBUGFS_DEL(rx_spec);
207e9f207f0SJiri Benc 	DEBUGFS_DEL(replays);
208e9f207f0SJiri Benc 	DEBUGFS_DEL(key);
209e9f207f0SJiri Benc 
210e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
211e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
212e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.dir);
213e9f207f0SJiri Benc 	key->debugfs.dir = NULL;
214e9f207f0SJiri Benc }
215e9f207f0SJiri Benc void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
216e9f207f0SJiri Benc {
217e9f207f0SJiri Benc 	char buf[50];
218e9f207f0SJiri Benc 
219e9f207f0SJiri Benc 	if (!sdata->debugfsdir)
220e9f207f0SJiri Benc 		return;
221e9f207f0SJiri Benc 
222e9f207f0SJiri Benc 	sprintf(buf, "../keys/%d", sdata->default_key->keyidx);
223e9f207f0SJiri Benc 	sdata->debugfs.default_key =
224e9f207f0SJiri Benc 		debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
225e9f207f0SJiri Benc }
226e9f207f0SJiri Benc void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
227e9f207f0SJiri Benc {
228e9f207f0SJiri Benc 	if (!sdata)
229e9f207f0SJiri Benc 		return;
230e9f207f0SJiri Benc 
231e9f207f0SJiri Benc 	debugfs_remove(sdata->debugfs.default_key);
232e9f207f0SJiri Benc 	sdata->debugfs.default_key = NULL;
233e9f207f0SJiri Benc }
234e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_link(struct ieee80211_key *key,
235e9f207f0SJiri Benc 				    struct sta_info *sta)
236e9f207f0SJiri Benc {
237e9f207f0SJiri Benc 	char buf[50];
238e9f207f0SJiri Benc 
239e9f207f0SJiri Benc 	if (!key->debugfs.dir)
240e9f207f0SJiri Benc 		return;
241e9f207f0SJiri Benc 
242e9f207f0SJiri Benc 	sprintf(buf, "../sta/" MAC_FMT, MAC_ARG(sta->addr));
243e9f207f0SJiri Benc 	key->debugfs.stalink =
244e9f207f0SJiri Benc 		debugfs_create_symlink("station", key->debugfs.dir, buf);
245e9f207f0SJiri Benc }
246e9f207f0SJiri Benc 
247e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
248e9f207f0SJiri Benc 				   struct sta_info *sta)
249e9f207f0SJiri Benc {
250e9f207f0SJiri Benc 	debugfs_remove(key->debugfs.stalink);
251e9f207f0SJiri Benc 	key->debugfs.stalink = NULL;
252e9f207f0SJiri Benc }
253