1e9f207f0SJiri Benc /* 2e9f207f0SJiri Benc * Copyright 2003-2005 Devicescape Software, Inc. 3e9f207f0SJiri Benc * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> 4e9f207f0SJiri Benc * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> 5e9f207f0SJiri Benc * 6e9f207f0SJiri Benc * This program is free software; you can redistribute it and/or modify 7e9f207f0SJiri Benc * it under the terms of the GNU General Public License version 2 as 8e9f207f0SJiri Benc * published by the Free Software Foundation. 9e9f207f0SJiri Benc */ 10e9f207f0SJiri Benc 11e9f207f0SJiri Benc #include <linux/kobject.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13e9f207f0SJiri Benc #include "ieee80211_i.h" 142c8dccc7SJohannes Berg #include "key.h" 15e9f207f0SJiri Benc #include "debugfs.h" 16e9f207f0SJiri Benc #include "debugfs_key.h" 17e9f207f0SJiri Benc 1807caf9d6SEliad Peller #define KEY_READ(name, prop, format_string) \ 19e9f207f0SJiri Benc static ssize_t key_##name##_read(struct file *file, \ 20e9f207f0SJiri Benc char __user *userbuf, \ 21e9f207f0SJiri Benc size_t count, loff_t *ppos) \ 22e9f207f0SJiri Benc { \ 23e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; \ 2407caf9d6SEliad Peller return mac80211_format_buffer(userbuf, count, ppos, \ 2507caf9d6SEliad Peller format_string, key->prop); \ 26e9f207f0SJiri Benc } 2707caf9d6SEliad Peller #define KEY_READ_D(name) KEY_READ(name, name, "%d\n") 2807caf9d6SEliad Peller #define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n") 29e9f207f0SJiri Benc 30e9f207f0SJiri Benc #define KEY_OPS(name) \ 31e9f207f0SJiri Benc static const struct file_operations key_ ##name## _ops = { \ 32e9f207f0SJiri Benc .read = key_##name##_read, \ 33234e3405SStephen Boyd .open = simple_open, \ 342b18ab36SArnd Bergmann .llseek = generic_file_llseek, \ 35e9f207f0SJiri Benc } 36e9f207f0SJiri Benc 37e9f207f0SJiri Benc #define KEY_FILE(name, format) \ 38e9f207f0SJiri Benc KEY_READ_##format(name) \ 39e9f207f0SJiri Benc KEY_OPS(name) 40e9f207f0SJiri Benc 4107caf9d6SEliad Peller #define KEY_CONF_READ(name, format_string) \ 4207caf9d6SEliad Peller KEY_READ(conf_##name, conf.name, format_string) 4307caf9d6SEliad Peller #define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n") 448f20fc24SJohannes Berg 458f20fc24SJohannes Berg #define KEY_CONF_OPS(name) \ 468f20fc24SJohannes Berg static const struct file_operations key_ ##name## _ops = { \ 478f20fc24SJohannes Berg .read = key_conf_##name##_read, \ 48234e3405SStephen Boyd .open = simple_open, \ 492b18ab36SArnd Bergmann .llseek = generic_file_llseek, \ 508f20fc24SJohannes Berg } 518f20fc24SJohannes Berg 528f20fc24SJohannes Berg #define KEY_CONF_FILE(name, format) \ 538f20fc24SJohannes Berg KEY_CONF_READ_##format(name) \ 548f20fc24SJohannes Berg KEY_CONF_OPS(name) 558f20fc24SJohannes Berg 568f20fc24SJohannes Berg KEY_CONF_FILE(keylen, D); 578f20fc24SJohannes Berg KEY_CONF_FILE(keyidx, D); 588f20fc24SJohannes Berg KEY_CONF_FILE(hw_key_idx, D); 5911a843b7SJohannes Berg KEY_FILE(flags, X); 60e9f207f0SJiri Benc KEY_FILE(tx_rx_count, D); 6107caf9d6SEliad Peller KEY_READ(ifindex, sdata->name, "%s\n"); 62e7a64f12SJohannes Berg KEY_OPS(ifindex); 63e9f207f0SJiri Benc 64e9f207f0SJiri Benc static ssize_t key_algorithm_read(struct file *file, 65e9f207f0SJiri Benc char __user *userbuf, 66e9f207f0SJiri Benc size_t count, loff_t *ppos) 67e9f207f0SJiri Benc { 6897359d12SJohannes Berg char buf[15]; 69e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; 7097359d12SJohannes Berg u32 c = key->conf.cipher; 71e9f207f0SJiri Benc 7297359d12SJohannes Berg sprintf(buf, "%.2x-%.2x-%.2x:%d\n", 7397359d12SJohannes Berg c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff); 7497359d12SJohannes Berg return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); 75e9f207f0SJiri Benc } 76e9f207f0SJiri Benc KEY_OPS(algorithm); 77e9f207f0SJiri Benc 78e9f207f0SJiri Benc static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf, 79e9f207f0SJiri Benc size_t count, loff_t *ppos) 80e9f207f0SJiri Benc { 81aba83a0bSJohannes Berg u64 pn; 82e9f207f0SJiri Benc char buf[20]; 83e9f207f0SJiri Benc int len; 84e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; 85e9f207f0SJiri Benc 8697359d12SJohannes Berg switch (key->conf.cipher) { 8797359d12SJohannes Berg case WLAN_CIPHER_SUITE_WEP40: 8897359d12SJohannes Berg case WLAN_CIPHER_SUITE_WEP104: 89e9f207f0SJiri Benc len = scnprintf(buf, sizeof(buf), "\n"); 9050339a67SJohannes Berg break; 9197359d12SJohannes Berg case WLAN_CIPHER_SUITE_TKIP: 92e9f207f0SJiri Benc len = scnprintf(buf, sizeof(buf), "%08x %04x\n", 93b0f76b33SHarvey Harrison key->u.tkip.tx.iv32, 94b0f76b33SHarvey Harrison key->u.tkip.tx.iv16); 9550339a67SJohannes Berg break; 9697359d12SJohannes Berg case WLAN_CIPHER_SUITE_CCMP: 97aba83a0bSJohannes Berg pn = atomic64_read(&key->u.ccmp.tx_pn); 98e9f207f0SJiri Benc len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n", 99aba83a0bSJohannes Berg (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24), 100aba83a0bSJohannes Berg (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn); 10150339a67SJohannes Berg break; 10297359d12SJohannes Berg case WLAN_CIPHER_SUITE_AES_CMAC: 10375396ae6SJohannes Berg pn = atomic64_read(&key->u.aes_cmac.tx_pn); 1043cfcf6acSJouni Malinen len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n", 10575396ae6SJohannes Berg (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24), 10675396ae6SJohannes Berg (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn); 1073cfcf6acSJouni Malinen break; 108e9f207f0SJiri Benc default: 109e9f207f0SJiri Benc return 0; 110e9f207f0SJiri Benc } 111e9f207f0SJiri Benc return simple_read_from_buffer(userbuf, count, ppos, buf, len); 112e9f207f0SJiri Benc } 113e9f207f0SJiri Benc KEY_OPS(tx_spec); 114e9f207f0SJiri Benc 115e9f207f0SJiri Benc static ssize_t key_rx_spec_read(struct file *file, char __user *userbuf, 116e9f207f0SJiri Benc size_t count, loff_t *ppos) 117e9f207f0SJiri Benc { 118e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; 1195a306f58SJohannes Berg char buf[14*IEEE80211_NUM_TIDS+1], *p = buf; 120e9f207f0SJiri Benc int i, len; 121e9f207f0SJiri Benc const u8 *rpn; 122e9f207f0SJiri Benc 12397359d12SJohannes Berg switch (key->conf.cipher) { 12497359d12SJohannes Berg case WLAN_CIPHER_SUITE_WEP40: 12597359d12SJohannes Berg case WLAN_CIPHER_SUITE_WEP104: 126e9f207f0SJiri Benc len = scnprintf(buf, sizeof(buf), "\n"); 12750339a67SJohannes Berg break; 12897359d12SJohannes Berg case WLAN_CIPHER_SUITE_TKIP: 1295a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 130e9f207f0SJiri Benc p += scnprintf(p, sizeof(buf)+buf-p, 131e9f207f0SJiri Benc "%08x %04x\n", 132b0f76b33SHarvey Harrison key->u.tkip.rx[i].iv32, 133b0f76b33SHarvey Harrison key->u.tkip.rx[i].iv16); 134e9f207f0SJiri Benc len = p - buf; 13550339a67SJohannes Berg break; 13697359d12SJohannes Berg case WLAN_CIPHER_SUITE_CCMP: 1375a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) { 138e9f207f0SJiri Benc rpn = key->u.ccmp.rx_pn[i]; 139e9f207f0SJiri Benc p += scnprintf(p, sizeof(buf)+buf-p, 140e9f207f0SJiri Benc "%02x%02x%02x%02x%02x%02x\n", 141e9f207f0SJiri Benc rpn[0], rpn[1], rpn[2], 142e9f207f0SJiri Benc rpn[3], rpn[4], rpn[5]); 143e9f207f0SJiri Benc } 144e9f207f0SJiri Benc len = p - buf; 14550339a67SJohannes Berg break; 14697359d12SJohannes Berg case WLAN_CIPHER_SUITE_AES_CMAC: 1473cfcf6acSJouni Malinen rpn = key->u.aes_cmac.rx_pn; 1483cfcf6acSJouni Malinen p += scnprintf(p, sizeof(buf)+buf-p, 1493cfcf6acSJouni Malinen "%02x%02x%02x%02x%02x%02x\n", 1503cfcf6acSJouni Malinen rpn[0], rpn[1], rpn[2], 1513cfcf6acSJouni Malinen rpn[3], rpn[4], rpn[5]); 1523cfcf6acSJouni Malinen len = p - buf; 1533cfcf6acSJouni Malinen break; 154e9f207f0SJiri Benc default: 155e9f207f0SJiri Benc return 0; 156e9f207f0SJiri Benc } 157e9f207f0SJiri Benc return simple_read_from_buffer(userbuf, count, ppos, buf, len); 158e9f207f0SJiri Benc } 159e9f207f0SJiri Benc KEY_OPS(rx_spec); 160e9f207f0SJiri Benc 161e9f207f0SJiri Benc static ssize_t key_replays_read(struct file *file, char __user *userbuf, 162e9f207f0SJiri Benc size_t count, loff_t *ppos) 163e9f207f0SJiri Benc { 164e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; 165e9f207f0SJiri Benc char buf[20]; 166e9f207f0SJiri Benc int len; 167e9f207f0SJiri Benc 16897359d12SJohannes Berg switch (key->conf.cipher) { 16997359d12SJohannes Berg case WLAN_CIPHER_SUITE_CCMP: 170e9f207f0SJiri Benc len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays); 1713cfcf6acSJouni Malinen break; 17297359d12SJohannes Berg case WLAN_CIPHER_SUITE_AES_CMAC: 1733cfcf6acSJouni Malinen len = scnprintf(buf, sizeof(buf), "%u\n", 1743cfcf6acSJouni Malinen key->u.aes_cmac.replays); 1753cfcf6acSJouni Malinen break; 1763cfcf6acSJouni Malinen default: 1773cfcf6acSJouni Malinen return 0; 1783cfcf6acSJouni Malinen } 179e9f207f0SJiri Benc return simple_read_from_buffer(userbuf, count, ppos, buf, len); 180e9f207f0SJiri Benc } 181e9f207f0SJiri Benc KEY_OPS(replays); 182e9f207f0SJiri Benc 1833cfcf6acSJouni Malinen static ssize_t key_icverrors_read(struct file *file, char __user *userbuf, 1843cfcf6acSJouni Malinen size_t count, loff_t *ppos) 1853cfcf6acSJouni Malinen { 1863cfcf6acSJouni Malinen struct ieee80211_key *key = file->private_data; 1873cfcf6acSJouni Malinen char buf[20]; 1883cfcf6acSJouni Malinen int len; 1893cfcf6acSJouni Malinen 19097359d12SJohannes Berg switch (key->conf.cipher) { 19197359d12SJohannes Berg case WLAN_CIPHER_SUITE_AES_CMAC: 1923cfcf6acSJouni Malinen len = scnprintf(buf, sizeof(buf), "%u\n", 1933cfcf6acSJouni Malinen key->u.aes_cmac.icverrors); 1943cfcf6acSJouni Malinen break; 1953cfcf6acSJouni Malinen default: 1963cfcf6acSJouni Malinen return 0; 1973cfcf6acSJouni Malinen } 1983cfcf6acSJouni Malinen return simple_read_from_buffer(userbuf, count, ppos, buf, len); 1993cfcf6acSJouni Malinen } 2003cfcf6acSJouni Malinen KEY_OPS(icverrors); 2013cfcf6acSJouni Malinen 202e9f207f0SJiri Benc static ssize_t key_key_read(struct file *file, char __user *userbuf, 203e9f207f0SJiri Benc size_t count, loff_t *ppos) 204e9f207f0SJiri Benc { 205e9f207f0SJiri Benc struct ieee80211_key *key = file->private_data; 206520efd1aSJesper Juhl int i, bufsize = 2 * key->conf.keylen + 2; 207e9f207f0SJiri Benc char *buf = kmalloc(bufsize, GFP_KERNEL); 208e9f207f0SJiri Benc char *p = buf; 209520efd1aSJesper Juhl ssize_t res; 210520efd1aSJesper Juhl 211520efd1aSJesper Juhl if (!buf) 212520efd1aSJesper Juhl return -ENOMEM; 213e9f207f0SJiri Benc 2148f20fc24SJohannes Berg for (i = 0; i < key->conf.keylen; i++) 2158f20fc24SJohannes Berg p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]); 216e9f207f0SJiri Benc p += scnprintf(p, bufsize+buf-p, "\n"); 217e9f207f0SJiri Benc res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf); 218e9f207f0SJiri Benc kfree(buf); 219e9f207f0SJiri Benc return res; 220e9f207f0SJiri Benc } 221e9f207f0SJiri Benc KEY_OPS(key); 222e9f207f0SJiri Benc 223e9f207f0SJiri Benc #define DEBUGFS_ADD(name) \ 2247bcfaf2fSJohannes Berg debugfs_create_file(#name, 0400, key->debugfs.dir, \ 2257bcfaf2fSJohannes Berg key, &key_##name##_ops); 226e9f207f0SJiri Benc 2273b96766fSJohannes Berg void ieee80211_debugfs_key_add(struct ieee80211_key *key) 228e9f207f0SJiri Benc { 22950339a67SJohannes Berg static int keycount; 2309446f3efSJohannes Berg char buf[100]; 2313b96766fSJohannes Berg struct sta_info *sta; 232e9f207f0SJiri Benc 2333b96766fSJohannes Berg if (!key->local->debugfs.keys) 234e9f207f0SJiri Benc return; 235e9f207f0SJiri Benc 23650339a67SJohannes Berg sprintf(buf, "%d", keycount); 237d9c58f30SJohannes Berg key->debugfs.cnt = keycount; 23850339a67SJohannes Berg keycount++; 239e9f207f0SJiri Benc key->debugfs.dir = debugfs_create_dir(buf, 2403b96766fSJohannes Berg key->local->debugfs.keys); 241e9f207f0SJiri Benc 242e9f207f0SJiri Benc if (!key->debugfs.dir) 243e9f207f0SJiri Benc return; 244e9f207f0SJiri Benc 24540b275b6SJohannes Berg sta = key->sta; 24640b275b6SJohannes Berg if (sta) { 2479446f3efSJohannes Berg sprintf(buf, "../../netdev:%s/stations/%pM", 2489446f3efSJohannes Berg sta->sdata->name, sta->sta.addr); 2493b96766fSJohannes Berg key->debugfs.stalink = 2503b96766fSJohannes Berg debugfs_create_symlink("station", key->debugfs.dir, buf); 25140b275b6SJohannes Berg } 2523b96766fSJohannes Berg 253e9f207f0SJiri Benc DEBUGFS_ADD(keylen); 2548f20fc24SJohannes Berg DEBUGFS_ADD(flags); 255e9f207f0SJiri Benc DEBUGFS_ADD(keyidx); 256e9f207f0SJiri Benc DEBUGFS_ADD(hw_key_idx); 257e9f207f0SJiri Benc DEBUGFS_ADD(tx_rx_count); 258e9f207f0SJiri Benc DEBUGFS_ADD(algorithm); 259e9f207f0SJiri Benc DEBUGFS_ADD(tx_spec); 260e9f207f0SJiri Benc DEBUGFS_ADD(rx_spec); 261e9f207f0SJiri Benc DEBUGFS_ADD(replays); 2623cfcf6acSJouni Malinen DEBUGFS_ADD(icverrors); 263e9f207f0SJiri Benc DEBUGFS_ADD(key); 264e7a64f12SJohannes Berg DEBUGFS_ADD(ifindex); 265e9f207f0SJiri Benc }; 266e9f207f0SJiri Benc 267e9f207f0SJiri Benc void ieee80211_debugfs_key_remove(struct ieee80211_key *key) 268e9f207f0SJiri Benc { 269e9f207f0SJiri Benc if (!key) 270e9f207f0SJiri Benc return; 271e9f207f0SJiri Benc 2727bcfaf2fSJohannes Berg debugfs_remove_recursive(key->debugfs.dir); 273e9f207f0SJiri Benc key->debugfs.dir = NULL; 274e9f207f0SJiri Benc } 275f7e0104cSJohannes Berg 276f7e0104cSJohannes Berg void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata) 277e9f207f0SJiri Benc { 278e9f207f0SJiri Benc char buf[50]; 27978520cadSJohannes Berg struct ieee80211_key *key; 280e9f207f0SJiri Benc 2817bcfaf2fSJohannes Berg if (!sdata->debugfs.dir) 282e9f207f0SJiri Benc return; 283e9f207f0SJiri Benc 284f7e0104cSJohannes Berg lockdep_assert_held(&sdata->local->key_mtx); 28578520cadSJohannes Berg 286135792ecSJohannes Berg if (sdata->debugfs.default_unicast_key) { 287135792ecSJohannes Berg debugfs_remove(sdata->debugfs.default_unicast_key); 288135792ecSJohannes Berg sdata->debugfs.default_unicast_key = NULL; 289135792ecSJohannes Berg } 290135792ecSJohannes Berg 291f7e0104cSJohannes Berg if (sdata->default_unicast_key) { 29240b275b6SJohannes Berg key = key_mtx_dereference(sdata->local, 29340b275b6SJohannes Berg sdata->default_unicast_key); 29478520cadSJohannes Berg sprintf(buf, "../keys/%d", key->debugfs.cnt); 295f7e0104cSJohannes Berg sdata->debugfs.default_unicast_key = 296f7e0104cSJohannes Berg debugfs_create_symlink("default_unicast_key", 2977bcfaf2fSJohannes Berg sdata->debugfs.dir, buf); 298135792ecSJohannes Berg } 299135792ecSJohannes Berg 300135792ecSJohannes Berg if (sdata->debugfs.default_multicast_key) { 301135792ecSJohannes Berg debugfs_remove(sdata->debugfs.default_multicast_key); 302135792ecSJohannes Berg sdata->debugfs.default_multicast_key = NULL; 303e9f207f0SJiri Benc } 30478520cadSJohannes Berg 305f7e0104cSJohannes Berg if (sdata->default_multicast_key) { 30640b275b6SJohannes Berg key = key_mtx_dereference(sdata->local, 30740b275b6SJohannes Berg sdata->default_multicast_key); 308f7e0104cSJohannes Berg sprintf(buf, "../keys/%d", key->debugfs.cnt); 309f7e0104cSJohannes Berg sdata->debugfs.default_multicast_key = 310f7e0104cSJohannes Berg debugfs_create_symlink("default_multicast_key", 311f7e0104cSJohannes Berg sdata->debugfs.dir, buf); 312f7e0104cSJohannes Berg } 313e9f207f0SJiri Benc } 314e9f207f0SJiri Benc 3153cfcf6acSJouni Malinen void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata) 3163cfcf6acSJouni Malinen { 3173cfcf6acSJouni Malinen char buf[50]; 3183cfcf6acSJouni Malinen struct ieee80211_key *key; 3193cfcf6acSJouni Malinen 3207bcfaf2fSJohannes Berg if (!sdata->debugfs.dir) 3213cfcf6acSJouni Malinen return; 3223cfcf6acSJouni Malinen 32340b275b6SJohannes Berg key = key_mtx_dereference(sdata->local, 32440b275b6SJohannes Berg sdata->default_mgmt_key); 3253cfcf6acSJouni Malinen if (key) { 3263cfcf6acSJouni Malinen sprintf(buf, "../keys/%d", key->debugfs.cnt); 3277bcfaf2fSJohannes Berg sdata->debugfs.default_mgmt_key = 3283cfcf6acSJouni Malinen debugfs_create_symlink("default_mgmt_key", 3297bcfaf2fSJohannes Berg sdata->debugfs.dir, buf); 3303cfcf6acSJouni Malinen } else 3313cfcf6acSJouni Malinen ieee80211_debugfs_key_remove_mgmt_default(sdata); 3323cfcf6acSJouni Malinen } 3333cfcf6acSJouni Malinen 3343cfcf6acSJouni Malinen void ieee80211_debugfs_key_remove_mgmt_default(struct ieee80211_sub_if_data *sdata) 3353cfcf6acSJouni Malinen { 3363cfcf6acSJouni Malinen if (!sdata) 3373cfcf6acSJouni Malinen return; 3383cfcf6acSJouni Malinen 3397bcfaf2fSJohannes Berg debugfs_remove(sdata->debugfs.default_mgmt_key); 3407bcfaf2fSJohannes Berg sdata->debugfs.default_mgmt_key = NULL; 3413cfcf6acSJouni Malinen } 3423cfcf6acSJouni Malinen 343e9f207f0SJiri Benc void ieee80211_debugfs_key_sta_del(struct ieee80211_key *key, 344e9f207f0SJiri Benc struct sta_info *sta) 345e9f207f0SJiri Benc { 346e9f207f0SJiri Benc debugfs_remove(key->debugfs.stalink); 347e9f207f0SJiri Benc key->debugfs.stalink = NULL; 348e9f207f0SJiri Benc } 349