Forest Bond | 92b9679 | 2009-06-13 07:38:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * |
| 20 | * File: key.c |
| 21 | * |
| 22 | * Purpose: Implement functions for 802.11i Key management |
| 23 | * |
| 24 | * Author: Jerry Chen |
| 25 | * |
| 26 | * Date: May 29, 2003 |
| 27 | * |
| 28 | * Functions: |
Forest Bond | 92b9679 | 2009-06-13 07:38:31 -0400 | [diff] [blame] | 29 | * |
| 30 | * Revision History: |
| 31 | * |
| 32 | */ |
| 33 | |
Malcolm Priestley | 11d404c | 2012-11-24 14:19:34 +0000 | [diff] [blame] | 34 | #include "mac.h" |
Forest Bond | 92b9679 | 2009-06-13 07:38:31 -0400 | [diff] [blame] | 35 | #include "key.h" |
Malcolm Priestley | 62c8526 | 2014-05-26 13:59:07 +0100 | [diff] [blame] | 36 | #include "usbpipe.h" |
Forest Bond | 92b9679 | 2009-06-13 07:38:31 -0400 | [diff] [blame] | 37 | |
Malcolm Priestley | d1eb500 | 2014-06-25 21:14:24 +0100 | [diff] [blame] | 38 | int vnt_key_init_table(struct vnt_private *priv) |
| 39 | { |
| 40 | int ret; |
| 41 | u8 i; |
| 42 | u8 data[MAX_KEY_TABLE]; |
| 43 | |
| 44 | for (i = 0; i < MAX_KEY_TABLE; i++) |
| 45 | data[i] = i; |
| 46 | |
| 47 | ret = vnt_control_out(priv, MESSAGE_TYPE_CLRKEYENTRY, |
| 48 | 0, 0, ARRAY_SIZE(data), data); |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | static int vnt_set_keymode(struct ieee80211_hw *hw, u8 *mac_addr, |
| 54 | struct ieee80211_key_conf *key, u32 key_type, u32 mode, |
| 55 | bool onfly_latch) |
| 56 | { |
| 57 | struct vnt_private *priv = hw->priv; |
| 58 | u8 broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 59 | u16 key_mode = 0; |
| 60 | u32 entry = 0; |
| 61 | u8 *bssid; |
| 62 | u8 key_inx = key->keyidx; |
| 63 | u8 i; |
| 64 | |
| 65 | if (mac_addr) |
| 66 | bssid = mac_addr; |
| 67 | else |
| 68 | bssid = &broadcast[0]; |
| 69 | |
| 70 | if (key_type != VNT_KEY_DEFAULTKEY) { |
| 71 | for (i = 0; i < (MAX_KEY_TABLE - 1); i++) { |
| 72 | if (!test_bit(i, &priv->key_entry_inuse)) { |
| 73 | set_bit(i, &priv->key_entry_inuse); |
| 74 | |
| 75 | key->hw_key_idx = i; |
| 76 | entry = key->hw_key_idx; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | switch (key_type) { |
| 83 | /* fallthrough */ |
| 84 | case VNT_KEY_DEFAULTKEY: |
| 85 | /* default key last entry */ |
| 86 | entry = MAX_KEY_TABLE - 1; |
| 87 | key->hw_key_idx = entry; |
| 88 | case VNT_KEY_ALLGROUP: |
| 89 | key_mode |= VNT_KEY_ALLGROUP; |
| 90 | if (onfly_latch) |
| 91 | key_mode |= VNT_KEY_ONFLY_ALL; |
| 92 | case VNT_KEY_GROUP_ADDRESS: |
| 93 | key_mode |= mode; |
| 94 | case VNT_KEY_GROUP: |
| 95 | key_mode |= (mode << 4); |
| 96 | key_mode |= VNT_KEY_GROUP; |
| 97 | break; |
| 98 | case VNT_KEY_PAIRWISE: |
| 99 | key_mode |= mode; |
| 100 | key_inx = 4; |
Malcolm Priestley | f9ef05c | 2014-07-11 20:02:18 +0100 | [diff] [blame] | 101 | /* Don't save entry for pairwise key for station mode */ |
| 102 | if (priv->op_mode == NL80211_IFTYPE_STATION) |
| 103 | clear_bit(entry, &priv->key_entry_inuse); |
Malcolm Priestley | d1eb500 | 2014-06-25 21:14:24 +0100 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | if (onfly_latch) |
| 110 | key_mode |= VNT_KEY_ONFLY; |
| 111 | |
| 112 | if (mode == KEY_CTL_WEP) { |
| 113 | if (key->keylen == WLAN_KEY_LEN_WEP40) |
| 114 | key->key[15] &= 0x7f; |
| 115 | if (key->keylen == WLAN_KEY_LEN_WEP104) |
| 116 | key->key[15] |= 0x80; |
| 117 | } |
| 118 | |
| 119 | vnt_mac_set_keyentry(priv, key_mode, entry, key_inx, bssid, key->key); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta, |
| 125 | struct ieee80211_vif *vif, struct ieee80211_key_conf *key) |
| 126 | { |
| 127 | struct ieee80211_bss_conf *conf = &vif->bss_conf; |
| 128 | struct vnt_private *priv = hw->priv; |
| 129 | u8 *mac_addr = NULL; |
| 130 | u8 key_dec_mode = 0; |
| 131 | int ret = 0, u; |
| 132 | |
| 133 | if (sta) |
| 134 | mac_addr = &sta->addr[0]; |
| 135 | |
| 136 | switch (key->cipher) { |
| 137 | case 0: |
| 138 | for (u = 0 ; u < MAX_KEY_TABLE; u++) |
| 139 | vnt_mac_disable_keyentry(priv, u); |
| 140 | return ret; |
| 141 | |
| 142 | case WLAN_CIPHER_SUITE_WEP40: |
| 143 | case WLAN_CIPHER_SUITE_WEP104: |
| 144 | for (u = 0; u < MAX_KEY_TABLE; u++) |
| 145 | vnt_mac_disable_keyentry(priv, u); |
| 146 | |
| 147 | vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY, |
| 148 | KEY_CTL_WEP, true); |
| 149 | |
| 150 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; |
| 151 | |
| 152 | return ret; |
| 153 | case WLAN_CIPHER_SUITE_TKIP: |
| 154 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC; |
| 155 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; |
| 156 | |
| 157 | key_dec_mode = KEY_CTL_TKIP; |
| 158 | |
| 159 | break; |
| 160 | case WLAN_CIPHER_SUITE_CCMP: |
Malcolm Priestley | f1945a1 | 2014-07-19 12:30:06 +0100 | [diff] [blame] | 161 | if (priv->local_id <= MAC_REVISION_A1) |
Malcolm Priestley | d1eb500 | 2014-06-25 21:14:24 +0100 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | |
| 164 | key_dec_mode = KEY_CTL_CCMP; |
| 165 | |
| 166 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) { |
| 171 | vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE, |
| 172 | key_dec_mode, true); |
| 173 | } else { |
| 174 | vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY, |
| 175 | key_dec_mode, true); |
| 176 | |
| 177 | vnt_set_keymode(hw, (u8 *)conf->bssid, key, |
| 178 | VNT_KEY_GROUP_ADDRESS, key_dec_mode, true); |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |