Linux-libre 5.7.6-gnu
[librecmc/linux-libre.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, bool onfly_latch)
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                 /* fall through */
71         case VNT_KEY_ALLGROUP:
72                 key_mode |= VNT_KEY_ALLGROUP;
73                 if (onfly_latch)
74                         key_mode |= VNT_KEY_ONFLY_ALL;
75                 /* fall through */
76         case VNT_KEY_GROUP_ADDRESS:
77                 key_mode |= mode;
78                 /* fall through */
79         case VNT_KEY_GROUP:
80                 key_mode |= (mode << 4);
81                 key_mode |= VNT_KEY_GROUP;
82                 break;
83         case  VNT_KEY_PAIRWISE:
84                 key_mode |= mode;
85                 key_inx = 4;
86                 break;
87         default:
88                 return -EINVAL;
89         }
90
91         if (onfly_latch)
92                 key_mode |= VNT_KEY_ONFLY;
93
94         if (mode == KEY_CTL_WEP) {
95                 if (key->keylen == WLAN_KEY_LEN_WEP40)
96                         key->key[15] &= 0x7f;
97                 if (key->keylen == WLAN_KEY_LEN_WEP104)
98                         key->key[15] |= 0x80;
99         }
100
101         vnt_mac_set_keyentry(priv, key_mode, entry, key_inx, bssid, key->key);
102
103         return 0;
104 }
105
106 int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
107                  struct ieee80211_vif *vif, struct ieee80211_key_conf *key)
108 {
109         struct vnt_private *priv = hw->priv;
110         u8 *mac_addr = NULL;
111         u8 key_dec_mode = 0;
112         int ret = 0, u;
113
114         if (sta)
115                 mac_addr = &sta->addr[0];
116
117         switch (key->cipher) {
118         case 0:
119                 for (u = 0 ; u < MAX_KEY_TABLE; u++)
120                         vnt_mac_disable_keyentry(priv, u);
121                 return ret;
122
123         case WLAN_CIPHER_SUITE_WEP40:
124         case WLAN_CIPHER_SUITE_WEP104:
125                 for (u = 0; u < MAX_KEY_TABLE; u++)
126                         vnt_mac_disable_keyentry(priv, u);
127
128                 vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
129                                 KEY_CTL_WEP, true);
130
131                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
132
133                 return ret;
134         case WLAN_CIPHER_SUITE_TKIP:
135                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
136                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
137
138                 key_dec_mode = KEY_CTL_TKIP;
139
140                 break;
141         case WLAN_CIPHER_SUITE_CCMP:
142                 if (priv->local_id <= MAC_REVISION_A1)
143                         return -EOPNOTSUPP;
144
145                 key_dec_mode = KEY_CTL_CCMP;
146
147                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
148                 break;
149         default:
150                 return -EOPNOTSUPP;
151         }
152
153         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
154                 vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE,
155                                 key_dec_mode, true);
156         else
157                 vnt_set_keymode(hw, mac_addr, key, VNT_KEY_GROUP_ADDRESS,
158                                 key_dec_mode, true);
159
160         return 0;
161 }