Merge tag 'char-misc-5.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[linux-2.6-microblaze.git] / drivers / staging / vt6656 / key.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * File: key.c
7  *
8  * Purpose: Implement functions for 802.11i Key management
9  *
10  * Author: Jerry Chen
11  *
12  * Date: May 29, 2003
13  *
14  * Functions:
15  *
16  * Revision History:
17  *
18  */
19
20 #include "mac.h"
21 #include "key.h"
22 #include "usbpipe.h"
23
24 int vnt_key_init_table(struct vnt_private *priv)
25 {
26         u8 i;
27         u8 data[MAX_KEY_TABLE];
28
29         for (i = 0; i < MAX_KEY_TABLE; i++)
30                 data[i] = i;
31
32         return vnt_control_out(priv, MESSAGE_TYPE_CLRKEYENTRY,
33                         0, 0, ARRAY_SIZE(data), data);
34 }
35
36 static int vnt_set_keymode(struct ieee80211_hw *hw, u8 *mac_addr,
37                            struct ieee80211_key_conf *key, u32 key_type,
38                            u32 mode)
39 {
40         struct vnt_private *priv = hw->priv;
41         u8 broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
42         u16 key_mode = 0;
43         u32 entry = 0;
44         u8 *bssid;
45         u8 key_inx = key->keyidx;
46         u8 i;
47
48         if (mac_addr)
49                 bssid = mac_addr;
50         else
51                 bssid = &broadcast[0];
52
53         if (key_type != VNT_KEY_DEFAULTKEY) {
54                 for (i = 0; i < (MAX_KEY_TABLE - 1); i++) {
55                         if (!test_bit(i, &priv->key_entry_inuse)) {
56                                 set_bit(i, &priv->key_entry_inuse);
57
58                                 key->hw_key_idx = i;
59                                 entry = key->hw_key_idx;
60                                 break;
61                         }
62                 }
63         }
64
65         switch (key_type) {
66         case VNT_KEY_DEFAULTKEY:
67                 /* default key last entry */
68                 entry = MAX_KEY_TABLE - 1;
69                 key->hw_key_idx = entry;
70                 fallthrough;
71         case VNT_KEY_GROUP_ADDRESS:
72                 key_mode = mode | (mode << 4);
73                 break;
74         case VNT_KEY_GROUP:
75                 key_mode = mode << 4;
76                 break;
77         case  VNT_KEY_PAIRWISE:
78                 key_mode |= mode;
79                 key_inx = 4;
80                 break;
81         default:
82                 return -EINVAL;
83         }
84
85         key_mode |= key_type;
86
87         if (mode == KEY_CTL_WEP) {
88                 if (key->keylen == WLAN_KEY_LEN_WEP40)
89                         key->key[15] &= 0x7f;
90                 if (key->keylen == WLAN_KEY_LEN_WEP104)
91                         key->key[15] |= 0x80;
92         }
93
94         return vnt_mac_set_keyentry(priv, key_mode, entry,
95                                     key_inx, bssid, key->key);
96 }
97
98 int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
99                  struct ieee80211_vif *vif, struct ieee80211_key_conf *key)
100 {
101         struct vnt_private *priv = hw->priv;
102         u8 *mac_addr = NULL;
103         u8 key_dec_mode = 0;
104
105         if (sta)
106                 mac_addr = &sta->addr[0];
107
108         switch (key->cipher) {
109         case WLAN_CIPHER_SUITE_WEP40:
110         case WLAN_CIPHER_SUITE_WEP104:
111                 vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
112                                 KEY_CTL_WEP);
113
114                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
115
116                 return vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
117                                        KEY_CTL_WEP);
118
119         case WLAN_CIPHER_SUITE_TKIP:
120                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
121                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
122
123                 key_dec_mode = KEY_CTL_TKIP;
124
125                 break;
126         case WLAN_CIPHER_SUITE_CCMP:
127                 if (priv->local_id <= MAC_REVISION_A1)
128                         return -EOPNOTSUPP;
129
130                 key_dec_mode = KEY_CTL_CCMP;
131
132                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
133                 break;
134         default:
135                 return -EOPNOTSUPP;
136         }
137
138         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
139                 return vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE,
140                                        key_dec_mode);
141
142         return vnt_set_keymode(hw, mac_addr, key,
143                                 VNT_KEY_GROUP_ADDRESS, key_dec_mode);
144 }