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, buflen, 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 char buf[buflen]; \ 24 struct ieee80211_key *key = file->private_data; \ 25 int res = scnprintf(buf, buflen, format_string, key->prop); \ 26 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \ 27 } 28 #define KEY_READ_D(name) KEY_READ(name, name, 20, "%d\n") 29 #define KEY_READ_X(name) KEY_READ(name, name, 20, "0x%x\n") 30 31 #define KEY_OPS(name) \ 32 static const struct file_operations key_ ##name## _ops = { \ 33 .read = key_##name##_read, \ 34 .open = mac80211_open_file_generic, \ 35 .llseek = generic_file_llseek, \ 36 } 37 38 #define KEY_FILE(name, format) \ 39 KEY_READ_##format(name) \ 40 KEY_OPS(name) 41 42 #define KEY_CONF_READ(name, buflen, format_string) \ 43 KEY_READ(conf_##name, conf.name, buflen, format_string) 44 #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, 20, "%d\n") 45 46 #define KEY_CONF_OPS(name) \ 47 static const struct file_operations key_ ##name## _ops = { \ 48 .read = key_conf_##name##_read, \ 49 .open = mac80211_open_file_generic, \ 50 .llseek = generic_file_llseek, \ 51 } 52 53 #define KEY_CONF_FILE(name, format) \ 54 KEY_CONF_READ_##format(name) \ 55 KEY_CONF_OPS(name) 56 57 KEY_CONF_FILE(keylen, D); 58 KEY_CONF_FILE(keyidx, D); 59 KEY_CONF_FILE(hw_key_idx, D); 60 KEY_FILE(flags, X); 61 KEY_FILE(tx_rx_count, D); 62 KEY_READ(ifindex, sdata->name, IFNAMSIZ + 2, "%s\n"); 63 KEY_OPS(ifindex); 64 65 static ssize_t key_algorithm_read(struct file *file, 66 char __user *userbuf, 67 size_t count, loff_t *ppos) 68 { 69 char *alg; 70 struct ieee80211_key *key = file->private_data; 71 72 switch (key->conf.alg) { 73 case ALG_WEP: 74 alg = "WEP\n"; 75 break; 76 case ALG_TKIP: 77 alg = "TKIP\n"; 78 break; 79 case ALG_CCMP: 80 alg = "CCMP\n"; 81 break; 82 case ALG_AES_CMAC: 83 alg = "AES-128-CMAC\n"; 84 break; 85 default: 86 return 0; 87 } 88 return simple_read_from_buffer(userbuf, count, ppos, alg, strlen(alg)); 89 } 90 KEY_OPS(algorithm); 91 92 static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf, 93 size_t count, loff_t *ppos) 94 { 95 const u8 *tpn; 96 char buf[20]; 97 int len; 98 struct ieee80211_key *key = file->private_data; 99 100 switch (key->conf.alg) { 101 case ALG_WEP: 102 len = scnprintf(buf, sizeof(buf), "\n"); 103 break; 104 case ALG_TKIP: 105 len = scnprintf(buf, sizeof(buf), "%08x %04x\n", 106 key->u.tkip.tx.iv32, 107 key->u.tkip.tx.iv16); 108 break; 109 case ALG_CCMP: 110 tpn = key->u.ccmp.tx_pn; 111 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n", 112 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], tpn[5]); 113 break; 114 case ALG_AES_CMAC: 115 tpn = key->u.aes_cmac.tx_pn; 116 len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n", 117 tpn[0], tpn[1], tpn[2], tpn[3], tpn[4], 118 tpn[5]); 119 break; 120 default: 121 return 0; 122 } 123 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 124 } 125 KEY_OPS(tx_spec); 126 127 static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf, 128 size_t count, loff_t *ppos) 129 { 130 struct ieee80211_key *key = file->private_data; 131 char buf[14*NUM_RX_DATA_QUEUES+1], *p = buf; 132 int i, len; 133 const u8 *rpn; 134 135 switch (key->conf.alg) { 136 case ALG_WEP: 137 len = scnprintf(buf, sizeof(buf), "\n"); 138 break; 139 case ALG_TKIP: 140 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 141 p += scnprintf(p, sizeof(buf)+buf-p, 142 "%08x %04x\n", 143 key->u.tkip.rx[i].iv32, 144 key->u.tkip.rx[i].iv16); 145 len = p - buf; 146 break; 147 case ALG_CCMP: 148 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) { 149 rpn = key->u.ccmp.rx_pn[i]; 150 p += scnprintf(p, sizeof(buf)+buf-p, 151 "%02x%02x%02x%02x%02x%02x\n", 152 rpn[0], rpn[1], rpn[2], 153 rpn[3], rpn[4], rpn[5]); 154 } 155 len = p - buf; 156 break; 157 case ALG_AES_CMAC: 158 rpn = key->u.aes_cmac.rx_pn; 159 p += scnprintf(p, sizeof(buf)+buf-p, 160 "%02x%02x%02x%02x%02x%02x\n", 161 rpn[0], rpn[1], rpn[2], 162 rpn[3], rpn[4], rpn[5]); 163 len = p - buf; 164 break; 165 default: 166 return 0; 167 } 168 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 169 } 170 KEY_OPS(rx_spec); 171 172 static ssize_t key_replays_read(struct file *file, char __user *userbuf, 173 size_t count, loff_t *ppos) 174 { 175 struct ieee80211_key *key = file->private_data; 176 char buf[20]; 177 int len; 178 179 switch (key->conf.alg) { 180 case ALG_CCMP: 181 len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays); 182 break; 183 case ALG_AES_CMAC: 184 len = scnprintf(buf, sizeof(buf), "%u\n", 185 key->u.aes_cmac.replays); 186 break; 187 default: 188 return 0; 189 } 190 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 191 } 192 KEY_OPS(replays); 193 194 static ssize_t key_icverrors_read(struct file *file, char __user *userbuf, 195 size_t count, loff_t *ppos) 196 { 197 struct ieee80211_key *key = file->private_data; 198 char buf[20]; 199 int len; 200 201 switch (key->conf.alg) { 202 case ALG_AES_CMAC: 203 len = scnprintf(buf, sizeof(buf), "%u\n", 204 key->u.aes_cmac.icverrors); 205 break; 206 default: 207 return 0; 208 } 209 return simple_read_from_buffer(userbuf, count, ppos, buf, len); 210 } 211 KEY_OPS(icverrors); 212 213 static ssize_t key_key_read(struct file *file, char __user *userbuf, 214 size_t count, loff_t *ppos) 215 { 216 struct ieee80211_key *key = file->private_data; 217 int i, res, bufsize = 2 * key->conf.keylen + 2; 218 char *buf = kmalloc(bufsize, GFP_KERNEL); 219 char *p = buf; 220 221 for (i = 0; i < key->conf.keylen; i++) 222 p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]); 223 p += scnprintf(p, bufsize+buf-p, "\n"); 224 res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); 225 kfree(buf); 226 return res; 227 } 228 KEY_OPS(key); 229 230 #define DEBUGFS_ADD(name) \ 231 debugfs_create_file(#name, 0400, key->debugfs.dir, \ 232 key, &key_##name##_ops); 233 234 void ieee80211_debugfs_key_add(struct ieee80211_key *key) 235 { 236 static int keycount; 237 char buf[50]; 238 struct sta_info *sta; 239 240 if (!key->local->debugfs.keys) 241 return; 242 243 sprintf(buf, "%d", keycount); 244 key->debugfs.cnt = keycount; 245 keycount++; 246 key->debugfs.dir = debugfs_create_dir(buf, 247 key->local->debugfs.keys); 248 249 if (!key->debugfs.dir) 250 return; 251 252 rcu_read_lock(); 253 sta = rcu_dereference(key->sta); 254 if (sta) 255 sprintf(buf, "../../stations/%pM", sta->sta.addr); 256 rcu_read_unlock(); 257 258 /* using sta as a boolean is fine outside RCU lock */ 259 if (sta) 260 key->debugfs.stalink = 261 debugfs_create_symlink("station", key->debugfs.dir, buf); 262 263 DEBUGFS_ADD(keylen); 264 DEBUGFS_ADD(flags); 265 DEBUGFS_ADD(keyidx); 266 DEBUGFS_ADD(hw_key_idx); 267 DEBUGFS_ADD(tx_rx_count); 268 DEBUGFS_ADD(algorithm); 269 DEBUGFS_ADD(tx_spec); 270 DEBUGFS_ADD(rx_spec); 271 DEBUGFS_ADD(replays); 272 DEBUGFS_ADD(icverrors); 273 DEBUGFS_ADD(key); 274 DEBUGFS_ADD(ifindex); 275 }; 276 277 void ieee80211_debugfs_key_remove(struct ieee80211_key *key) 278 { 279 if (!key) 280 return; 281 282 debugfs_remove_recursive(key->debugfs.dir); 283 key->debugfs.dir = NULL; 284 } 285 void ieee80211_debugfs_key_add_default(struct ieee80211_sub_if_data *sdata) 286 { 287 char buf[50]; 288 struct ieee80211_key *key; 289 290 if (!sdata->debugfs.dir) 291 return; 292 293 /* this is running under the key lock */ 294 295 key = sdata->default_key; 296 if (key) { 297 sprintf(buf, "../keys/%d", key->debugfs.cnt); 298 sdata->debugfs.default_key = 299 debugfs_create_symlink("default_key", 300 sdata->debugfs.dir, buf); 301 } else 302 ieee80211_debugfs_key_remove_default(sdata); 303 } 304 305 void ieee80211_debugfs_key_remove_default(struct ieee80211_sub_if_data *sdata) 306 { 307 if (!sdata) 308 return; 309 310 debugfs_remove(sdata->debugfs.default_key); 311 sdata->debugfs.default_key = NULL; 312 } 313 314 void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata) 315 { 316 char buf[50]; 317 struct ieee80211_key *key; 318 319 if (!sdata->debugfs.dir) 320 return; 321 322 /* this is running under the key lock */ 323 324 key = sdata->default_mgmt_key; 325 if (key) { 326 sprintf(buf, "../keys/%d", key->debugfs.cnt); 327 sdata->debugfs.default_mgmt_key = 328 debugfs_create_symlink("default_mgmt_key", 329 sdata->debugfs.dir, buf); 330 } else 331 ieee80211_debugfs_key_remove_mgmt_default(sdata); 332 } 333 334 void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata) 335 { 336 if (!sdata) 337 return; 338 339 debugfs_remove(sdata->debugfs.default_mgmt_key); 340 sdata->debugfs.default_mgmt_key = NULL; 341 } 342 343 void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key, 344 struct sta_info *sta) 345 { 346 debugfs_remove(key->debugfs.stalink); 347 key->debugfs.stalink = NULL; 348 } 349