Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 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" |
Johannes Berg | 2c8dccc | 2008-04-08 15:14:40 -0400 | [diff] [blame] | 13 | #include "key.h" |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 14 | #include "debugfs.h" |
| 15 | #include "debugfs_key.h" |
| 16 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 17 | #define KEY_READ(name, prop, buflen, format_string) \ |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 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; \ |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 24 | int res = scnprintf(buf, buflen, format_string, key->prop); \ |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 25 | return simple_read_from_buffer(userbuf, count, ppos, buf, res); \ |
| 26 | } |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 27 | #define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n") |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 28 | #define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n") |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 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 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 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") |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 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); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 57 | KEY_FILE(flags, X); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 58 | KEY_FILE(tx_rx_count, D); |
Johannes Berg | e7a64f1 | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 59 | KEY_READ(ifindex, sdata->dev->ifindex, 20, "%d\n"); |
| 60 | KEY_OPS(ifindex); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 61 | |
| 62 | static ssize_t key_algorithm_read(struct file *file, |
| 63 | char __user *userbuf, |
| 64 | size_t count, loff_t *ppos) |
| 65 | { |
| 66 | char *alg; |
| 67 | struct ieee80211_key *key = file->private_data; |
| 68 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 69 | switch (key->conf.alg) { |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 70 | case ALG_WEP: |
| 71 | alg = "WEP\n"; |
| 72 | break; |
| 73 | case ALG_TKIP: |
| 74 | alg = "TKIP\n"; |
| 75 | break; |
| 76 | case ALG_CCMP: |
| 77 | alg = "CCMP\n"; |
| 78 | break; |
| 79 | default: |
| 80 | return 0; |
| 81 | } |
| 82 | return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg)); |
| 83 | } |
| 84 | KEY_OPS(algorithm); |
| 85 | |
| 86 | static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf, |
| 87 | size_t count, loff_t *ppos) |
| 88 | { |
| 89 | const u8 *tpn; |
| 90 | char buf[20]; |
| 91 | int len; |
| 92 | struct ieee80211_key *key = file->private_data; |
| 93 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 94 | switch (key->conf.alg) { |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 95 | case ALG_WEP: |
| 96 | len = scnprintf(buf, sizeof(buf), "\n"); |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 97 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 98 | case ALG_TKIP: |
| 99 | len = scnprintf(buf, sizeof(buf), "%08x %04x\n", |
Harvey Harrison | b0f76b3 | 2008-05-14 16:26:19 -0700 | [diff] [blame] | 100 | key->u.tkip.tx.iv32, |
| 101 | key->u.tkip.tx.iv16); |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 102 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 103 | case ALG_CCMP: |
| 104 | tpn = key->u.ccmp.tx_pn; |
| 105 | len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n", |
| 106 | tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]); |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 107 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 108 | default: |
| 109 | return 0; |
| 110 | } |
| 111 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 112 | } |
| 113 | KEY_OPS(tx_spec); |
| 114 | |
| 115 | static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf, |
| 116 | size_t count, loff_t *ppos) |
| 117 | { |
| 118 | struct ieee80211_key *key = file->private_data; |
| 119 | char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf; |
| 120 | int i, len; |
| 121 | const u8 *rpn; |
| 122 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 123 | switch (key->conf.alg) { |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 124 | case ALG_WEP: |
| 125 | len = scnprintf(buf, sizeof(buf), "\n"); |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 126 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 127 | case ALG_TKIP: |
| 128 | for (i = 0; i < NUM_RX_DATA_QUEUES; i++) |
| 129 | p += scnprintf(p, sizeof(buf)+buf-p, |
| 130 | "%08x %04x\n", |
Harvey Harrison | b0f76b3 | 2008-05-14 16:26:19 -0700 | [diff] [blame] | 131 | key->u.tkip.rx[i].iv32, |
| 132 | key->u.tkip.rx[i].iv16); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 133 | len = p - buf; |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 134 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 135 | case ALG_CCMP: |
| 136 | for (i = 0; i < NUM_RX_DATA_QUEUES; i++) { |
| 137 | rpn = key->u.ccmp.rx_pn[i]; |
| 138 | p += scnprintf(p, sizeof(buf)+buf-p, |
| 139 | "%02x%02x%02x%02x%02x%02x\n", |
| 140 | rpn[0], rpn[1], rpn[2], |
| 141 | rpn[3], rpn[4], rpn[5]); |
| 142 | } |
| 143 | len = p - buf; |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 144 | break; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 145 | default: |
| 146 | return 0; |
| 147 | } |
| 148 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 149 | } |
| 150 | KEY_OPS(rx_spec); |
| 151 | |
| 152 | static ssize_t key_replays_read(struct file *file, char __user *userbuf, |
| 153 | size_t count, loff_t *ppos) |
| 154 | { |
| 155 | struct ieee80211_key *key = file->private_data; |
| 156 | char buf[20]; |
| 157 | int len; |
| 158 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 159 | if (key->conf.alg != ALG_CCMP) |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays); |
| 162 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 163 | } |
| 164 | KEY_OPS(replays); |
| 165 | |
| 166 | static ssize_t key_key_read(struct file *file, char __user *userbuf, |
| 167 | size_t count, loff_t *ppos) |
| 168 | { |
| 169 | struct ieee80211_key *key = file->private_data; |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 170 | int i, res, bufsize = 2 * key->conf.keylen + 2; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 171 | char *buf = kmalloc(bufsize, GFP_KERNEL); |
| 172 | char *p = buf; |
| 173 | |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 174 | for (i = 0; i < key->conf.keylen; i++) |
| 175 | p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 176 | p += scnprintf(p, bufsize+buf-p, "\n"); |
| 177 | res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); |
| 178 | kfree(buf); |
| 179 | return res; |
| 180 | } |
| 181 | KEY_OPS(key); |
| 182 | |
| 183 | #define DEBUGFS_ADD(name) \ |
| 184 | key->debugfs.name = debugfs_create_file(#name, 0400,\ |
| 185 | key->debugfs.dir, key, &key_##name##_ops); |
| 186 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 187 | void ieee80211_debugfs_key_add(struct ieee80211_key *key) |
| 188 | { |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 189 | static int keycount; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 190 | char buf[50]; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 191 | struct sta_info *sta; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 192 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 193 | if (!key->local->debugfs.keys) |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 194 | return; |
| 195 | |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 196 | sprintf(buf, "%d", keycount); |
Johannes Berg | d9c58f3 | 2008-04-08 16:46:36 -0400 | [diff] [blame] | 197 | key->debugfs.cnt = keycount; |
Johannes Berg | 50339a6 | 2007-08-28 17:01:52 -0400 | [diff] [blame] | 198 | keycount++; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 199 | key->debugfs.dir = debugfs_create_dir(buf, |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 200 | key->local->debugfs.keys); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 201 | |
| 202 | if (!key->debugfs.dir) |
| 203 | return; |
| 204 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 205 | rcu_read_lock(); |
| 206 | sta = rcu_dereference(key->sta); |
| 207 | if (sta) |
Johannes Berg | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame^] | 208 | sprintf(buf, "../../stations/%pM", sta->sta.addr); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 209 | rcu_read_unlock(); |
| 210 | |
| 211 | /* using sta as a boolean is fine outside RCU lock */ |
| 212 | if (sta) |
| 213 | key->debugfs.stalink = |
| 214 | debugfs_create_symlink("station", key->debugfs.dir, buf); |
| 215 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 216 | DEBUGFS_ADD(keylen); |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 217 | DEBUGFS_ADD(flags); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 218 | DEBUGFS_ADD(keyidx); |
| 219 | DEBUGFS_ADD(hw_key_idx); |
| 220 | DEBUGFS_ADD(tx_rx_count); |
| 221 | DEBUGFS_ADD(algorithm); |
| 222 | DEBUGFS_ADD(tx_spec); |
| 223 | DEBUGFS_ADD(rx_spec); |
| 224 | DEBUGFS_ADD(replays); |
| 225 | DEBUGFS_ADD(key); |
Johannes Berg | e7a64f1 | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 226 | DEBUGFS_ADD(ifindex); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | #define DEBUGFS_DEL(name) \ |
| 230 | debugfs_remove(key->debugfs.name); key->debugfs.name = NULL; |
| 231 | |
| 232 | void ieee80211_debugfs_key_remove(struct ieee80211_key *key) |
| 233 | { |
| 234 | if (!key) |
| 235 | return; |
| 236 | |
| 237 | DEBUGFS_DEL(keylen); |
Johannes Berg | 8f20fc2 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 238 | DEBUGFS_DEL(flags); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 239 | DEBUGFS_DEL(keyidx); |
| 240 | DEBUGFS_DEL(hw_key_idx); |
| 241 | DEBUGFS_DEL(tx_rx_count); |
| 242 | DEBUGFS_DEL(algorithm); |
| 243 | DEBUGFS_DEL(tx_spec); |
| 244 | DEBUGFS_DEL(rx_spec); |
| 245 | DEBUGFS_DEL(replays); |
| 246 | DEBUGFS_DEL(key); |
Johannes Berg | e7a64f1 | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 247 | DEBUGFS_DEL(ifindex); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 248 | |
| 249 | debugfs_remove(key->debugfs.stalink); |
| 250 | key->debugfs.stalink = NULL; |
| 251 | debugfs_remove(key->debugfs.dir); |
| 252 | key->debugfs.dir = NULL; |
| 253 | } |
| 254 | void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata) |
| 255 | { |
| 256 | char buf[50]; |
Johannes Berg | 78520ca | 2008-05-03 01:04:47 +0200 | [diff] [blame] | 257 | struct ieee80211_key *key; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 258 | |
| 259 | if (!sdata->debugfsdir) |
| 260 | return; |
| 261 | |
Johannes Berg | 78520ca | 2008-05-03 01:04:47 +0200 | [diff] [blame] | 262 | /* this is running under the key lock */ |
| 263 | |
| 264 | key = sdata->default_key; |
| 265 | if (key) { |
| 266 | sprintf(buf, "../keys/%d", key->debugfs.cnt); |
Jouni Malinen | 2b58b20 | 2008-08-28 15:12:06 +0300 | [diff] [blame] | 267 | sdata->common_debugfs.default_key = |
Johannes Berg | 78520ca | 2008-05-03 01:04:47 +0200 | [diff] [blame] | 268 | debugfs_create_symlink("default_key", |
| 269 | sdata->debugfsdir, buf); |
| 270 | } else |
| 271 | ieee80211_debugfs_key_remove_default(sdata); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 272 | } |
Johannes Berg | 78520ca | 2008-05-03 01:04:47 +0200 | [diff] [blame] | 273 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 274 | void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata) |
| 275 | { |
| 276 | if (!sdata) |
| 277 | return; |
| 278 | |
Jouni Malinen | 2b58b20 | 2008-08-28 15:12:06 +0300 | [diff] [blame] | 279 | debugfs_remove(sdata->common_debugfs.default_key); |
| 280 | sdata->common_debugfs.default_key = NULL; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 281 | } |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 282 | |
| 283 | void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key, |
| 284 | struct sta_info *sta) |
| 285 | { |
| 286 | debugfs_remove(key->debugfs.stalink); |
| 287 | key->debugfs.stalink = NULL; |
| 288 | } |