linux/net/mac80211/debugfs_key.c
<<
>>
Prefs
   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, format_string)                             \
  19static ssize_t key_##name##_read(struct file *file,                     \
  20                                 char __user *userbuf,                  \
  21                                 size_t count, loff_t *ppos)            \
  22{                                                                       \
  23        struct ieee80211_key *key = file->private_data;                 \
  24        return mac80211_format_buffer(userbuf, count, ppos,             \
  25                                      format_string, key->prop);        \
  26}
  27#define KEY_READ_D(name) KEY_READ(name, name, "%d\n")
  28#define KEY_READ_X(name) KEY_READ(name, name, "0x%x\n")
  29
  30#define KEY_OPS(name)                                                   \
  31static const struct file_operations key_ ##name## _ops = {              \
  32        .read = key_##name##_read,                                      \
  33        .open = simple_open,                                            \
  34        .llseek = generic_file_llseek,                                  \
  35}
  36
  37#define KEY_FILE(name, format)                                          \
  38                 KEY_READ_##format(name)                                \
  39                 KEY_OPS(name)
  40
  41#define KEY_CONF_READ(name, format_string)                              \
  42        KEY_READ(conf_##name, conf.name, format_string)
  43#define KEY_CONF_READ_D(name) KEY_CONF_READ(name, "%d\n")
  44
  45#define KEY_CONF_OPS(name)                                              \
  46static const struct file_operations key_ ##name## _ops = {              \
  47        .read = key_conf_##name##_read,                                 \
  48        .open = simple_open,                                            \
  49        .llseek = generic_file_llseek,                                  \
  50}
  51
  52#define KEY_CONF_FILE(name, format)                                     \
  53                 KEY_CONF_READ_##format(name)                           \
  54                 KEY_CONF_OPS(name)
  55
  56KEY_CONF_FILE(keylen, D);
  57KEY_CONF_FILE(keyidx, D);
  58KEY_CONF_FILE(hw_key_idx, D);
  59KEY_FILE(flags, X);
  60KEY_FILE(tx_rx_count, D);
  61KEY_READ(ifindex, sdata->name, "%s\n");
  62KEY_OPS(ifindex);
  63
  64static ssize_t key_algorithm_read(struct file *file,
  65                                  char __user *userbuf,
  66                                  size_t count, loff_t *ppos)
  67{
  68        char buf[15];
  69        struct ieee80211_key *key = file->private_data;
  70        u32 c = key->conf.cipher;
  71
  72        sprintf(buf, "%.2x-%.2x-%.2x:%d\n",
  73                c >> 24, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
  74        return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  75}
  76KEY_OPS(algorithm);
  77
  78static ssize_t key_tx_spec_read(struct file *file, char __user *userbuf,
  79                                size_t count, loff_t *ppos)
  80{
  81        u64 pn;
  82        char buf[20];
  83        int len;
  84        struct ieee80211_key *key = file->private_data;
  85
  86        switch (key->conf.cipher) {
  87        case WLAN_CIPHER_SUITE_WEP40:
  88        case WLAN_CIPHER_SUITE_WEP104:
  89                len = scnprintf(buf, sizeof(buf), "\n");
  90                break;
  91        case WLAN_CIPHER_SUITE_TKIP:
  92                len = scnprintf(buf, sizeof(buf), "%08x %04x\n",
  93                                key->u.tkip.tx.iv32,
  94                                key->u.tkip.tx.iv16);
  95                break;
  96        case WLAN_CIPHER_SUITE_CCMP:
  97                pn = atomic64_read(&key->u.ccmp.tx_pn);
  98                len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
  99                                (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
 100                                (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
 101                break;
 102        case WLAN_CIPHER_SUITE_AES_CMAC:
 103                pn = atomic64_read(&key->u.aes_cmac.tx_pn);
 104                len = scnprintf(buf, sizeof(buf), "%02x%02x%02x%02x%02x%02x\n",
 105                                (u8)(pn >> 40), (u8)(pn >> 32), (u8)(pn >> 24),
 106                                (u8)(pn >> 16), (u8)(pn >> 8), (u8)pn);
 107                break;
 108        default:
 109                return 0;
 110        }
 111        return simple_read_from_buffer(userbuf, count, ppos, buf, len);
 112}
 113KEY_OPS(tx_spec);
 114
 115static 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
 123        switch (key->conf.cipher) {
 124        case WLAN_CIPHER_SUITE_WEP40:
 125        case WLAN_CIPHER_SUITE_WEP104:
 126                len = scnprintf(buf, sizeof(buf), "\n");
 127                break;
 128        case WLAN_CIPHER_SUITE_TKIP:
 129                for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
 130                        p += scnprintf(p, sizeof(buf)+buf-p,
 131                                       "%08x %04x\n",
 132                                       key->u.tkip.rx[i].iv32,
 133                                       key->u.tkip.rx[i].iv16);
 134                len = p - buf;
 135                break;
 136        case WLAN_CIPHER_SUITE_CCMP:
 137                for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++) {
 138                        rpn = key->u.ccmp.rx_pn[i];
 139                        p += scnprintf(p, sizeof(buf)+buf-p,
 140                                       "%02x%02x%02x%02x%02x%02x\n",
 141                                       rpn[0], rpn[1], rpn[2],
 142                                       rpn[3], rpn[4], rpn[5]);
 143                }
 144                len = p - buf;
 145                break;
 146        case WLAN_CIPHER_SUITE_AES_CMAC:
 147                rpn = key->u.aes_cmac.rx_pn;
 148                p += scnprintf(p, sizeof(buf)+buf-p,
 149                               "%02x%02x%02x%02x%02x%02x\n",
 150                               rpn[0], rpn[1], rpn[2],
 151                               rpn[3], rpn[4], rpn[5]);
 152                len = p - buf;
 153                break;
 154        default:
 155                return 0;
 156        }
 157        return simple_read_from_buffer(userbuf, count, ppos, buf, len);
 158}
 159KEY_OPS(rx_spec);
 160
 161static ssize_t key_replays_read(struct file *file, char __user *userbuf,
 162                                size_t count, loff_t *ppos)
 163{
 164        struct ieee80211_key *key = file->private_data;
 165        char buf[20];
 166        int len;
 167
 168        switch (key->conf.cipher) {
 169        case WLAN_CIPHER_SUITE_CCMP:
 170                len = scnprintf(buf, sizeof(buf), "%u\n", key->u.ccmp.replays);
 171                break;
 172        case WLAN_CIPHER_SUITE_AES_CMAC:
 173                len = scnprintf(buf, sizeof(buf), "%u\n",
 174                                key->u.aes_cmac.replays);
 175                break;
 176        default:
 177                return 0;
 178        }
 179        return simple_read_from_buffer(userbuf, count, ppos, buf, len);
 180}
 181KEY_OPS(replays);
 182
 183static ssize_t key_icverrors_read(struct file *file, char __user *userbuf,
 184                                  size_t count, loff_t *ppos)
 185{
 186        struct ieee80211_key *key = file->private_data;
 187        char buf[20];
 188        int len;
 189
 190        switch (key->conf.cipher) {
 191        case WLAN_CIPHER_SUITE_AES_CMAC:
 192                len = scnprintf(buf, sizeof(buf), "%u\n",
 193                                key->u.aes_cmac.icverrors);
 194                break;
 195        default:
 196                return 0;
 197        }
 198        return simple_read_from_buffer(userbuf, count, ppos, buf, len);
 199}
 200KEY_OPS(icverrors);
 201
 202static ssize_t key_key_read(struct file *file, char __user *userbuf,
 203                            size_t count, loff_t *ppos)
 204{
 205        struct ieee80211_key *key = file->private_data;
 206        int i, bufsize = 2 * key->conf.keylen + 2;
 207        char *buf = kmalloc(bufsize, GFP_KERNEL);
 208        char *p = buf;
 209        ssize_t res;
 210
 211        if (!buf)
 212                return -ENOMEM;
 213
 214        for (i = 0; i < key->conf.keylen; i++)
 215                p += scnprintf(p, bufsize + buf - p, "%02x", key->conf.key[i]);
 216        p += scnprintf(p, bufsize+buf-p, "\n");
 217        res = simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
 218        kfree(buf);
 219        return res;
 220}
 221KEY_OPS(key);
 222
 223#define DEBUGFS_ADD(name) \
 224        debugfs_create_file(#name, 0400, key->debugfs.dir, \
 225                            key, &key_##name##_ops);
 226
 227void ieee80211_debugfs_key_add(struct ieee80211_key *key)
 228{
 229        static int keycount;
 230        char buf[100];
 231        struct sta_info *sta;
 232
 233        if (!key->local->debugfs.keys)
 234                return;
 235
 236        sprintf(buf, "%d", keycount);
 237        key->debugfs.cnt = keycount;
 238        keycount++;
 239        key->debugfs.dir = debugfs_create_dir(buf,
 240                                        key->local->debugfs.keys);
 241
 242        if (!key->debugfs.dir)
 243                return;
 244
 245        sta = key->sta;
 246        if (sta) {
 247                sprintf(buf, "../../netdev:%s/stations/%pM",
 248                        sta->sdata->name, sta->sta.addr);
 249                key->debugfs.stalink =
 250                        debugfs_create_symlink("station", key->debugfs.dir, buf);
 251        }
 252
 253        DEBUGFS_ADD(keylen);
 254        DEBUGFS_ADD(flags);
 255        DEBUGFS_ADD(keyidx);
 256        DEBUGFS_ADD(hw_key_idx);
 257        DEBUGFS_ADD(tx_rx_count);
 258        DEBUGFS_ADD(algorithm);
 259        DEBUGFS_ADD(tx_spec);
 260        DEBUGFS_ADD(rx_spec);
 261        DEBUGFS_ADD(replays);
 262        DEBUGFS_ADD(icverrors);
 263        DEBUGFS_ADD(key);
 264        DEBUGFS_ADD(ifindex);
 265};
 266
 267void ieee80211_debugfs_key_remove(struct ieee80211_key *key)
 268{
 269        if (!key)
 270                return;
 271
 272        debugfs_remove_recursive(key->debugfs.dir);
 273        key->debugfs.dir = NULL;
 274}
 275
 276void ieee80211_debugfs_key_update_default(struct ieee80211_sub_if_data *sdata)
 277{
 278        char buf[50];
 279        struct ieee80211_key *key;
 280
 281        if (!sdata->debugfs.dir)
 282                return;
 283
 284        lockdep_assert_held(&sdata->local->key_mtx);
 285
 286        if (sdata->debugfs.default_unicast_key) {
 287                debugfs_remove(sdata->debugfs.default_unicast_key);
 288                sdata->debugfs.default_unicast_key = NULL;
 289        }
 290
 291        if (sdata->default_unicast_key) {
 292                key = key_mtx_dereference(sdata->local,
 293                                          sdata->default_unicast_key);
 294                sprintf(buf, "../keys/%d", key->debugfs.cnt);
 295                sdata->debugfs.default_unicast_key =
 296                        debugfs_create_symlink("default_unicast_key",
 297                                               sdata->debugfs.dir, buf);
 298        }
 299
 300        if (sdata->debugfs.default_multicast_key) {
 301                debugfs_remove(sdata->debugfs.default_multicast_key);
 302                sdata->debugfs.default_multicast_key = NULL;
 303        }
 304
 305        if (sdata->default_multicast_key) {
 306                key = key_mtx_dereference(sdata->local,
 307                                          sdata->default_multicast_key);
 308                sprintf(buf, "../keys/%d", key->debugfs.cnt);
 309                sdata->debugfs.default_multicast_key =
 310                        debugfs_create_symlink("default_multicast_key",
 311                                               sdata->debugfs.dir, buf);
 312        }
 313}
 314
 315void ieee80211_debugfs_key_add_mgmt_default(struct ieee80211_sub_if_data *sdata)
 316{
 317        char buf[50];
 318        struct ieee80211_key *key;
 319
 320        if (!sdata->debugfs.dir)
 321                return;
 322
 323        key = key_mtx_dereference(sdata->local,
 324                                  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
 334void 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
 343void 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
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.