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