xref: /openbmc/linux/net/mac80211/debugfs_key.c (revision 4f3db074)
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 		pn = atomic64_read(&key->u.ccmp.tx_pn);
99 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
100 				(u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
101 				(u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
102 		break;
103 	case WLAN_CIPHER_SUITE_AES_CMAC:
104 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
105 		pn = atomic64_read(&key->u.aes_cmac.tx_pn);
106 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
107 				(u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
108 				(u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
109 		break;
110 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
111 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
112 		pn = atomic64_read(&key->u.aes_gmac.tx_pn);
113 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
114 				(u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
115 				(u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
116 		break;
117 	case WLAN_CIPHER_SUITE_GCMP:
118 	case WLAN_CIPHER_SUITE_GCMP_256:
119 		pn = atomic64_read(&key->u.gcmp.tx_pn);
120 		len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
121 				(u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
122 				(u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
123 		break;
124 	default:
125 		return 0;
126 	}
127 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
128 }
129 KEY_OPS(tx_spec);
130 
131 static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
132 				size_t count, loff_t *ppos)
133 {
134 	struct ieee80211_key *key = file->private_data;
135 	char buf[14*IEEE80211_NUM_TIDS+1], *p = buf;
136 	int i, len;
137 	const u8 *rpn;
138 
139 	switch (key->conf.cipher) {
140 	case WLAN_CIPHER_SUITE_WEP40:
141 	case WLAN_CIPHER_SUITE_WEP104:
142 		len = scnprintf(buf, sizeof(buf), "\n");
143 		break;
144 	case WLAN_CIPHER_SUITE_TKIP:
145 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
146 			p += scnprintf(p, sizeof(buf)+buf-p,
147 				       "%08x %04x\n",
148 				       key->u.tkip.rx[i].iv32,
149 				       key->u.tkip.rx[i].iv16);
150 		len = p - buf;
151 		break;
152 	case WLAN_CIPHER_SUITE_CCMP:
153 	case WLAN_CIPHER_SUITE_CCMP_256:
154 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
155 			rpn = key->u.ccmp.rx_pn[i];
156 			p += scnprintf(p, sizeof(buf)+buf-p,
157 				       "%02x%02x%02x%02x%02x%02x\n",
158 				       rpn[0], rpn[1], rpn[2],
159 				       rpn[3], rpn[4], rpn[5]);
160 		}
161 		len = p - buf;
162 		break;
163 	case WLAN_CIPHER_SUITE_AES_CMAC:
164 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
165 		rpn = key->u.aes_cmac.rx_pn;
166 		p += scnprintf(p, sizeof(buf)+buf-p,
167 			       "%02x%02x%02x%02x%02x%02x\n",
168 			       rpn[0], rpn[1], rpn[2],
169 			       rpn[3], rpn[4], rpn[5]);
170 		len = p - buf;
171 		break;
172 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
173 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
174 		rpn = key->u.aes_gmac.rx_pn;
175 		p += scnprintf(p, sizeof(buf)+buf-p,
176 			       "%02x%02x%02x%02x%02x%02x\n",
177 			       rpn[0], rpn[1], rpn[2],
178 			       rpn[3], rpn[4], rpn[5]);
179 		len = p - buf;
180 		break;
181 	case WLAN_CIPHER_SUITE_GCMP:
182 	case WLAN_CIPHER_SUITE_GCMP_256:
183 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
184 			rpn = key->u.gcmp.rx_pn[i];
185 			p += scnprintf(p, sizeof(buf)+buf-p,
186 				       "%02x%02x%02x%02x%02x%02x\n",
187 				       rpn[0], rpn[1], rpn[2],
188 				       rpn[3], rpn[4], rpn[5]);
189 		}
190 		len = p - buf;
191 		break;
192 	default:
193 		return 0;
194 	}
195 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
196 }
197 KEY_OPS(rx_spec);
198 
199 static ssize_t key_replays_read(struct file *file, char __user *userbuf,
200 				size_t count, loff_t *ppos)
201 {
202 	struct ieee80211_key *key = file->private_data;
203 	char buf[20];
204 	int len;
205 
206 	switch (key->conf.cipher) {
207 	case WLAN_CIPHER_SUITE_CCMP:
208 	case WLAN_CIPHER_SUITE_CCMP_256:
209 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
210 		break;
211 	case WLAN_CIPHER_SUITE_AES_CMAC:
212 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
213 		len = scnprintf(buf, sizeof(buf), "%u\n",
214 				key->u.aes_cmac.replays);
215 		break;
216 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
217 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
218 		len = scnprintf(buf, sizeof(buf), "%u\n",
219 				key->u.aes_gmac.replays);
220 		break;
221 	case WLAN_CIPHER_SUITE_GCMP:
222 	case WLAN_CIPHER_SUITE_GCMP_256:
223 		len = scnprintf(buf, sizeof(buf), "%u\n", key->u.gcmp.replays);
224 		break;
225 	default:
226 		return 0;
227 	}
228 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
229 }
230 KEY_OPS(replays);
231 
232 static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
233 				  size_t count, loff_t *ppos)
234 {
235 	struct ieee80211_key *key = file->private_data;
236 	char buf[20];
237 	int len;
238 
239 	switch (key->conf.cipher) {
240 	case WLAN_CIPHER_SUITE_AES_CMAC:
241 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
242 		len = scnprintf(buf, sizeof(buf), "%u\n",
243 				key->u.aes_cmac.icverrors);
244 		break;
245 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
246 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
247 		len = scnprintf(buf, sizeof(buf), "%u\n",
248 				key->u.aes_gmac.icverrors);
249 		break;
250 	default:
251 		return 0;
252 	}
253 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
254 }
255 KEY_OPS(icverrors);
256 
257 static ssize_t key_mic_failures_read(struct file *file, char __user *userbuf,
258 				     size_t count, loff_t *ppos)
259 {
260 	struct ieee80211_key *key = file->private_data;
261 	char buf[20];
262 	int len;
263 
264 	if (key->conf.cipher != WLAN_CIPHER_SUITE_TKIP)
265 		return -EINVAL;
266 
267 	len = scnprintf(buf, sizeof(buf), "%u\n", key->u.tkip.mic_failures);
268 
269 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
270 }
271 KEY_OPS(mic_failures);
272 
273 static ssize_t key_key_read(struct file *file, char __user *userbuf,
274 			    size_t count, loff_t *ppos)
275 {
276 	struct ieee80211_key *key = file->private_data;
277 	int i, bufsize = 2 * key->conf.keylen + 2;
278 	char *buf = kmalloc(bufsize, GFP_KERNEL);
279 	char *p = buf;
280 	ssize_t res;
281 
282 	if (!buf)
283 		return -ENOMEM;
284 
285 	for (i = 0; i < key->conf.keylen; i++)
286 		p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
287 	p += scnprintf(p, bufsize+buf-p, "\n");
288 	res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
289 	kfree(buf);
290 	return res;
291 }
292 KEY_OPS(key);
293 
294 #define DEBUGFS_ADD(name) \
295 	debugfs_create_file(#name, 0400, key->debugfs.dir, \
296 			    key, &key_##name##_ops);
297 
298 void ieee80211_debugfs_key_add(struct ieee80211_key *key)
299 {
300 	static int keycount;
301 	char buf[100];
302 	struct sta_info *sta;
303 
304 	if (!key->local->debugfs.keys)
305 		return;
306 
307 	sprintf(buf, "%d", keycount);
308 	key->debugfs.cnt = keycount;
309 	keycount++;
310 	key->debugfs.dir = debugfs_create_dir(buf,
311 					key->local->debugfs.keys);
312 
313 	if (!key->debugfs.dir)
314 		return;
315 
316 	sta = key->sta;
317 	if (sta) {
318 		sprintf(buf, "../../netdev:%s/stations/%pM",
319 			sta->sdata->name, sta->sta.addr);
320 		key->debugfs.stalink =
321 			debugfs_create_symlink("station", key->debugfs.dir, buf);
322 	}
323 
324 	DEBUGFS_ADD(keylen);
325 	DEBUGFS_ADD(flags);
326 	DEBUGFS_ADD(keyidx);
327 	DEBUGFS_ADD(hw_key_idx);
328 	DEBUGFS_ADD(tx_rx_count);
329 	DEBUGFS_ADD(algorithm);
330 	DEBUGFS_ADD(tx_spec);
331 	DEBUGFS_ADD(rx_spec);
332 	DEBUGFS_ADD(replays);
333 	DEBUGFS_ADD(icverrors);
334 	DEBUGFS_ADD(mic_failures);
335 	DEBUGFS_ADD(key);
336 	DEBUGFS_ADD(ifindex);
337 };
338 
339 void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
340 {
341 	if (!key)
342 		return;
343 
344 	debugfs_remove_recursive(key->debugfs.dir);
345 	key->debugfs.dir = NULL;
346 }
347 
348 void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
349 {
350 	char buf[50];
351 	struct ieee80211_key *key;
352 
353 	if (!sdata->vif.debugfs_dir)
354 		return;
355 
356 	lockdep_assert_held(&sdata->local->key_mtx);
357 
358 	debugfs_remove(sdata->debugfs.default_unicast_key);
359 	sdata->debugfs.default_unicast_key = NULL;
360 
361 	if (sdata->default_unicast_key) {
362 		key = key_mtx_dereference(sdata->local,
363 					  sdata->default_unicast_key);
364 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
365 		sdata->debugfs.default_unicast_key =
366 			debugfs_create_symlink("default_unicast_key",
367 					       sdata->vif.debugfs_dir, buf);
368 	}
369 
370 	debugfs_remove(sdata->debugfs.default_multicast_key);
371 	sdata->debugfs.default_multicast_key = NULL;
372 
373 	if (sdata->default_multicast_key) {
374 		key = key_mtx_dereference(sdata->local,
375 					  sdata->default_multicast_key);
376 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
377 		sdata->debugfs.default_multicast_key =
378 			debugfs_create_symlink("default_multicast_key",
379 					       sdata->vif.debugfs_dir, buf);
380 	}
381 }
382 
383 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
384 {
385 	char buf[50];
386 	struct ieee80211_key *key;
387 
388 	if (!sdata->vif.debugfs_dir)
389 		return;
390 
391 	key = key_mtx_dereference(sdata->local,
392 				  sdata->default_mgmt_key);
393 	if (key) {
394 		sprintf(buf, "../keys/%d", key->debugfs.cnt);
395 		sdata->debugfs.default_mgmt_key =
396 			debugfs_create_symlink("default_mgmt_key",
397 					       sdata->vif.debugfs_dir, buf);
398 	} else
399 		ieee80211_debugfs_key_remove_mgmt_default(sdata);
400 }
401 
402 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata)
403 {
404 	if (!sdata)
405 		return;
406 
407 	debugfs_remove(sdata->debugfs.default_mgmt_key);
408 	sdata->debugfs.default_mgmt_key = NULL;
409 }
410 
411 void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
412 				   struct sta_info *sta)
413 {
414 	debugfs_remove(key->debugfs.stalink);
415 	key->debugfs.stalink = NULL;
416 }
417