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