linux/drivers/staging/wfx/key.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Key management related functions.
   4 *
   5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
   6 * Copyright (c) 2010, ST-Ericsson
   7 */
   8#include <linux/etherdevice.h>
   9#include <net/mac80211.h>
  10
  11#include "key.h"
  12#include "wfx.h"
  13#include "hif_tx_mib.h"
  14
  15static int wfx_alloc_key(struct wfx_dev *wdev)
  16{
  17        int idx;
  18
  19        idx = ffs(~wdev->key_map) - 1;
  20        if (idx < 0 || idx >= MAX_KEY_ENTRIES)
  21                return -1;
  22
  23        wdev->key_map |= BIT(idx);
  24        return idx;
  25}
  26
  27static void wfx_free_key(struct wfx_dev *wdev, int idx)
  28{
  29        WARN(!(wdev->key_map & BIT(idx)), "inconsistent key allocation");
  30        wdev->key_map &= ~BIT(idx);
  31}
  32
  33static u8 fill_wep_pair(struct wfx_hif_wep_pairwise_key *msg,
  34                        struct ieee80211_key_conf *key, u8 *peer_addr)
  35{
  36        WARN(key->keylen > sizeof(msg->key_data), "inconsistent data");
  37        msg->key_length = key->keylen;
  38        memcpy(msg->key_data, key->key, key->keylen);
  39        ether_addr_copy(msg->peer_address, peer_addr);
  40        return HIF_KEY_TYPE_WEP_PAIRWISE;
  41}
  42
  43static u8 fill_wep_group(struct wfx_hif_wep_group_key *msg,
  44                         struct ieee80211_key_conf *key)
  45{
  46        WARN(key->keylen > sizeof(msg->key_data), "inconsistent data");
  47        msg->key_id = key->keyidx;
  48        msg->key_length = key->keylen;
  49        memcpy(msg->key_data, key->key, key->keylen);
  50        return HIF_KEY_TYPE_WEP_DEFAULT;
  51}
  52
  53static u8 fill_tkip_pair(struct wfx_hif_tkip_pairwise_key *msg,
  54                         struct ieee80211_key_conf *key, u8 *peer_addr)
  55{
  56        u8 *keybuf = key->key;
  57
  58        WARN(key->keylen != sizeof(msg->tkip_key_data) + sizeof(msg->tx_mic_key) +
  59                            sizeof(msg->rx_mic_key), "inconsistent data");
  60        memcpy(msg->tkip_key_data, keybuf, sizeof(msg->tkip_key_data));
  61        keybuf += sizeof(msg->tkip_key_data);
  62        memcpy(msg->tx_mic_key, keybuf, sizeof(msg->tx_mic_key));
  63        keybuf += sizeof(msg->tx_mic_key);
  64        memcpy(msg->rx_mic_key, keybuf, sizeof(msg->rx_mic_key));
  65        ether_addr_copy(msg->peer_address, peer_addr);
  66        return HIF_KEY_TYPE_TKIP_PAIRWISE;
  67}
  68
  69static u8 fill_tkip_group(struct wfx_hif_tkip_group_key *msg, struct ieee80211_key_conf *key,
  70                          struct ieee80211_key_seq *seq, enum nl80211_iftype iftype)
  71{
  72        u8 *keybuf = key->key;
  73
  74        WARN(key->keylen != sizeof(msg->tkip_key_data) + 2 * sizeof(msg->rx_mic_key),
  75             "inconsistent data");
  76        msg->key_id = key->keyidx;
  77        memcpy(msg->rx_sequence_counter, &seq->tkip.iv16, sizeof(seq->tkip.iv16));
  78        memcpy(msg->rx_sequence_counter + sizeof(u16), &seq->tkip.iv32, sizeof(seq->tkip.iv32));
  79        memcpy(msg->tkip_key_data, keybuf, sizeof(msg->tkip_key_data));
  80        keybuf += sizeof(msg->tkip_key_data);
  81        if (iftype == NL80211_IFTYPE_AP)
  82                /* Use Tx MIC Key */
  83                memcpy(msg->rx_mic_key, keybuf + 0, sizeof(msg->rx_mic_key));
  84        else
  85                /* Use Rx MIC Key */
  86                memcpy(msg->rx_mic_key, keybuf + 8, sizeof(msg->rx_mic_key));
  87        return HIF_KEY_TYPE_TKIP_GROUP;
  88}
  89
  90static u8 fill_ccmp_pair(struct wfx_hif_aes_pairwise_key *msg,
  91                         struct ieee80211_key_conf *key, u8 *peer_addr)
  92{
  93        WARN(key->keylen != sizeof(msg->aes_key_data), "inconsistent data");
  94        ether_addr_copy(msg->peer_address, peer_addr);
  95        memcpy(msg->aes_key_data, key->key, key->keylen);
  96        return HIF_KEY_TYPE_AES_PAIRWISE;
  97}
  98
  99static u8 fill_ccmp_group(struct wfx_hif_aes_group_key *msg,
 100                          struct ieee80211_key_conf *key, struct ieee80211_key_seq *seq)
 101{
 102        WARN(key->keylen != sizeof(msg->aes_key_data), "inconsistent data");
 103        memcpy(msg->aes_key_data, key->key, key->keylen);
 104        memcpy(msg->rx_sequence_counter, seq->ccmp.pn, sizeof(seq->ccmp.pn));
 105        memreverse(msg->rx_sequence_counter, sizeof(seq->ccmp.pn));
 106        msg->key_id = key->keyidx;
 107        return HIF_KEY_TYPE_AES_GROUP;
 108}
 109
 110static u8 fill_sms4_pair(struct wfx_hif_wapi_pairwise_key *msg,
 111                         struct ieee80211_key_conf *key, u8 *peer_addr)
 112{
 113        u8 *keybuf = key->key;
 114
 115        WARN(key->keylen != sizeof(msg->wapi_key_data) + sizeof(msg->mic_key_data),
 116             "inconsistent data");
 117        ether_addr_copy(msg->peer_address, peer_addr);
 118        memcpy(msg->wapi_key_data, keybuf, sizeof(msg->wapi_key_data));
 119        keybuf += sizeof(msg->wapi_key_data);
 120        memcpy(msg->mic_key_data, keybuf, sizeof(msg->mic_key_data));
 121        msg->key_id = key->keyidx;
 122        return HIF_KEY_TYPE_WAPI_PAIRWISE;
 123}
 124
 125static u8 fill_sms4_group(struct wfx_hif_wapi_group_key *msg,
 126                          struct ieee80211_key_conf *key)
 127{
 128        u8 *keybuf = key->key;
 129
 130        WARN(key->keylen != sizeof(msg->wapi_key_data) + sizeof(msg->mic_key_data),
 131             "inconsistent data");
 132        memcpy(msg->wapi_key_data, keybuf, sizeof(msg->wapi_key_data));
 133        keybuf += sizeof(msg->wapi_key_data);
 134        memcpy(msg->mic_key_data, keybuf, sizeof(msg->mic_key_data));
 135        msg->key_id = key->keyidx;
 136        return HIF_KEY_TYPE_WAPI_GROUP;
 137}
 138
 139static u8 fill_aes_cmac_group(struct wfx_hif_igtk_group_key *msg,
 140                              struct ieee80211_key_conf *key, struct ieee80211_key_seq *seq)
 141{
 142        WARN(key->keylen != sizeof(msg->igtk_key_data), "inconsistent data");
 143        memcpy(msg->igtk_key_data, key->key, key->keylen);
 144        memcpy(msg->ipn, seq->aes_cmac.pn, sizeof(seq->aes_cmac.pn));
 145        memreverse(msg->ipn, sizeof(seq->aes_cmac.pn));
 146        msg->key_id = key->keyidx;
 147        return HIF_KEY_TYPE_IGTK_GROUP;
 148}
 149
 150static int wfx_add_key(struct wfx_vif *wvif, struct ieee80211_sta *sta,
 151                       struct ieee80211_key_conf *key)
 152{
 153        int ret;
 154        struct wfx_hif_req_add_key k = { };
 155        struct ieee80211_key_seq seq;
 156        struct wfx_dev *wdev = wvif->wdev;
 157        int idx = wfx_alloc_key(wvif->wdev);
 158        bool pairwise = key->flags & IEEE80211_KEY_FLAG_PAIRWISE;
 159
 160        WARN(key->flags & IEEE80211_KEY_FLAG_PAIRWISE && !sta, "inconsistent data");
 161        ieee80211_get_key_rx_seq(key, 0, &seq);
 162        if (idx < 0)
 163                return -EINVAL;
 164        k.int_id = wvif->id;
 165        k.entry_index = idx;
 166        if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
 167            key->cipher == WLAN_CIPHER_SUITE_WEP104) {
 168                if (pairwise)
 169                        k.type = fill_wep_pair(&k.key.wep_pairwise_key, key, sta->addr);
 170                else
 171                        k.type = fill_wep_group(&k.key.wep_group_key, key);
 172        } else if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
 173                if (pairwise)
 174                        k.type = fill_tkip_pair(&k.key.tkip_pairwise_key, key, sta->addr);
 175                else
 176                        k.type = fill_tkip_group(&k.key.tkip_group_key, key, &seq,
 177                                                 wvif->vif->type);
 178        } else if (key->cipher == WLAN_CIPHER_SUITE_CCMP) {
 179                if (pairwise)
 180                        k.type = fill_ccmp_pair(&k.key.aes_pairwise_key, key, sta->addr);
 181                else
 182                        k.type = fill_ccmp_group(&k.key.aes_group_key, key, &seq);
 183        } else if (key->cipher == WLAN_CIPHER_SUITE_SMS4) {
 184                if (pairwise)
 185                        k.type = fill_sms4_pair(&k.key.wapi_pairwise_key, key, sta->addr);
 186                else
 187                        k.type = fill_sms4_group(&k.key.wapi_group_key, key);
 188        } else if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
 189                k.type = fill_aes_cmac_group(&k.key.igtk_group_key, key, &seq);
 190                key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
 191        } else {
 192                dev_warn(wdev->dev, "unsupported key type %d\n", key->cipher);
 193                wfx_free_key(wdev, idx);
 194                return -EOPNOTSUPP;
 195        }
 196        ret = wfx_hif_add_key(wdev, &k);
 197        if (ret) {
 198                wfx_free_key(wdev, idx);
 199                return -EOPNOTSUPP;
 200        }
 201        key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE | IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
 202        key->hw_key_idx = idx;
 203        return 0;
 204}
 205
 206static int wfx_remove_key(struct wfx_vif *wvif, struct ieee80211_key_conf *key)
 207{
 208        WARN(key->hw_key_idx >= MAX_KEY_ENTRIES, "corrupted hw_key_idx");
 209        wfx_free_key(wvif->wdev, key->hw_key_idx);
 210        return wfx_hif_remove_key(wvif->wdev, key->hw_key_idx);
 211}
 212
 213int wfx_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, struct ieee80211_vif *vif,
 214                struct ieee80211_sta *sta, struct ieee80211_key_conf *key)
 215{
 216        int ret = -EOPNOTSUPP;
 217        struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
 218
 219        mutex_lock(&wvif->wdev->conf_mutex);
 220        if (cmd == SET_KEY)
 221                ret = wfx_add_key(wvif, sta, key);
 222        if (cmd == DISABLE_KEY)
 223                ret = wfx_remove_key(wvif, key);
 224        mutex_unlock(&wvif->wdev->conf_mutex);
 225        return ret;
 226}
 227
 228