xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision e2f1cf25)
1 /*
2  * Copyright 2003-2005	Devicescape Software, Inc.
3  * Copyright (c) 2006	Jiri Benc <jbenc@suse.cz>
4  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/kobject.h>
12 #include <linux/slab.h>
13 #include "ieee80211_i.h"
14 #include "key.h"
15 #include "debugfs.h"
16 #include "debugfs_key.h"
17 
18 #define KEY_READ(name, prop, format_string)				\
19 static ssize_t key_##name##_read(struct file *file,			\
20 				 char __user *userbuf,			\
21 				 size_t count, loff_t *ppos)		\
22 {									\
23 	struct ieee80211_key *key = file->private_data;			\
24 	return mac80211_format_buffer(userbuf, count, ppos, 		\
25 				      format_string, key->prop);	\
26 }
27 #define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
28 #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
29 
30 #define KEY_OPS(name)							\
31 static const struct file_operations key_ ##name## _ops = {		\
32 	.read = key_##name##_read,					\
33 	.open = simple_open,						\
34 	.llseek = generic_file_llseek,					\
35 }
36 
37 #define KEY_FILE(name, format)						\
38 		 KEY_READ_##format(name)				\
39 		 KEY_OPS(name)
40 
41 #define KEY_CONF_READ(name, format_string)				\
42 	KEY_READ(conf_##name, conf.name, format_string)
43 #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
44 
45 #define KEY_CONF_OPS(name)						\
46 static const struct file_operations key_ ##name## _ops = {		\
47 	.read = key_conf_##name##_read,					\
48 	.open = simple_open,						\
49 	.llseek = generic_file_llseek,					\
50 }
51 
52 #define KEY_CONF_FILE(name, format)					\
53 		 KEY_CONF_READ_##format(name)				\
54 		 KEY_CONF_OPS(name)
55 
56 KEY_CONF_FILE(keylen, D);
57 KEY_CONF_FILE(keyidx, D);
58 KEY_CONF_FILE(hw_key_idx, D);
59 KEY_FILE(flags, X);
60 KEY_FILE(tx_rx_count, D);
61 KEY_READ(ifindex, sdata->name, "%s\n");
62 KEY_OPS(ifindex);
63 
64 static ssize_t key_algorithm_read(struct file *file,
65 				  char __user *userbuf,
66 				  size_t count, loff_t *ppos)
67 {
68 	char buf[15];
69 	struct ieee80211_key *key = file->private_data;
70 	u32 c = key->conf.cipher;
71 
72 	sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
73 		c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
74 	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
75 }
76 KEY_OPS(algorithm);
77 
78 static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
79 				size_t count, loff_t *ppos)
80 {
81 	u64 pn;
82 	char buf[20];
83 	int len;
84 	struct ieee80211_key *key = file->private_data;
85 
86 	switch (key->conf.cipher) {
87 	case WLAN_CIPHER_SUITE_WEP40:
88 	case WLAN_CIPHER_SUITE_WEP104:
89 		len = scnprintf(buf, sizeof(buf), "\n");
90 		break;
91 	case WLAN_CIPHER_SUITE_TKIP:
92 		len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
93 				key->u.tkip.tx.iv32,
94 				key->u.tkip.tx.iv16);
95 		break;
96 	case WLAN_CIPHER_SUITE_CCMP:
97 	case WLAN_CIPHER_SUITE_CCMP_256:
98 	case WLAN_CIPHER_SUITE_AES_CMAC:
99 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
100 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
101 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
102 	case WLAN_CIPHER_SUITE_GCMP:
103 	case WLAN_CIPHER_SUITE_GCMP_256:
104 		pn = atomic64_read(&key->conf.tx_pn);
105 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
106 				(u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
107 				(u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
108 		break;
109 	default:
110 		return 0;
111 	}
112 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
113 }
114 KEY_OPS(tx_spec);
115 
116 static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
117 				size_t count, loff_t *ppos)
118 {
119 	struct ieee80211_key *key = file->private_data;
120 	char buf[14*IEEE80211_NUM_TIDS+1], *p = buf;
121 	int i, len;
122 	const u8 *rpn;
123 
124 	switch (key->conf.cipher) {
125 	case WLAN_CIPHER_SUITE_WEP40:
126 	case WLAN_CIPHER_SUITE_WEP104:
127 		len = scnprintf(buf, sizeof(buf), "\n");
128 		break;
129 	case WLAN_CIPHER_SUITE_TKIP:
130 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
131 			p += scnprintf(p, sizeof(buf)+buf-p,
132 				       "%08x %04x\n",
133 				       key->u.tkip.rx[i].iv32,
134 				       key->u.tkip.rx[i].iv16);
135 		len = p - buf;
136 		break;
137 	case WLAN_CIPHER_SUITE_CCMP:
138 	case WLAN_CIPHER_SUITE_CCMP_256:
139 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
140 			rpn = key->u.ccmp.rx_pn[i];
141 			p += scnprintf(p, sizeof(buf)+buf-p,
142 				       "%02x%02x%02x%02x%02x%02x\n",
143 				       rpn[0], rpn[1], rpn[2],
144 				       rpn[3], rpn[4], rpn[5]);
145 		}
146 		len = p - buf;
147 		break;
148 	case WLAN_CIPHER_SUITE_AES_CMAC:
149 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
150 		rpn = key->u.aes_cmac.rx_pn;
151 		p += scnprintf(p, sizeof(buf)+buf-p,
152 			       "%02x%02x%02x%02x%02x%02x\n",
153 			       rpn[0], rpn[1], rpn[2],
154 			       rpn[3], rpn[4], rpn[5]);
155 		len = p - buf;
156 		break;
157 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
158 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
159 		rpn = key->u.aes_gmac.rx_pn;
160 		p += scnprintf(p, sizeof(buf)+buf-p,
161 			       "%02x%02x%02x%02x%02x%02x\n",
162 			       rpn[0], rpn[1], rpn[2],
163 			       rpn[3], rpn[4], rpn[5]);
164 		len = p - buf;
165 		break;
166 	case WLAN_CIPHER_SUITE_GCMP:
167 	case WLAN_CIPHER_SUITE_GCMP_256:
168 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
169 			rpn = key->u.gcmp.rx_pn[i];
170 			p += scnprintf(p, sizeof(buf)+buf-p,
171 				       "%02x%02x%02x%02x%02x%02x\n",
172 				       rpn[0], rpn[1], rpn[2],
173 				       rpn[3], rpn[4], rpn[5]);
174 		}
175 		len = p - buf;
176 		break;
177 	default:
178 		return 0;
179 	}
180 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
181 }
182 KEY_OPS(rx_spec);
183 
184 static ssize_t key_replays_read(struct file *file, char __user *userbuf,
185 				size_t count, loff_t *ppos)
186 {
187 	struct ieee80211_key *key = file->private_data;
188 	char buf[20];
189 	int len;
190 
191 	switch (key->conf.cipher) {
192 	case WLAN_CIPHER_SUITE_CCMP:
193 	case WLAN_CIPHER_SUITE_CCMP_256:
194 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
195 		break;
196 	case WLAN_CIPHER_SUITE_AES_CMAC:
197 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
198 		len = scnprintf(buf, sizeof(buf), "%u\n",
199 				key->u.aes_cmac.replays);
200 		break;
201 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
202 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
203 		len = scnprintf(buf, sizeof(buf), "%u\n",
204 				key->u.aes_gmac.replays);
205 		break;
206 	case WLAN_CIPHER_SUITE_GCMP:
207 	case WLAN_CIPHER_SUITE_GCMP_256:
208 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.gcmp.replays);
209 		break;
210 	default:
211 		return 0;
212 	}
213 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
214 }
215 KEY_OPS(replays);
216 
217 static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
218 				  size_t count, loff_t *ppos)
219 {
220 	struct ieee80211_key *key = file->private_data;
221 	char buf[20];
222 	int len;
223 
224 	switch (key->conf.cipher) {
225 	case WLAN_CIPHER_SUITE_AES_CMAC:
226 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
227 		len = scnprintf(buf, sizeof(buf), "%u\n",
228 				key->u.aes_cmac.icverrors);
229 		break;
230 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
231 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
232 		len = scnprintf(buf, sizeof(buf), "%u\n",
233 				key->u.aes_gmac.icverrors);
234 		break;
235 	default:
236 		return 0;
237 	}
238 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
239 }
240 KEY_OPS(icverrors);
241 
242 static ssize_t key_mic_failures_read(struct file *file, char __user *userbuf,
243 				     size_t count, loff_t *ppos)
244 {
245 	struct ieee80211_key *key = file->private_data;
246 	char buf[20];
247 	int len;
248 
249 	if (key->conf.cipher != WLAN_CIPHER_SUITE_TKIP)
250 		return -EINVAL;
251 
252 	len = scnprintf(buf, sizeof(buf), "%u\n", key->u.tkip.mic_failures);
253 
254 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
255 }
256 KEY_OPS(mic_failures);
257 
258 static ssize_t key_key_read(struct file *file, char __user *userbuf,
259 			    size_t count, loff_t *ppos)
260 {
261 	struct ieee80211_key *key = file->private_data;
262 	int i, bufsize = 2 * key->conf.keylen + 2;
263 	char *buf = kmalloc(bufsize, GFP_KERNEL);
264 	char *p = buf;
265 	ssize_t res;
266 
267 	if (!buf)
268 		return -ENOMEM;
269 
270 	for (i = 0; i < key->conf.keylen; i++)
271 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
272 	p += scnprintf(p, bufsize+buf-p, "\n");
273 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
274 	kfree(buf);
275 	return res;
276 }
277 KEY_OPS(key);
278 
279 #define DEBUGFS_ADD(name) \
280 	debugfs_create_file(#name, 0400, key->debugfs.dir, \
281 			    key, &key_##name##_ops);
282 
283 void ieee80211_debugfs_key_add(struct ieee80211_key *key)
284 {
285 	static int keycount;
286 	char buf[100];
287 	struct sta_info *sta;
288 
289 	if (!key->local->debugfs.keys)
290 		return;
291 
292 	sprintf(buf, "%d", keycount);
293 	key->debugfs.cnt = keycount;
294 	keycount++;
295 	key->debugfs.dir = debugfs_create_dir(buf,
296 					key->local->debugfs.keys);
297 
298 	if (!key->debugfs.dir)
299 		return;
300 
301 	sta = key->sta;
302 	if (sta) {
303 		sprintf(buf, "../../netdev:%s/stations/%pM",
304 			sta->sdata->name, sta->sta.addr);
305 		key->debugfs.stalink =
306 			debugfs_create_symlink("station", key->debugfs.dir, buf);
307 	}
308 
309 	DEBUGFS_ADD(keylen);
310 	DEBUGFS_ADD(flags);
311 	DEBUGFS_ADD(keyidx);
312 	DEBUGFS_ADD(hw_key_idx);
313 	DEBUGFS_ADD(tx_rx_count);
314 	DEBUGFS_ADD(algorithm);
315 	DEBUGFS_ADD(tx_spec);
316 	DEBUGFS_ADD(rx_spec);
317 	DEBUGFS_ADD(replays);
318 	DEBUGFS_ADD(icverrors);
319 	DEBUGFS_ADD(mic_failures);
320 	DEBUGFS_ADD(key);
321 	DEBUGFS_ADD(ifindex);
322 };
323 
324 void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
325 {
326 	if (!key)
327 		return;
328 
329 	debugfs_remove_recursive(key->debugfs.dir);
330 	key->debugfs.dir = NULL;
331 }
332 
333 void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
334 {
335 	char buf[50];
336 	struct ieee80211_key *key;
337 
338 	if (!sdata->vif.debugfs_dir)
339 		return;
340 
341 	lockdep_assert_held(&sdata->local->key_mtx);
342 
343 	debugfs_remove(sdata->debugfs.default_unicast_key);
344 	sdata->debugfs.default_unicast_key = NULL;
345 
346 	if (sdata->default_unicast_key) {
347 		key = key_mtx_dereference(sdata->local,
348 					  sdata->default_unicast_key);
349 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
350 		sdata->debugfs.default_unicast_key =
351 			debugfs_create_symlink("default_unicast_key",
352 					       sdata->vif.debugfs_dir, buf);
353 	}
354 
355 	debugfs_remove(sdata->debugfs.default_multicast_key);
356 	sdata->debugfs.default_multicast_key = NULL;
357 
358 	if (sdata->default_multicast_key) {
359 		key = key_mtx_dereference(sdata->local,
360 					  sdata->default_multicast_key);
361 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
362 		sdata->debugfs.default_multicast_key =
363 			debugfs_create_symlink("default_multicast_key",
364 					       sdata->vif.debugfs_dir, buf);
365 	}
366 }
367 
368 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
369 {
370 	char buf[50];
371 	struct ieee80211_key *key;
372 
373 	if (!sdata->vif.debugfs_dir)
374 		return;
375 
376 	key = key_mtx_dereference(sdata->local,
377 				  sdata->default_mgmt_key);
378 	if (key) {
379 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
380 		sdata->debugfs.default_mgmt_key =
381 			debugfs_create_symlink("default_mgmt_key",
382 					       sdata->vif.debugfs_dir, buf);
383 	} else
384 		ieee80211_debugfs_key_remove_mgmt_default(sdata);
385 }
386 
387 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
388 {
389 	if (!sdata)
390 		return;
391 
392 	debugfs_remove(sdata->debugfs.default_mgmt_key);
393 	sdata->debugfs.default_mgmt_key = NULL;
394 }
395 
396 void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
397 				   struct sta_info *sta)
398 {
399 	debugfs_remove(key->debugfs.stalink);
400 	key->debugfs.stalink = NULL;
401 }
402