]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/key.c
mac80211: list features in WEP/TKIP disable in better order
[mirror_ubuntu-jammy-kernel.git] / net / mac80211 / key.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1f5a7e47
JB
2/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
3b96766f 6 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
d98ad83e 7 * Copyright 2013-2014 Intel Mobile Communications GmbH
fdf7cb41 8 * Copyright 2015-2017 Intel Deutschland GmbH
1f5a7e47
JB
9 */
10
11a843b7
JB
11#include <linux/if_ether.h>
12#include <linux/etherdevice.h>
13#include <linux/list.h>
d4e46a3d 14#include <linux/rcupdate.h>
db4d1169 15#include <linux/rtnetlink.h>
5a0e3ad6 16#include <linux/slab.h>
bc3b2d7f 17#include <linux/export.h>
1f5a7e47 18#include <net/mac80211.h>
2bdd713b 19#include <crypto/algapi.h>
d26ad377 20#include <asm/unaligned.h>
1f5a7e47 21#include "ieee80211_i.h"
24487981 22#include "driver-ops.h"
1f5a7e47
JB
23#include "debugfs_key.h"
24#include "aes_ccm.h"
3cfcf6ac 25#include "aes_cmac.h"
8ade538b 26#include "aes_gmac.h"
00b9cfa3 27#include "aes_gcm.h"
1f5a7e47 28
11a843b7 29
dbbea671
JB
30/**
31 * DOC: Key handling basics
11a843b7
JB
32 *
33 * Key handling in mac80211 is done based on per-interface (sub_if_data)
34 * keys and per-station keys. Since each station belongs to an interface,
35 * each station key also belongs to that interface.
36 *
b5c34f66
JB
37 * Hardware acceleration is done on a best-effort basis for algorithms
38 * that are implemented in software, for each key the hardware is asked
39 * to enable that key for offloading but if it cannot do that the key is
40 * simply kept for software encryption (unless it is for an algorithm
41 * that isn't implemented in software).
42 * There is currently no way of knowing whether a key is handled in SW
43 * or HW except by looking into debugfs.
11a843b7 44 *
b5c34f66
JB
45 * All key management is internally protected by a mutex. Within all
46 * other parts of mac80211, key references are, just as STA structure
47 * references, protected by RCU. Note, however, that some things are
48 * unprotected, namely the key->sta dereferences within the hardware
49 * acceleration functions. This means that sta_info_destroy() must
50 * remove the key which waits for an RCU grace period.
11a843b7
JB
51 */
52
53static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
11a843b7 54
ad0e2b5a 55static void assert_key_lock(struct ieee80211_local *local)
3b96766f 56{
46a5ebaf 57 lockdep_assert_held(&local->key_mtx);
3b96766f
JB
58}
59
f9dca80b
MK
60static void
61update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
62{
63 struct ieee80211_sub_if_data *vlan;
64
65 if (sdata->vif.type != NL80211_IFTYPE_AP)
66 return;
67
51f458d9
JB
68 /* crypto_tx_tailroom_needed_cnt is protected by this */
69 assert_key_lock(sdata->local);
f9dca80b 70
51f458d9
JB
71 rcu_read_lock();
72
73 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
f9dca80b
MK
74 vlan->crypto_tx_tailroom_needed_cnt += delta;
75
51f458d9 76 rcu_read_unlock();
f9dca80b
MK
77}
78
3bff1865
YAP
79static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
80{
81 /*
82 * When this count is zero, SKB resizing for allocating tailroom
83 * for IV or MMIC is skipped. But, this check has created two race
84 * cases in xmit path while transiting from zero count to one:
85 *
86 * 1. SKB resize was skipped because no key was added but just before
87 * the xmit key is added and SW encryption kicks off.
88 *
89 * 2. SKB resize was skipped because all the keys were hw planted but
90 * just before xmit one of the key is deleted and SW encryption kicks
91 * off.
92 *
93 * In both the above case SW encryption will find not enough space for
94 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
95 *
96 * Solution has been explained at
97 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
98 */
99
51f458d9
JB
100 assert_key_lock(sdata->local);
101
f9dca80b
MK
102 update_vlan_tailroom_need_count(sdata, 1);
103
3bff1865
YAP
104 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
105 /*
106 * Flush all XMIT packets currently using HW encryption or no
107 * encryption at all if the count transition is from 0 -> 1.
108 */
109 synchronize_net();
110 }
111}
112
f9dca80b
MK
113static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
114 int delta)
115{
51f458d9
JB
116 assert_key_lock(sdata->local);
117
f9dca80b
MK
118 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
119
120 update_vlan_tailroom_need_count(sdata, -delta);
121 sdata->crypto_tx_tailroom_needed_cnt -= delta;
122}
123
3ffc2a90 124static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
11a843b7 125{
db3bdcb9 126 struct ieee80211_sub_if_data *sdata = key->sdata;
89c91cae 127 struct sta_info *sta;
fa7e1fbc 128 int ret = -EOPNOTSUPP;
11a843b7 129
3b96766f
JB
130 might_sleep();
131
4619194a
JB
132 if (key->flags & KEY_FLAG_TAINTED) {
133 /* If we get here, it's during resume and the key is
134 * tainted so shouldn't be used/programmed any more.
135 * However, its flags may still indicate that it was
136 * programmed into the device (since we're in resume)
137 * so clear that flag now to avoid trying to remove
138 * it again later.
139 */
092c4098
AW
140 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
141 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
142 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
143 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
144 increment_tailroom_need_count(sdata);
145
4619194a 146 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
27b3eb9c 147 return -EINVAL;
4619194a 148 }
27b3eb9c 149
e31b8213 150 if (!key->local->ops->set_key)
3ffc2a90 151 goto out_unsupported;
11a843b7 152
ad0e2b5a
JB
153 assert_key_lock(key->local);
154
89c91cae 155 sta = key->sta;
dc822b5d 156
e31b8213
JB
157 /*
158 * If this is a per-STA GTK, check if it
159 * is supported; if not, return.
160 */
161 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
30686bf7 162 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
e31b8213
JB
163 goto out_unsupported;
164
89c91cae
JB
165 if (sta && !sta->uploaded)
166 goto out_unsupported;
167
18890d4b
HS
168 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
169 /*
170 * The driver doesn't know anything about VLAN interfaces.
171 * Hence, don't send GTKs for VLAN interfaces to the driver.
172 */
78ad2341
AW
173 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
174 ret = 1;
18890d4b 175 goto out_unsupported;
78ad2341 176 }
18890d4b 177 }
11a843b7 178
89c91cae
JB
179 ret = drv_set_key(key->local, SET_KEY, sdata,
180 sta ? &sta->sta : NULL, &key->conf);
11a843b7 181
e31b8213 182 if (!ret) {
11a843b7 183 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
3bff1865 184
092c4098
AW
185 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
186 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
187 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
f9dca80b 188 decrease_tailroom_need_count(sdata, 1);
3bff1865 189
077a9154
AN
190 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
191 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
192
9de18d81
DS
193 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
194 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
195
e31b8213
JB
196 return 0;
197 }
11a843b7 198
fa7e1fbc 199 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
bdcbd8e0 200 sdata_err(sdata,
0fb9a9ec 201 "failed to set key (%d, %pM) to hardware (%d)\n",
89c91cae
JB
202 key->conf.keyidx,
203 sta ? sta->sta.addr : bcast_addr, ret);
3ffc2a90 204
e31b8213
JB
205 out_unsupported:
206 switch (key->conf.cipher) {
207 case WLAN_CIPHER_SUITE_WEP40:
208 case WLAN_CIPHER_SUITE_WEP104:
209 case WLAN_CIPHER_SUITE_TKIP:
210 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db 211 case WLAN_CIPHER_SUITE_CCMP_256:
e31b8213 212 case WLAN_CIPHER_SUITE_AES_CMAC:
56c52da2 213 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
8ade538b
JM
214 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
215 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
00b9cfa3
JM
216 case WLAN_CIPHER_SUITE_GCMP:
217 case WLAN_CIPHER_SUITE_GCMP_256:
fa7e1fbc
JB
218 /* all of these we can do in software - if driver can */
219 if (ret == 1)
220 return 0;
78ad2341 221 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
fa7e1fbc 222 return -EINVAL;
e31b8213
JB
223 return 0;
224 default:
225 return -EINVAL;
3ffc2a90 226 }
11a843b7
JB
227}
228
229static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
230{
dc822b5d 231 struct ieee80211_sub_if_data *sdata;
89c91cae 232 struct sta_info *sta;
11a843b7
JB
233 int ret;
234
3b96766f
JB
235 might_sleep();
236
db4d1169 237 if (!key || !key->local->ops->set_key)
11a843b7
JB
238 return;
239
ad0e2b5a
JB
240 assert_key_lock(key->local);
241
242 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
11a843b7
JB
243 return;
244
89c91cae 245 sta = key->sta;
dc822b5d
JB
246 sdata = key->sdata;
247
092c4098
AW
248 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
249 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
250 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
3bff1865
YAP
251 increment_tailroom_need_count(sdata);
252
62872a9b 253 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
12375ef9 254 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
89c91cae 255 sta ? &sta->sta : NULL, &key->conf);
11a843b7
JB
256
257 if (ret)
bdcbd8e0 258 sdata_err(sdata,
0fb9a9ec 259 "failed to remove key (%d, %pM) from hardware (%d)\n",
89c91cae
JB
260 key->conf.keyidx,
261 sta ? sta->sta.addr : bcast_addr, ret);
62872a9b 262}
11a843b7 263
96fc6efb
AW
264int ieee80211_set_tx_key(struct ieee80211_key *key)
265{
266 struct sta_info *sta = key->sta;
267 struct ieee80211_local *local = key->local;
96fc6efb
AW
268
269 assert_key_lock(local);
270
96fc6efb 271 sta->ptk_idx = key->conf.keyidx;
90cc4bd6 272
dc3998ec
AW
273 if (!ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT))
274 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
96fc6efb
AW
275 ieee80211_check_fast_xmit(sta);
276
277 return 0;
278}
279
90cc4bd6
AW
280static void ieee80211_pairwise_rekey(struct ieee80211_key *old,
281 struct ieee80211_key *new)
62872a9b 282{
90cc4bd6
AW
283 struct ieee80211_local *local = new->local;
284 struct sta_info *sta = new->sta;
285 int i;
62872a9b 286
90cc4bd6 287 assert_key_lock(local);
62872a9b 288
90cc4bd6
AW
289 if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) {
290 /* Extended Key ID key install, initial one or rekey */
291
dc3998ec
AW
292 if (sta->ptk_idx != INVALID_PTK_KEYIDX &&
293 !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) {
90cc4bd6
AW
294 /* Aggregation Sessions with Extended Key ID must not
295 * mix MPDUs with different keyIDs within one A-MPDU.
3e47bf1c
AW
296 * Tear down running Tx aggregation sessions and block
297 * new Rx/Tx aggregation requests during rekey to
dc3998ec
AW
298 * ensure there are no A-MPDUs when the driver is not
299 * supporting A-MPDU key borders. (Blocking Tx only
300 * would be sufficient but WLAN_STA_BLOCK_BA gets the
301 * job done for the few ms we need it.)
90cc4bd6
AW
302 */
303 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
304 mutex_lock(&sta->ampdu_mlme.mtx);
305 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
306 ___ieee80211_stop_tx_ba_session(sta, i,
307 AGG_STOP_LOCAL_REQUEST);
308 mutex_unlock(&sta->ampdu_mlme.mtx);
309 }
310 } else if (old) {
311 /* Rekey without Extended Key ID.
312 * Aggregation sessions are OK when running on SW crypto.
313 * A broken remote STA may cause issues not observed with HW
314 * crypto, though.
315 */
316 if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
317 return;
62872a9b 318
90cc4bd6
AW
319 /* Stop Tx till we are on the new key */
320 old->flags |= KEY_FLAG_TAINTED;
62872a9b 321 ieee80211_clear_fast_xmit(sta);
62872a9b
AW
322 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
323 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
324 ieee80211_sta_tear_down_BA_sessions(sta,
325 AGG_STOP_LOCAL_REQUEST);
326 }
62872a9b
AW
327 if (!wiphy_ext_feature_isset(local->hw.wiphy,
328 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
329 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
330 sta->sta.addr);
331 /* Flushing the driver queues *may* help prevent
332 * the clear text leaks and freezes.
333 */
90cc4bd6 334 ieee80211_flush_queues(local, old->sdata, false);
62872a9b
AW
335 }
336 }
3b96766f
JB
337}
338
339static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
f7e0104c 340 int idx, bool uni, bool multi)
3b96766f
JB
341{
342 struct ieee80211_key *key = NULL;
343
ad0e2b5a
JB
344 assert_key_lock(sdata->local);
345
3b96766f 346 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
40b275b6 347 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
3b96766f 348
de5fad81 349 if (uni) {
f7e0104c 350 rcu_assign_pointer(sdata->default_unicast_key, key);
17c18bf8 351 ieee80211_check_fast_xmit_iface(sdata);
ec4efc4a
JB
352 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
353 drv_set_default_unicast_key(sdata->local, sdata, idx);
de5fad81
YD
354 }
355
f7e0104c
JB
356 if (multi)
357 rcu_assign_pointer(sdata->default_multicast_key, key);
3b96766f 358
f7e0104c 359 ieee80211_debugfs_key_update_default(sdata);
3b96766f
JB
360}
361
f7e0104c
JB
362void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
363 bool uni, bool multi)
3b96766f 364{
ad0e2b5a 365 mutex_lock(&sdata->local->key_mtx);
f7e0104c 366 __ieee80211_set_default_key(sdata, idx, uni, multi);
ad0e2b5a 367 mutex_unlock(&sdata->local->key_mtx);
3b96766f
JB
368}
369
3cfcf6ac
JM
370static void
371__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
372{
373 struct ieee80211_key *key = NULL;
374
ad0e2b5a
JB
375 assert_key_lock(sdata->local);
376
3cfcf6ac
JM
377 if (idx >= NUM_DEFAULT_KEYS &&
378 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
40b275b6 379 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
3cfcf6ac
JM
380
381 rcu_assign_pointer(sdata->default_mgmt_key, key);
382
f7e0104c 383 ieee80211_debugfs_key_update_default(sdata);
3cfcf6ac
JM
384}
385
386void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
387 int idx)
388{
ad0e2b5a 389 mutex_lock(&sdata->local->key_mtx);
3cfcf6ac 390 __ieee80211_set_default_mgmt_key(sdata, idx);
ad0e2b5a 391 mutex_unlock(&sdata->local->key_mtx);
3cfcf6ac
JM
392}
393
62872a9b 394static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
3b8d9c29
JB
395 struct sta_info *sta,
396 bool pairwise,
397 struct ieee80211_key *old,
398 struct ieee80211_key *new)
3b96766f 399{
f7e0104c 400 int idx;
90cc4bd6 401 int ret = 0;
f7e0104c 402 bool defunikey, defmultikey, defmgmtkey;
3b96766f 403
5282c3ba
JB
404 /* caller must provide at least one old/new */
405 if (WARN_ON(!new && !old))
62872a9b 406 return 0;
5282c3ba 407
3b96766f 408 if (new)
ef044763 409 list_add_tail_rcu(&new->list, &sdata->key_list);
3b96766f 410
2475b1cc 411 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
3b96766f 412
90cc4bd6
AW
413 if (new && sta && pairwise) {
414 /* Unicast rekey needs special handling. With Extended Key ID
415 * old is still NULL for the first rekey.
416 */
417 ieee80211_pairwise_rekey(old, new);
418 }
419
62872a9b 420 if (old) {
2475b1cc 421 idx = old->conf.keyidx;
90cc4bd6
AW
422
423 if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
424 ieee80211_key_disable_hw_accel(old);
425
426 if (new)
427 ret = ieee80211_key_enable_hw_accel(new);
428 }
62872a9b 429 } else {
40b5a0f8 430 /* new must be provided in case old is not */
2475b1cc 431 idx = new->conf.keyidx;
40b5a0f8 432 if (!new->local->wowlan)
62872a9b 433 ret = ieee80211_key_enable_hw_accel(new);
62872a9b
AW
434 }
435
436 if (ret)
437 return ret;
3b96766f 438
2475b1cc
MS
439 if (sta) {
440 if (pairwise) {
441 rcu_assign_pointer(sta->ptk[idx], new);
96fc6efb
AW
442 if (new &&
443 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
444 sta->ptk_idx = idx;
62872a9b
AW
445 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
446 ieee80211_check_fast_xmit(sta);
447 }
2475b1cc
MS
448 } else {
449 rcu_assign_pointer(sta->gtk[idx], new);
2475b1cc 450 }
96fc6efb
AW
451 /* Only needed for transition from no key -> key.
452 * Still triggers unnecessary when using Extended Key ID
453 * and installing the second key ID the first time.
454 */
455 if (new && !old)
62872a9b 456 ieee80211_check_fast_rx(sta);
2475b1cc 457 } else {
40b275b6
JB
458 defunikey = old &&
459 old == key_mtx_dereference(sdata->local,
460 sdata->default_unicast_key);
461 defmultikey = old &&
462 old == key_mtx_dereference(sdata->local,
463 sdata->default_multicast_key);
464 defmgmtkey = old &&
465 old == key_mtx_dereference(sdata->local,
466 sdata->default_mgmt_key);
3b96766f 467
f7e0104c
JB
468 if (defunikey && !new)
469 __ieee80211_set_default_key(sdata, -1, true, false);
470 if (defmultikey && !new)
471 __ieee80211_set_default_key(sdata, -1, false, true);
3cfcf6ac
JM
472 if (defmgmtkey && !new)
473 __ieee80211_set_default_mgmt_key(sdata, -1);
3b96766f
JB
474
475 rcu_assign_pointer(sdata->keys[idx], new);
f7e0104c
JB
476 if (defunikey && new)
477 __ieee80211_set_default_key(sdata, new->conf.keyidx,
478 true, false);
479 if (defmultikey && new)
480 __ieee80211_set_default_key(sdata, new->conf.keyidx,
481 false, true);
3cfcf6ac
JM
482 if (defmgmtkey && new)
483 __ieee80211_set_default_mgmt_key(sdata,
484 new->conf.keyidx);
3b96766f
JB
485 }
486
b5c34f66 487 if (old)
ef044763 488 list_del_rcu(&old->list);
62872a9b
AW
489
490 return 0;
11a843b7
JB
491}
492
2475b1cc
MS
493struct ieee80211_key *
494ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
495 const u8 *key_data,
496 size_t seq_len, const u8 *seq,
497 const struct ieee80211_cipher_scheme *cs)
1f5a7e47
JB
498{
499 struct ieee80211_key *key;
1ac62ba7 500 int i, j, err;
1f5a7e47 501
8c5bb1fa
JB
502 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
503 return ERR_PTR(-EINVAL);
11a843b7
JB
504
505 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
1f5a7e47 506 if (!key)
1ac62ba7 507 return ERR_PTR(-ENOMEM);
11a843b7
JB
508
509 /*
510 * Default to software encryption; we'll later upload the
511 * key to the hardware if possible.
512 */
11a843b7
JB
513 key->conf.flags = 0;
514 key->flags = 0;
515
97359d12 516 key->conf.cipher = cipher;
11a843b7
JB
517 key->conf.keyidx = idx;
518 key->conf.keylen = key_len;
97359d12
JB
519 switch (cipher) {
520 case WLAN_CIPHER_SUITE_WEP40:
521 case WLAN_CIPHER_SUITE_WEP104:
4325f6ca
JB
522 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
523 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
76708dee 524 break;
97359d12 525 case WLAN_CIPHER_SUITE_TKIP:
4325f6ca
JB
526 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
527 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
9f26a952 528 if (seq) {
5a306f58 529 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
faa8fdc8
JM
530 key->u.tkip.rx[i].iv32 =
531 get_unaligned_le32(&seq[2]);
532 key->u.tkip.rx[i].iv16 =
533 get_unaligned_le16(seq);
534 }
535 }
523b02ea 536 spin_lock_init(&key->u.tkip.txlock);
76708dee 537 break;
97359d12 538 case WLAN_CIPHER_SUITE_CCMP:
4325f6ca
JB
539 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
540 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
9f26a952 541 if (seq) {
5a306f58 542 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
4325f6ca 543 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
faa8fdc8 544 key->u.ccmp.rx_pn[i][j] =
4325f6ca 545 seq[IEEE80211_CCMP_PN_LEN - j - 1];
faa8fdc8 546 }
11a843b7
JB
547 /*
548 * Initialize AES key state here as an optimization so that
549 * it does not need to be initialized for every packet.
550 */
2b2ba0db
JM
551 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
552 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
553 if (IS_ERR(key->u.ccmp.tfm)) {
554 err = PTR_ERR(key->u.ccmp.tfm);
555 kfree(key);
556 return ERR_PTR(err);
557 }
558 break;
559 case WLAN_CIPHER_SUITE_CCMP_256:
560 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
561 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
562 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
563 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
564 key->u.ccmp.rx_pn[i][j] =
565 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
566 /* Initialize AES key state here as an optimization so that
567 * it does not need to be initialized for every packet.
568 */
569 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
570 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
1ac62ba7
BH
571 if (IS_ERR(key->u.ccmp.tfm)) {
572 err = PTR_ERR(key->u.ccmp.tfm);
3b96766f 573 kfree(key);
1f951a7f 574 return ERR_PTR(err);
11a843b7 575 }
60ae0f20
JB
576 break;
577 case WLAN_CIPHER_SUITE_AES_CMAC:
56c52da2 578 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
60ae0f20 579 key->conf.iv_len = 0;
56c52da2
JM
580 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
581 key->conf.icv_len = sizeof(struct ieee80211_mmie);
582 else
583 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
60ae0f20 584 if (seq)
4325f6ca 585 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
0f927323 586 key->u.aes_cmac.rx_pn[j] =
4325f6ca 587 seq[IEEE80211_CMAC_PN_LEN - j - 1];
3cfcf6ac
JM
588 /*
589 * Initialize AES key state here as an optimization so that
590 * it does not need to be initialized for every packet.
591 */
592 key->u.aes_cmac.tfm =
56c52da2 593 ieee80211_aes_cmac_key_setup(key_data, key_len);
1ac62ba7
BH
594 if (IS_ERR(key->u.aes_cmac.tfm)) {
595 err = PTR_ERR(key->u.aes_cmac.tfm);
3cfcf6ac 596 kfree(key);
1f951a7f 597 return ERR_PTR(err);
3cfcf6ac 598 }
60ae0f20 599 break;
8ade538b
JM
600 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
601 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
602 key->conf.iv_len = 0;
603 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
604 if (seq)
605 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
606 key->u.aes_gmac.rx_pn[j] =
607 seq[IEEE80211_GMAC_PN_LEN - j - 1];
608 /* Initialize AES key state here as an optimization so that
609 * it does not need to be initialized for every packet.
610 */
611 key->u.aes_gmac.tfm =
612 ieee80211_aes_gmac_key_setup(key_data, key_len);
613 if (IS_ERR(key->u.aes_gmac.tfm)) {
614 err = PTR_ERR(key->u.aes_gmac.tfm);
615 kfree(key);
616 return ERR_PTR(err);
617 }
618 break;
00b9cfa3
JM
619 case WLAN_CIPHER_SUITE_GCMP:
620 case WLAN_CIPHER_SUITE_GCMP_256:
621 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
622 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
623 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
624 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
625 key->u.gcmp.rx_pn[i][j] =
626 seq[IEEE80211_GCMP_PN_LEN - j - 1];
627 /* Initialize AES key state here as an optimization so that
628 * it does not need to be initialized for every packet.
629 */
630 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
631 key_len);
632 if (IS_ERR(key->u.gcmp.tfm)) {
633 err = PTR_ERR(key->u.gcmp.tfm);
634 kfree(key);
635 return ERR_PTR(err);
636 }
637 break;
2475b1cc
MS
638 default:
639 if (cs) {
e3a55b53
JB
640 if (seq_len && seq_len != cs->pn_len) {
641 kfree(key);
642 return ERR_PTR(-EINVAL);
643 }
2475b1cc
MS
644
645 key->conf.iv_len = cs->hdr_len;
646 key->conf.icv_len = cs->mic_len;
647 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
e3a55b53 648 for (j = 0; j < seq_len; j++)
2475b1cc 649 key->u.gen.rx_pn[i][j] =
e3a55b53 650 seq[seq_len - j - 1];
c7ef38e0 651 key->flags |= KEY_FLAG_CIPHER_SCHEME;
2475b1cc 652 }
3cfcf6ac 653 }
60ae0f20
JB
654 memcpy(key->conf.key, key_data, key_len);
655 INIT_LIST_HEAD(&key->list);
3cfcf6ac 656
db4d1169
JB
657 return key;
658}
11a843b7 659
79cf2dfa
JB
660static void ieee80211_key_free_common(struct ieee80211_key *key)
661{
00b9cfa3
JM
662 switch (key->conf.cipher) {
663 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db 664 case WLAN_CIPHER_SUITE_CCMP_256:
79cf2dfa 665 ieee80211_aes_key_free(key->u.ccmp.tfm);
00b9cfa3
JM
666 break;
667 case WLAN_CIPHER_SUITE_AES_CMAC:
56c52da2 668 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
79cf2dfa 669 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
00b9cfa3 670 break;
8ade538b
JM
671 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
672 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
673 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
674 break;
00b9cfa3
JM
675 case WLAN_CIPHER_SUITE_GCMP:
676 case WLAN_CIPHER_SUITE_GCMP_256:
677 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
678 break;
679 }
29c3f9c3 680 kzfree(key);
79cf2dfa
JB
681}
682
6d10e46b
JB
683static void __ieee80211_key_destroy(struct ieee80211_key *key,
684 bool delay_tailroom)
ad0e2b5a 685{
3bff1865 686 if (key->local) {
8d1f7ecd
JB
687 struct ieee80211_sub_if_data *sdata = key->sdata;
688
32162a4d 689 ieee80211_debugfs_key_remove(key);
8d1f7ecd
JB
690
691 if (delay_tailroom) {
692 /* see ieee80211_delayed_tailroom_dec */
693 sdata->crypto_tx_tailroom_pending_dec++;
694 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
695 HZ/2);
696 } else {
f9dca80b 697 decrease_tailroom_need_count(sdata, 1);
8d1f7ecd 698 }
3bff1865 699 }
ad0e2b5a 700
79cf2dfa
JB
701 ieee80211_key_free_common(key);
702}
703
6d10e46b
JB
704static void ieee80211_key_destroy(struct ieee80211_key *key,
705 bool delay_tailroom)
706{
707 if (!key)
708 return;
709
710 /*
ef044763
EP
711 * Synchronize so the TX path and rcu key iterators
712 * can no longer be using this key before we free/remove it.
6d10e46b
JB
713 */
714 synchronize_net();
715
716 __ieee80211_key_destroy(key, delay_tailroom);
717}
718
79cf2dfa
JB
719void ieee80211_key_free_unused(struct ieee80211_key *key)
720{
721 WARN_ON(key->sdata || key->local);
722 ieee80211_key_free_common(key);
ad0e2b5a
JB
723}
724
cfbb0d90
JB
725static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
726 struct ieee80211_key *old,
727 struct ieee80211_key *new)
728{
729 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
730 u8 *tk_old, *tk_new;
731
732 if (!old || new->conf.keylen != old->conf.keylen)
733 return false;
734
735 tk_old = old->conf.key;
736 tk_new = new->conf.key;
737
738 /*
739 * In station mode, don't compare the TX MIC key, as it's never used
740 * and offloaded rekeying may not care to send it to the host. This
741 * is the case in iwlwifi, for example.
742 */
743 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
744 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
745 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
746 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
747 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
748 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
749 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
750 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
751 tk_old = tkip_old;
752 tk_new = tkip_new;
753 }
754
755 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
756}
757
3ffc2a90
JB
758int ieee80211_key_link(struct ieee80211_key *key,
759 struct ieee80211_sub_if_data *sdata,
760 struct sta_info *sta)
db4d1169
JB
761{
762 struct ieee80211_key *old_key;
133bf90d
MP
763 int idx = key->conf.keyidx;
764 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
765 /*
766 * We want to delay tailroom updates only for station - in that
767 * case it helps roaming speed, but in other cases it hurts and
768 * can cause warnings to appear.
769 */
770 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
96fc6efb 771 int ret = -EOPNOTSUPP;
db4d1169 772
ad0e2b5a 773 mutex_lock(&sdata->local->key_mtx);
3b96766f 774
96fc6efb
AW
775 if (sta && pairwise) {
776 struct ieee80211_key *alt_key;
777
2475b1cc 778 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
96fc6efb
AW
779 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
780
781 /* The rekey code assumes that the old and new key are using
782 * the same cipher. Enforce the assumption for pairwise keys.
783 */
784 if (key &&
785 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
786 (old_key && old_key->conf.cipher != key->conf.cipher)))
787 goto out;
788 } else if (sta) {
40b275b6 789 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
96fc6efb 790 } else {
40b275b6 791 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
96fc6efb
AW
792 }
793
794 /* Non-pairwise keys must also not switch the cipher on rekey */
795 if (!pairwise) {
796 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
797 goto out;
798 }
db4d1169 799
fdf7cb41
JB
800 /*
801 * Silently accept key re-installation without really installing the
802 * new version of the key to avoid nonce reuse or replay issues.
803 */
cfbb0d90 804 if (ieee80211_key_identical(sdata, old_key, key)) {
fdf7cb41
JB
805 ieee80211_key_free_unused(key);
806 ret = 0;
807 goto out;
808 }
809
810 key->local = sdata->local;
811 key->sdata = sdata;
812 key->sta = sta;
813
3bff1865
YAP
814 increment_tailroom_need_count(sdata);
815
62872a9b 816 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
db4d1169 817
62872a9b
AW
818 if (!ret) {
819 ieee80211_debugfs_key_add(key);
820 ieee80211_key_destroy(old_key, delay_tailroom);
27b3eb9c 821 } else {
62872a9b 822 ieee80211_key_free(key, delay_tailroom);
27b3eb9c 823 }
79cf2dfa 824
fdf7cb41 825 out:
ad0e2b5a 826 mutex_unlock(&sdata->local->key_mtx);
3ffc2a90
JB
827
828 return ret;
1f5a7e47
JB
829}
830
3b8d9c29 831void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
1f5a7e47 832{
5c0c3641
JB
833 if (!key)
834 return;
835
3b96766f
JB
836 /*
837 * Replace key with nothingness if it was ever used.
838 */
3a245766 839 if (key->sdata)
3b8d9c29 840 ieee80211_key_replace(key->sdata, key->sta,
e31b8213
JB
841 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
842 key, NULL);
3b8d9c29 843 ieee80211_key_destroy(key, delay_tailroom);
3b96766f 844}
d4e46a3d 845
ad0e2b5a 846void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
3a245766
JB
847{
848 struct ieee80211_key *key;
f9dca80b 849 struct ieee80211_sub_if_data *vlan;
11a843b7 850
3a245766 851 ASSERT_RTNL();
11a843b7 852
9607e6b6 853 if (WARN_ON(!ieee80211_sdata_running(sdata)))
3a245766 854 return;
11a843b7 855
ad0e2b5a 856 mutex_lock(&sdata->local->key_mtx);
11a843b7 857
f9dca80b
MK
858 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
859 sdata->crypto_tx_tailroom_pending_dec);
860
861 if (sdata->vif.type == NL80211_IFTYPE_AP) {
862 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
863 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
864 vlan->crypto_tx_tailroom_pending_dec);
865 }
3bff1865
YAP
866
867 list_for_each_entry(key, &sdata->key_list, list) {
868 increment_tailroom_need_count(sdata);
ad0e2b5a 869 ieee80211_key_enable_hw_accel(key);
3bff1865 870 }
3b96766f 871
ad0e2b5a 872 mutex_unlock(&sdata->local->key_mtx);
11a843b7
JB
873}
874
f9dca80b
MK
875void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
876{
877 struct ieee80211_sub_if_data *vlan;
878
879 mutex_lock(&sdata->local->key_mtx);
880
881 sdata->crypto_tx_tailroom_needed_cnt = 0;
882
883 if (sdata->vif.type == NL80211_IFTYPE_AP) {
884 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
885 vlan->crypto_tx_tailroom_needed_cnt = 0;
886 }
887
888 mutex_unlock(&sdata->local->key_mtx);
889}
890
830af02f
JB
891void ieee80211_iter_keys(struct ieee80211_hw *hw,
892 struct ieee80211_vif *vif,
893 void (*iter)(struct ieee80211_hw *hw,
894 struct ieee80211_vif *vif,
895 struct ieee80211_sta *sta,
896 struct ieee80211_key_conf *key,
897 void *data),
898 void *iter_data)
899{
900 struct ieee80211_local *local = hw_to_local(hw);
27b3eb9c 901 struct ieee80211_key *key, *tmp;
830af02f
JB
902 struct ieee80211_sub_if_data *sdata;
903
904 ASSERT_RTNL();
905
906 mutex_lock(&local->key_mtx);
907 if (vif) {
908 sdata = vif_to_sdata(vif);
27b3eb9c 909 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
830af02f
JB
910 iter(hw, &sdata->vif,
911 key->sta ? &key->sta->sta : NULL,
912 &key->conf, iter_data);
913 } else {
914 list_for_each_entry(sdata, &local->interfaces, list)
27b3eb9c
JB
915 list_for_each_entry_safe(key, tmp,
916 &sdata->key_list, list)
830af02f
JB
917 iter(hw, &sdata->vif,
918 key->sta ? &key->sta->sta : NULL,
919 &key->conf, iter_data);
920 }
921 mutex_unlock(&local->key_mtx);
922}
923EXPORT_SYMBOL(ieee80211_iter_keys);
924
ef044763
EP
925static void
926_ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
927 struct ieee80211_sub_if_data *sdata,
928 void (*iter)(struct ieee80211_hw *hw,
929 struct ieee80211_vif *vif,
930 struct ieee80211_sta *sta,
931 struct ieee80211_key_conf *key,
932 void *data),
933 void *iter_data)
934{
935 struct ieee80211_key *key;
936
937 list_for_each_entry_rcu(key, &sdata->key_list, list) {
938 /* skip keys of station in removal process */
939 if (key->sta && key->sta->removed)
940 continue;
941 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
942 continue;
943
944 iter(hw, &sdata->vif,
945 key->sta ? &key->sta->sta : NULL,
946 &key->conf, iter_data);
947 }
948}
949
950void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
951 struct ieee80211_vif *vif,
952 void (*iter)(struct ieee80211_hw *hw,
953 struct ieee80211_vif *vif,
954 struct ieee80211_sta *sta,
955 struct ieee80211_key_conf *key,
956 void *data),
957 void *iter_data)
958{
959 struct ieee80211_local *local = hw_to_local(hw);
960 struct ieee80211_sub_if_data *sdata;
961
962 if (vif) {
963 sdata = vif_to_sdata(vif);
964 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
965 } else {
966 list_for_each_entry_rcu(sdata, &local->interfaces, list)
967 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
968 }
969}
970EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
971
7907c7d3
JB
972static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
973 struct list_head *keys)
3b96766f
JB
974{
975 struct ieee80211_key *key, *tmp;
3b96766f 976
f9dca80b
MK
977 decrease_tailroom_need_count(sdata,
978 sdata->crypto_tx_tailroom_pending_dec);
8d1f7ecd
JB
979 sdata->crypto_tx_tailroom_pending_dec = 0;
980
3cfcf6ac 981 ieee80211_debugfs_key_remove_mgmt_default(sdata);
3b96766f 982
6d10e46b
JB
983 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
984 ieee80211_key_replace(key->sdata, key->sta,
985 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
986 key, NULL);
7907c7d3 987 list_add_tail(&key->list, keys);
6d10e46b 988 }
3b96766f 989
f7e0104c 990 ieee80211_debugfs_key_update_default(sdata);
7907c7d3 991}
f7e0104c 992
7907c7d3
JB
993void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
994 bool force_synchronize)
995{
996 struct ieee80211_local *local = sdata->local;
997 struct ieee80211_sub_if_data *vlan;
f9dca80b 998 struct ieee80211_sub_if_data *master;
7907c7d3
JB
999 struct ieee80211_key *key, *tmp;
1000 LIST_HEAD(keys);
1001
1002 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
1003
1004 mutex_lock(&local->key_mtx);
1005
1006 ieee80211_free_keys_iface(sdata, &keys);
1007
1008 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1009 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1010 ieee80211_free_keys_iface(vlan, &keys);
6d10e46b
JB
1011 }
1012
7907c7d3
JB
1013 if (!list_empty(&keys) || force_synchronize)
1014 synchronize_net();
1015 list_for_each_entry_safe(key, tmp, &keys, list)
1016 __ieee80211_key_destroy(key, false);
1017
f9dca80b
MK
1018 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1019 if (sdata->bss) {
1020 master = container_of(sdata->bss,
1021 struct ieee80211_sub_if_data,
1022 u.ap);
1023
1024 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1025 master->crypto_tx_tailroom_needed_cnt);
1026 }
1027 } else {
1028 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1029 sdata->crypto_tx_tailroom_pending_dec);
1030 }
1031
7907c7d3
JB
1032 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1033 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1034 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1035 vlan->crypto_tx_tailroom_pending_dec);
1036 }
8d1f7ecd 1037
7907c7d3 1038 mutex_unlock(&local->key_mtx);
11a843b7 1039}
c68f4b89 1040
6d10e46b
JB
1041void ieee80211_free_sta_keys(struct ieee80211_local *local,
1042 struct sta_info *sta)
1043{
c8782078 1044 struct ieee80211_key *key;
6d10e46b
JB
1045 int i;
1046
1047 mutex_lock(&local->key_mtx);
28a9bc68 1048 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
6d10e46b
JB
1049 key = key_mtx_dereference(local, sta->gtk[i]);
1050 if (!key)
1051 continue;
1052 ieee80211_key_replace(key->sdata, key->sta,
1053 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1054 key, NULL);
133bf90d
MP
1055 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1056 NL80211_IFTYPE_STATION);
6d10e46b
JB
1057 }
1058
2475b1cc
MS
1059 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1060 key = key_mtx_dereference(local, sta->ptk[i]);
1061 if (!key)
1062 continue;
6d10e46b
JB
1063 ieee80211_key_replace(key->sdata, key->sta,
1064 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1065 key, NULL);
133bf90d
MP
1066 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1067 NL80211_IFTYPE_STATION);
c8782078 1068 }
6d10e46b
JB
1069
1070 mutex_unlock(&local->key_mtx);
1071}
1072
8d1f7ecd
JB
1073void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1074{
1075 struct ieee80211_sub_if_data *sdata;
1076
1077 sdata = container_of(wk, struct ieee80211_sub_if_data,
1078 dec_tailroom_needed_wk.work);
1079
1080 /*
1081 * The reason for the delayed tailroom needed decrementing is to
1082 * make roaming faster: during roaming, all keys are first deleted
1083 * and then new keys are installed. The first new key causes the
1084 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1085 * the cost of synchronize_net() (which can be slow). Avoid this
1086 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1087 * key removal for a while, so if we roam the value is larger than
1088 * zero and no 0->1 transition happens.
1089 *
1090 * The cost is that if the AP switching was from an AP with keys
1091 * to one without, we still allocate tailroom while it would no
1092 * longer be needed. However, in the typical (fast) roaming case
1093 * within an ESS this usually won't happen.
1094 */
1095
1096 mutex_lock(&sdata->local->key_mtx);
f9dca80b
MK
1097 decrease_tailroom_need_count(sdata,
1098 sdata->crypto_tx_tailroom_pending_dec);
8d1f7ecd
JB
1099 sdata->crypto_tx_tailroom_pending_dec = 0;
1100 mutex_unlock(&sdata->local->key_mtx);
1101}
c68f4b89
JB
1102
1103void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1104 const u8 *replay_ctr, gfp_t gfp)
1105{
1106 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1107
1108 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1109
1110 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1111}
1112EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
3ea542d3 1113
3ea542d3
JB
1114void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1115 int tid, struct ieee80211_key_seq *seq)
1116{
1117 struct ieee80211_key *key;
1118 const u8 *pn;
1119
1120 key = container_of(keyconf, struct ieee80211_key, conf);
1121
1122 switch (key->conf.cipher) {
1123 case WLAN_CIPHER_SUITE_TKIP:
5a306f58 1124 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
3ea542d3
JB
1125 return;
1126 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1127 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1128 break;
1129 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db 1130 case WLAN_CIPHER_SUITE_CCMP_256:
5a306f58 1131 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
3ea542d3
JB
1132 return;
1133 if (tid < 0)
5a306f58 1134 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
3ea542d3
JB
1135 else
1136 pn = key->u.ccmp.rx_pn[tid];
4325f6ca 1137 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
3ea542d3
JB
1138 break;
1139 case WLAN_CIPHER_SUITE_AES_CMAC:
56c52da2 1140 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3ea542d3
JB
1141 if (WARN_ON(tid != 0))
1142 return;
1143 pn = key->u.aes_cmac.rx_pn;
4325f6ca 1144 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
3ea542d3 1145 break;
8ade538b
JM
1146 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1147 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1148 if (WARN_ON(tid != 0))
1149 return;
1150 pn = key->u.aes_gmac.rx_pn;
1151 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1152 break;
00b9cfa3
JM
1153 case WLAN_CIPHER_SUITE_GCMP:
1154 case WLAN_CIPHER_SUITE_GCMP_256:
1155 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1156 return;
1157 if (tid < 0)
1158 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1159 else
1160 pn = key->u.gcmp.rx_pn[tid];
1161 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1162 break;
3ea542d3
JB
1163 }
1164}
1165EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
27b3eb9c 1166
27b3eb9c
JB
1167void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1168 int tid, struct ieee80211_key_seq *seq)
1169{
1170 struct ieee80211_key *key;
1171 u8 *pn;
1172
1173 key = container_of(keyconf, struct ieee80211_key, conf);
1174
1175 switch (key->conf.cipher) {
1176 case WLAN_CIPHER_SUITE_TKIP:
1177 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1178 return;
1179 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1180 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1181 break;
1182 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db 1183 case WLAN_CIPHER_SUITE_CCMP_256:
27b3eb9c
JB
1184 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1185 return;
1186 if (tid < 0)
1187 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1188 else
1189 pn = key->u.ccmp.rx_pn[tid];
1190 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1191 break;
1192 case WLAN_CIPHER_SUITE_AES_CMAC:
56c52da2 1193 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
27b3eb9c
JB
1194 if (WARN_ON(tid != 0))
1195 return;
1196 pn = key->u.aes_cmac.rx_pn;
1197 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1198 break;
8ade538b
JM
1199 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1200 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1201 if (WARN_ON(tid != 0))
1202 return;
1203 pn = key->u.aes_gmac.rx_pn;
1204 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1205 break;
00b9cfa3
JM
1206 case WLAN_CIPHER_SUITE_GCMP:
1207 case WLAN_CIPHER_SUITE_GCMP_256:
1208 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1209 return;
1210 if (tid < 0)
1211 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1212 else
1213 pn = key->u.gcmp.rx_pn[tid];
1214 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1215 break;
27b3eb9c
JB
1216 default:
1217 WARN_ON(1);
1218 break;
1219 }
1220}
1221EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1222
1223void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1224{
1225 struct ieee80211_key *key;
1226
1227 key = container_of(keyconf, struct ieee80211_key, conf);
1228
1229 assert_key_lock(key->local);
1230
1231 /*
1232 * if key was uploaded, we assume the driver will/has remove(d)
1233 * it, so adjust bookkeeping accordingly
1234 */
1235 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1236 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1237
092c4098
AW
1238 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1239 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1240 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
27b3eb9c
JB
1241 increment_tailroom_need_count(key->sdata);
1242 }
1243
1244 ieee80211_key_free(key, false);
1245}
1246EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1247
1248struct ieee80211_key_conf *
1249ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1250 struct ieee80211_key_conf *keyconf)
1251{
1252 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1253 struct ieee80211_local *local = sdata->local;
1254 struct ieee80211_key *key;
1255 int err;
1256
1257 if (WARN_ON(!local->wowlan))
1258 return ERR_PTR(-EINVAL);
1259
1260 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1261 return ERR_PTR(-EINVAL);
1262
1263 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1264 keyconf->keylen, keyconf->key,
2475b1cc 1265 0, NULL, NULL);
27b3eb9c 1266 if (IS_ERR(key))
c5dc164d 1267 return ERR_CAST(key);
27b3eb9c
JB
1268
1269 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1270 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1271
1272 err = ieee80211_key_link(key, sdata, NULL);
1273 if (err)
1274 return ERR_PTR(err);
1275
1276 return &key->conf;
1277}
1278EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);