2 * Copyright 2003-2005 Devicescape Software, Inc.
3 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
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.
11 #include <linux/kobject.h>
12 #include "ieee80211_i.h"
13 #include "ieee80211_key.h"
15 #include "debugfs_key.h"
17 #define KEY_READ(name, buflen, format_string) \
18 static ssize_t key_##name##_read(struct file *file, \
19 char __user *userbuf, \
20 size_t count, loff_t *ppos) \
23 struct ieee80211_key *key = file->private_data; \
24 int res = scnprintf(buf, buflen, format_string, key->name); \
25 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
27 #define KEY_READ_D(name) KEY_READ(name, 20, "%d\n")
29 #define KEY_OPS(name) \
30 static const struct file_operations key_ ##name## _ops = { \
31 .read = key_##name##_read, \
32 .open = mac80211_open_file_generic, \
35 #define KEY_FILE(name, format) \
36 KEY_READ_##format(name) \
40 KEY_FILE(force_sw_encrypt, D);
42 KEY_FILE(hw_key_idx, D);
43 KEY_FILE(tx_rx_count, D);
45 static ssize_t key_algorithm_read(struct file *file,
47 size_t count, loff_t *ppos)
50 struct ieee80211_key *key = file->private_data;
65 return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg));
69 static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
70 size_t count, loff_t *ppos)
75 struct ieee80211_key *key = file->private_data;
79 len = scnprintf(buf, sizeof(buf), "\n");
82 len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
87 tpn = key->u.ccmp.tx_pn;
88 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
89 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]);
94 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
98 static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf,
99 size_t count, loff_t *ppos)
101 struct ieee80211_key *key = file->private_data;
102 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf;
108 len = scnprintf(buf, sizeof(buf), "\n");
111 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
112 p += scnprintf(p, sizeof(buf)+buf-p,
114 key->u.tkip.iv32_rx[i],
115 key->u.tkip.iv16_rx[i]);
119 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
120 rpn = key->u.ccmp.rx_pn[i];
121 p += scnprintf(p, sizeof(buf)+buf-p,
122 "%02x%02x%02x%02x%02x%02x\n",
123 rpn[0], rpn[1], rpn[2],
124 rpn[3], rpn[4], rpn[5]);
131 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
135 static ssize_t key_replays_read(struct file *file, char __user *userbuf,
136 size_t count, loff_t *ppos)
138 struct ieee80211_key *key = file->private_data;
142 if (key->alg != ALG_CCMP)
144 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
145 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
149 static ssize_t key_key_read(struct file *file, char __user *userbuf,
150 size_t count, loff_t *ppos)
152 struct ieee80211_key *key = file->private_data;
153 int i, res, bufsize = 2*key->keylen+2;
154 char *buf = kmalloc(bufsize, GFP_KERNEL);
157 for (i = 0; i < key->keylen; i++)
158 p += scnprintf(p, bufsize+buf-p, "%02x", key->key[i]);
159 p += scnprintf(p, bufsize+buf-p, "\n");
160 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
166 #define DEBUGFS_ADD(name) \
167 key->debugfs.name = debugfs_create_file(#name, 0400,\
168 key->debugfs.dir, key, &key_##name##_ops);
170 void ieee80211_debugfs_key_add(struct ieee80211_local *local,
171 struct ieee80211_key *key)
176 if (!local->debugfs.keys)
179 sprintf(buf, "%d", keycount);
181 key->debugfs.dir = debugfs_create_dir(buf,
182 local->debugfs.keys);
184 if (!key->debugfs.dir)
188 DEBUGFS_ADD(force_sw_encrypt);
190 DEBUGFS_ADD(hw_key_idx);
191 DEBUGFS_ADD(tx_rx_count);
192 DEBUGFS_ADD(algorithm);
193 DEBUGFS_ADD(tx_spec);
194 DEBUGFS_ADD(rx_spec);
195 DEBUGFS_ADD(replays);
199 #define DEBUGFS_DEL(name) \
200 debugfs_remove(key->debugfs.name); key->debugfs.name = NULL;
202 void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
208 DEBUGFS_DEL(force_sw_encrypt);
210 DEBUGFS_DEL(hw_key_idx);
211 DEBUGFS_DEL(tx_rx_count);
212 DEBUGFS_DEL(algorithm);
213 DEBUGFS_DEL(tx_spec);
214 DEBUGFS_DEL(rx_spec);
215 DEBUGFS_DEL(replays);
218 debugfs_remove(key->debugfs.stalink);
219 key->debugfs.stalink = NULL;
220 debugfs_remove(key->debugfs.dir);
221 key->debugfs.dir = NULL;
223 void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata)
227 if (!sdata->debugfsdir)
230 sprintf(buf, "../keys/%d", sdata->default_key->keyidx);
231 sdata->debugfs.default_key =
232 debugfs_create_symlink("default_key", sdata->debugfsdir, buf);
234 void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata)
239 debugfs_remove(sdata->debugfs.default_key);
240 sdata->debugfs.default_key = NULL;
242 void ieee80211_debugfs_key_sta_link(struct ieee80211_key *key,
243 struct sta_info *sta)
247 if (!key->debugfs.dir)
250 sprintf(buf, "../../stations/" MAC_FMT, MAC_ARG(sta->addr));
251 key->debugfs.stalink =
252 debugfs_create_symlink("station", key->debugfs.dir, buf);
255 void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key,
256 struct sta_info *sta)
258 debugfs_remove(key->debugfs.stalink);
259 key->debugfs.stalink = NULL;