]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/mac80211/key.c
Merge branches 'acpi-doc', 'acpi-pm', 'acpi-pcc' and 'acpi-tables'
[mirror_ubuntu-artful-kernel.git] / net / mac80211 / key.c
CommitLineData
1f5a7e47
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
3b96766f 5 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
d98ad83e 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
1f5a7e47
JB
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
11a843b7
JB
13#include <linux/if_ether.h>
14#include <linux/etherdevice.h>
15#include <linux/list.h>
d4e46a3d 16#include <linux/rcupdate.h>
db4d1169 17#include <linux/rtnetlink.h>
5a0e3ad6 18#include <linux/slab.h>
bc3b2d7f 19#include <linux/export.h>
1f5a7e47 20#include <net/mac80211.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"
1f5a7e47 27
11a843b7 28
dbbea671
JB
29/**
30 * DOC: Key handling basics
11a843b7
JB
31 *
32 * Key handling in mac80211 is done based on per-interface (sub_if_data)
33 * keys and per-station keys. Since each station belongs to an interface,
34 * each station key also belongs to that interface.
35 *
b5c34f66
JB
36 * Hardware acceleration is done on a best-effort basis for algorithms
37 * that are implemented in software, for each key the hardware is asked
38 * to enable that key for offloading but if it cannot do that the key is
39 * simply kept for software encryption (unless it is for an algorithm
40 * that isn't implemented in software).
41 * There is currently no way of knowing whether a key is handled in SW
42 * or HW except by looking into debugfs.
11a843b7 43 *
b5c34f66
JB
44 * All key management is internally protected by a mutex. Within all
45 * other parts of mac80211, key references are, just as STA structure
46 * references, protected by RCU. Note, however, that some things are
47 * unprotected, namely the key->sta dereferences within the hardware
48 * acceleration functions. This means that sta_info_destroy() must
49 * remove the key which waits for an RCU grace period.
11a843b7
JB
50 */
51
52static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
11a843b7 53
ad0e2b5a 54static void assert_key_lock(struct ieee80211_local *local)
3b96766f 55{
46a5ebaf 56 lockdep_assert_held(&local->key_mtx);
3b96766f
JB
57}
58
3bff1865
YAP
59static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
60{
61 /*
62 * When this count is zero, SKB resizing for allocating tailroom
63 * for IV or MMIC is skipped. But, this check has created two race
64 * cases in xmit path while transiting from zero count to one:
65 *
66 * 1. SKB resize was skipped because no key was added but just before
67 * the xmit key is added and SW encryption kicks off.
68 *
69 * 2. SKB resize was skipped because all the keys were hw planted but
70 * just before xmit one of the key is deleted and SW encryption kicks
71 * off.
72 *
73 * In both the above case SW encryption will find not enough space for
74 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
75 *
76 * Solution has been explained at
77 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
78 */
79
80 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
81 /*
82 * Flush all XMIT packets currently using HW encryption or no
83 * encryption at all if the count transition is from 0 -> 1.
84 */
85 synchronize_net();
86 }
87}
88
3ffc2a90 89static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
11a843b7 90{
dc822b5d 91 struct ieee80211_sub_if_data *sdata;
89c91cae 92 struct sta_info *sta;
11a843b7
JB
93 int ret;
94
3b96766f
JB
95 might_sleep();
96
4619194a
JB
97 if (key->flags & KEY_FLAG_TAINTED) {
98 /* If we get here, it's during resume and the key is
99 * tainted so shouldn't be used/programmed any more.
100 * However, its flags may still indicate that it was
101 * programmed into the device (since we're in resume)
102 * so clear that flag now to avoid trying to remove
103 * it again later.
104 */
105 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
27b3eb9c 106 return -EINVAL;
4619194a 107 }
27b3eb9c 108
e31b8213 109 if (!key->local->ops->set_key)
3ffc2a90 110 goto out_unsupported;
11a843b7 111
ad0e2b5a
JB
112 assert_key_lock(key->local);
113
89c91cae 114 sta = key->sta;
dc822b5d 115
e31b8213
JB
116 /*
117 * If this is a per-STA GTK, check if it
118 * is supported; if not, return.
119 */
120 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
121 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
122 goto out_unsupported;
123
89c91cae
JB
124 if (sta && !sta->uploaded)
125 goto out_unsupported;
126
dc822b5d 127 sdata = key->sdata;
18890d4b
HS
128 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
129 /*
130 * The driver doesn't know anything about VLAN interfaces.
131 * Hence, don't send GTKs for VLAN interfaces to the driver.
132 */
133 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
134 goto out_unsupported;
18890d4b 135 }
11a843b7 136
89c91cae
JB
137 ret = drv_set_key(key->local, SET_KEY, sdata,
138 sta ? &sta->sta : NULL, &key->conf);
11a843b7 139
e31b8213 140 if (!ret) {
11a843b7 141 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
3bff1865 142
1e359a5d
JB
143 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
144 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
145 (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
3bff1865
YAP
146 sdata->crypto_tx_tailroom_needed_cnt--;
147
077a9154
AN
148 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
149 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
150
e31b8213
JB
151 return 0;
152 }
11a843b7 153
e31b8213 154 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
bdcbd8e0 155 sdata_err(sdata,
0fb9a9ec 156 "failed to set key (%d, %pM) to hardware (%d)\n",
89c91cae
JB
157 key->conf.keyidx,
158 sta ? sta->sta.addr : bcast_addr, ret);
3ffc2a90 159
e31b8213
JB
160 out_unsupported:
161 switch (key->conf.cipher) {
162 case WLAN_CIPHER_SUITE_WEP40:
163 case WLAN_CIPHER_SUITE_WEP104:
164 case WLAN_CIPHER_SUITE_TKIP:
165 case WLAN_CIPHER_SUITE_CCMP:
166 case WLAN_CIPHER_SUITE_AES_CMAC:
167 /* all of these we can do in software */
168 return 0;
169 default:
170 return -EINVAL;
3ffc2a90 171 }
11a843b7
JB
172}
173
174static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
175{
dc822b5d 176 struct ieee80211_sub_if_data *sdata;
89c91cae 177 struct sta_info *sta;
11a843b7
JB
178 int ret;
179
3b96766f
JB
180 might_sleep();
181
db4d1169 182 if (!key || !key->local->ops->set_key)
11a843b7
JB
183 return;
184
ad0e2b5a
JB
185 assert_key_lock(key->local);
186
187 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
11a843b7
JB
188 return;
189
89c91cae 190 sta = key->sta;
dc822b5d
JB
191 sdata = key->sdata;
192
1e359a5d
JB
193 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
194 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
195 (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
3bff1865
YAP
196 increment_tailroom_need_count(sdata);
197
12375ef9 198 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
89c91cae 199 sta ? &sta->sta : NULL, &key->conf);
11a843b7
JB
200
201 if (ret)
bdcbd8e0 202 sdata_err(sdata,
0fb9a9ec 203 "failed to remove key (%d, %pM) from hardware (%d)\n",
89c91cae
JB
204 key->conf.keyidx,
205 sta ? sta->sta.addr : bcast_addr, ret);
11a843b7 206
3b96766f 207 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
3b96766f
JB
208}
209
210static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
f7e0104c 211 int idx, bool uni, bool multi)
3b96766f
JB
212{
213 struct ieee80211_key *key = NULL;
214
ad0e2b5a
JB
215 assert_key_lock(sdata->local);
216
3b96766f 217 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
40b275b6 218 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
3b96766f 219
de5fad81 220 if (uni) {
f7e0104c 221 rcu_assign_pointer(sdata->default_unicast_key, key);
de5fad81
YD
222 drv_set_default_unicast_key(sdata->local, sdata, idx);
223 }
224
f7e0104c
JB
225 if (multi)
226 rcu_assign_pointer(sdata->default_multicast_key, key);
3b96766f 227
f7e0104c 228 ieee80211_debugfs_key_update_default(sdata);
3b96766f
JB
229}
230
f7e0104c
JB
231void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
232 bool uni, bool multi)
3b96766f 233{
ad0e2b5a 234 mutex_lock(&sdata->local->key_mtx);
f7e0104c 235 __ieee80211_set_default_key(sdata, idx, uni, multi);
ad0e2b5a 236 mutex_unlock(&sdata->local->key_mtx);
3b96766f
JB
237}
238
3cfcf6ac
JM
239static void
240__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
241{
242 struct ieee80211_key *key = NULL;
243
ad0e2b5a
JB
244 assert_key_lock(sdata->local);
245
3cfcf6ac
JM
246 if (idx >= NUM_DEFAULT_KEYS &&
247 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
40b275b6 248 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
3cfcf6ac
JM
249
250 rcu_assign_pointer(sdata->default_mgmt_key, key);
251
f7e0104c 252 ieee80211_debugfs_key_update_default(sdata);
3cfcf6ac
JM
253}
254
255void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
256 int idx)
257{
ad0e2b5a 258 mutex_lock(&sdata->local->key_mtx);
3cfcf6ac 259 __ieee80211_set_default_mgmt_key(sdata, idx);
ad0e2b5a 260 mutex_unlock(&sdata->local->key_mtx);
3cfcf6ac
JM
261}
262
3b96766f 263
3b8d9c29
JB
264static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
265 struct sta_info *sta,
266 bool pairwise,
267 struct ieee80211_key *old,
268 struct ieee80211_key *new)
3b96766f 269{
f7e0104c
JB
270 int idx;
271 bool defunikey, defmultikey, defmgmtkey;
3b96766f 272
5282c3ba
JB
273 /* caller must provide at least one old/new */
274 if (WARN_ON(!new && !old))
275 return;
276
3b96766f 277 if (new)
f850e00f 278 list_add_tail(&new->list, &sdata->key_list);
3b96766f 279
2475b1cc 280 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
3b96766f 281
2475b1cc
MS
282 if (old)
283 idx = old->conf.keyidx;
284 else
285 idx = new->conf.keyidx;
3b96766f 286
2475b1cc
MS
287 if (sta) {
288 if (pairwise) {
289 rcu_assign_pointer(sta->ptk[idx], new);
290 sta->ptk_idx = idx;
291 } else {
292 rcu_assign_pointer(sta->gtk[idx], new);
293 sta->gtk_idx = idx;
294 }
295 } else {
40b275b6
JB
296 defunikey = old &&
297 old == key_mtx_dereference(sdata->local,
298 sdata->default_unicast_key);
299 defmultikey = old &&
300 old == key_mtx_dereference(sdata->local,
301 sdata->default_multicast_key);
302 defmgmtkey = old &&
303 old == key_mtx_dereference(sdata->local,
304 sdata->default_mgmt_key);
3b96766f 305
f7e0104c
JB
306 if (defunikey && !new)
307 __ieee80211_set_default_key(sdata, -1, true, false);
308 if (defmultikey && !new)
309 __ieee80211_set_default_key(sdata, -1, false, true);
3cfcf6ac
JM
310 if (defmgmtkey && !new)
311 __ieee80211_set_default_mgmt_key(sdata, -1);
3b96766f
JB
312
313 rcu_assign_pointer(sdata->keys[idx], new);
f7e0104c
JB
314 if (defunikey && new)
315 __ieee80211_set_default_key(sdata, new->conf.keyidx,
316 true, false);
317 if (defmultikey && new)
318 __ieee80211_set_default_key(sdata, new->conf.keyidx,
319 false, true);
3cfcf6ac
JM
320 if (defmgmtkey && new)
321 __ieee80211_set_default_mgmt_key(sdata,
322 new->conf.keyidx);
3b96766f
JB
323 }
324
b5c34f66
JB
325 if (old)
326 list_del(&old->list);
11a843b7
JB
327}
328
2475b1cc
MS
329struct ieee80211_key *
330ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
331 const u8 *key_data,
332 size_t seq_len, const u8 *seq,
333 const struct ieee80211_cipher_scheme *cs)
1f5a7e47
JB
334{
335 struct ieee80211_key *key;
1ac62ba7 336 int i, j, err;
1f5a7e47 337
8c5bb1fa
JB
338 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
339 return ERR_PTR(-EINVAL);
11a843b7
JB
340
341 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
1f5a7e47 342 if (!key)
1ac62ba7 343 return ERR_PTR(-ENOMEM);
11a843b7
JB
344
345 /*
346 * Default to software encryption; we'll later upload the
347 * key to the hardware if possible.
348 */
11a843b7
JB
349 key->conf.flags = 0;
350 key->flags = 0;
351
97359d12 352 key->conf.cipher = cipher;
11a843b7
JB
353 key->conf.keyidx = idx;
354 key->conf.keylen = key_len;
97359d12
JB
355 switch (cipher) {
356 case WLAN_CIPHER_SUITE_WEP40:
357 case WLAN_CIPHER_SUITE_WEP104:
4325f6ca
JB
358 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
359 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
76708dee 360 break;
97359d12 361 case WLAN_CIPHER_SUITE_TKIP:
4325f6ca
JB
362 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
363 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
9f26a952 364 if (seq) {
5a306f58 365 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
faa8fdc8
JM
366 key->u.tkip.rx[i].iv32 =
367 get_unaligned_le32(&seq[2]);
368 key->u.tkip.rx[i].iv16 =
369 get_unaligned_le16(seq);
370 }
371 }
523b02ea 372 spin_lock_init(&key->u.tkip.txlock);
76708dee 373 break;
97359d12 374 case WLAN_CIPHER_SUITE_CCMP:
4325f6ca
JB
375 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
376 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
9f26a952 377 if (seq) {
5a306f58 378 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
4325f6ca 379 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
faa8fdc8 380 key->u.ccmp.rx_pn[i][j] =
4325f6ca 381 seq[IEEE80211_CCMP_PN_LEN - j - 1];
faa8fdc8 382 }
11a843b7
JB
383 /*
384 * Initialize AES key state here as an optimization so that
385 * it does not need to be initialized for every packet.
386 */
387 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
1ac62ba7
BH
388 if (IS_ERR(key->u.ccmp.tfm)) {
389 err = PTR_ERR(key->u.ccmp.tfm);
3b96766f 390 kfree(key);
1f951a7f 391 return ERR_PTR(err);
11a843b7 392 }
60ae0f20
JB
393 break;
394 case WLAN_CIPHER_SUITE_AES_CMAC:
395 key->conf.iv_len = 0;
396 key->conf.icv_len = sizeof(struct ieee80211_mmie);
397 if (seq)
4325f6ca 398 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
0f927323 399 key->u.aes_cmac.rx_pn[j] =
4325f6ca 400 seq[IEEE80211_CMAC_PN_LEN - j - 1];
3cfcf6ac
JM
401 /*
402 * Initialize AES key state here as an optimization so that
403 * it does not need to be initialized for every packet.
404 */
405 key->u.aes_cmac.tfm =
406 ieee80211_aes_cmac_key_setup(key_data);
1ac62ba7
BH
407 if (IS_ERR(key->u.aes_cmac.tfm)) {
408 err = PTR_ERR(key->u.aes_cmac.tfm);
3cfcf6ac 409 kfree(key);
1f951a7f 410 return ERR_PTR(err);
3cfcf6ac 411 }
60ae0f20 412 break;
2475b1cc
MS
413 default:
414 if (cs) {
415 size_t len = (seq_len > MAX_PN_LEN) ?
416 MAX_PN_LEN : seq_len;
417
418 key->conf.iv_len = cs->hdr_len;
419 key->conf.icv_len = cs->mic_len;
420 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
421 for (j = 0; j < len; j++)
422 key->u.gen.rx_pn[i][j] =
423 seq[len - j - 1];
424 }
3cfcf6ac 425 }
60ae0f20
JB
426 memcpy(key->conf.key, key_data, key_len);
427 INIT_LIST_HEAD(&key->list);
3cfcf6ac 428
db4d1169
JB
429 return key;
430}
11a843b7 431
79cf2dfa
JB
432static void ieee80211_key_free_common(struct ieee80211_key *key)
433{
434 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
435 ieee80211_aes_key_free(key->u.ccmp.tfm);
436 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
437 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
29c3f9c3 438 kzfree(key);
79cf2dfa
JB
439}
440
6d10e46b
JB
441static void __ieee80211_key_destroy(struct ieee80211_key *key,
442 bool delay_tailroom)
ad0e2b5a 443{
32162a4d
JM
444 if (key->local)
445 ieee80211_key_disable_hw_accel(key);
ad0e2b5a 446
3bff1865 447 if (key->local) {
8d1f7ecd
JB
448 struct ieee80211_sub_if_data *sdata = key->sdata;
449
32162a4d 450 ieee80211_debugfs_key_remove(key);
8d1f7ecd
JB
451
452 if (delay_tailroom) {
453 /* see ieee80211_delayed_tailroom_dec */
454 sdata->crypto_tx_tailroom_pending_dec++;
455 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
456 HZ/2);
457 } else {
458 sdata->crypto_tx_tailroom_needed_cnt--;
459 }
3bff1865 460 }
ad0e2b5a 461
79cf2dfa
JB
462 ieee80211_key_free_common(key);
463}
464
6d10e46b
JB
465static void ieee80211_key_destroy(struct ieee80211_key *key,
466 bool delay_tailroom)
467{
468 if (!key)
469 return;
470
471 /*
472 * Synchronize so the TX path can no longer be using
473 * this key before we free/remove it.
474 */
475 synchronize_net();
476
477 __ieee80211_key_destroy(key, delay_tailroom);
478}
479
79cf2dfa
JB
480void ieee80211_key_free_unused(struct ieee80211_key *key)
481{
482 WARN_ON(key->sdata || key->local);
483 ieee80211_key_free_common(key);
ad0e2b5a
JB
484}
485
3ffc2a90
JB
486int ieee80211_key_link(struct ieee80211_key *key,
487 struct ieee80211_sub_if_data *sdata,
488 struct sta_info *sta)
db4d1169 489{
27b3eb9c 490 struct ieee80211_local *local = sdata->local;
db4d1169 491 struct ieee80211_key *old_key;
3ffc2a90 492 int idx, ret;
67aa030c 493 bool pairwise;
db4d1169 494
67aa030c 495 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
db4d1169
JB
496 idx = key->conf.keyidx;
497 key->local = sdata->local;
498 key->sdata = sdata;
499 key->sta = sta;
500
ad0e2b5a 501 mutex_lock(&sdata->local->key_mtx);
3b96766f 502
e31b8213 503 if (sta && pairwise)
2475b1cc 504 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
e31b8213 505 else if (sta)
40b275b6 506 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
d4e46a3d 507 else
40b275b6 508 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
db4d1169 509
3bff1865
YAP
510 increment_tailroom_need_count(sdata);
511
3b8d9c29
JB
512 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
513 ieee80211_key_destroy(old_key, true);
d4e46a3d 514
ad0e2b5a 515 ieee80211_debugfs_key_add(key);
db4d1169 516
27b3eb9c
JB
517 if (!local->wowlan) {
518 ret = ieee80211_key_enable_hw_accel(key);
519 if (ret)
520 ieee80211_key_free(key, true);
521 } else {
522 ret = 0;
523 }
79cf2dfa 524
ad0e2b5a 525 mutex_unlock(&sdata->local->key_mtx);
3ffc2a90
JB
526
527 return ret;
1f5a7e47
JB
528}
529
3b8d9c29 530void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
1f5a7e47 531{
5c0c3641
JB
532 if (!key)
533 return;
534
3b96766f
JB
535 /*
536 * Replace key with nothingness if it was ever used.
537 */
3a245766 538 if (key->sdata)
3b8d9c29 539 ieee80211_key_replace(key->sdata, key->sta,
e31b8213
JB
540 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
541 key, NULL);
3b8d9c29 542 ieee80211_key_destroy(key, delay_tailroom);
3b96766f 543}
d4e46a3d 544
ad0e2b5a 545void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
3a245766
JB
546{
547 struct ieee80211_key *key;
11a843b7 548
3a245766 549 ASSERT_RTNL();
11a843b7 550
9607e6b6 551 if (WARN_ON(!ieee80211_sdata_running(sdata)))
3a245766 552 return;
11a843b7 553
ad0e2b5a 554 mutex_lock(&sdata->local->key_mtx);
11a843b7 555
3bff1865
YAP
556 sdata->crypto_tx_tailroom_needed_cnt = 0;
557
558 list_for_each_entry(key, &sdata->key_list, list) {
559 increment_tailroom_need_count(sdata);
ad0e2b5a 560 ieee80211_key_enable_hw_accel(key);
3bff1865 561 }
3b96766f 562
ad0e2b5a 563 mutex_unlock(&sdata->local->key_mtx);
11a843b7
JB
564}
565
830af02f
JB
566void ieee80211_iter_keys(struct ieee80211_hw *hw,
567 struct ieee80211_vif *vif,
568 void (*iter)(struct ieee80211_hw *hw,
569 struct ieee80211_vif *vif,
570 struct ieee80211_sta *sta,
571 struct ieee80211_key_conf *key,
572 void *data),
573 void *iter_data)
574{
575 struct ieee80211_local *local = hw_to_local(hw);
27b3eb9c 576 struct ieee80211_key *key, *tmp;
830af02f
JB
577 struct ieee80211_sub_if_data *sdata;
578
579 ASSERT_RTNL();
580
581 mutex_lock(&local->key_mtx);
582 if (vif) {
583 sdata = vif_to_sdata(vif);
27b3eb9c 584 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
830af02f
JB
585 iter(hw, &sdata->vif,
586 key->sta ? &key->sta->sta : NULL,
587 &key->conf, iter_data);
588 } else {
589 list_for_each_entry(sdata, &local->interfaces, list)
27b3eb9c
JB
590 list_for_each_entry_safe(key, tmp,
591 &sdata->key_list, list)
830af02f
JB
592 iter(hw, &sdata->vif,
593 key->sta ? &key->sta->sta : NULL,
594 &key->conf, iter_data);
595 }
596 mutex_unlock(&local->key_mtx);
597}
598EXPORT_SYMBOL(ieee80211_iter_keys);
599
7907c7d3
JB
600static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
601 struct list_head *keys)
3b96766f
JB
602{
603 struct ieee80211_key *key, *tmp;
3b96766f 604
8d1f7ecd
JB
605 sdata->crypto_tx_tailroom_needed_cnt -=
606 sdata->crypto_tx_tailroom_pending_dec;
607 sdata->crypto_tx_tailroom_pending_dec = 0;
608
3cfcf6ac 609 ieee80211_debugfs_key_remove_mgmt_default(sdata);
3b96766f 610
6d10e46b
JB
611 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
612 ieee80211_key_replace(key->sdata, key->sta,
613 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
614 key, NULL);
7907c7d3 615 list_add_tail(&key->list, keys);
6d10e46b 616 }
3b96766f 617
f7e0104c 618 ieee80211_debugfs_key_update_default(sdata);
7907c7d3 619}
f7e0104c 620
7907c7d3
JB
621void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
622 bool force_synchronize)
623{
624 struct ieee80211_local *local = sdata->local;
625 struct ieee80211_sub_if_data *vlan;
626 struct ieee80211_key *key, *tmp;
627 LIST_HEAD(keys);
628
629 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
630
631 mutex_lock(&local->key_mtx);
632
633 ieee80211_free_keys_iface(sdata, &keys);
634
635 if (sdata->vif.type == NL80211_IFTYPE_AP) {
636 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
637 ieee80211_free_keys_iface(vlan, &keys);
6d10e46b
JB
638 }
639
7907c7d3
JB
640 if (!list_empty(&keys) || force_synchronize)
641 synchronize_net();
642 list_for_each_entry_safe(key, tmp, &keys, list)
643 __ieee80211_key_destroy(key, false);
644
8d1f7ecd
JB
645 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
646 sdata->crypto_tx_tailroom_pending_dec);
7907c7d3
JB
647 if (sdata->vif.type == NL80211_IFTYPE_AP) {
648 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
649 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
650 vlan->crypto_tx_tailroom_pending_dec);
651 }
8d1f7ecd 652
7907c7d3 653 mutex_unlock(&local->key_mtx);
11a843b7 654}
c68f4b89 655
6d10e46b
JB
656void ieee80211_free_sta_keys(struct ieee80211_local *local,
657 struct sta_info *sta)
658{
c8782078 659 struct ieee80211_key *key;
6d10e46b
JB
660 int i;
661
662 mutex_lock(&local->key_mtx);
28a9bc68 663 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
6d10e46b
JB
664 key = key_mtx_dereference(local, sta->gtk[i]);
665 if (!key)
666 continue;
667 ieee80211_key_replace(key->sdata, key->sta,
668 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
669 key, NULL);
c8782078 670 __ieee80211_key_destroy(key, true);
6d10e46b
JB
671 }
672
2475b1cc
MS
673 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
674 key = key_mtx_dereference(local, sta->ptk[i]);
675 if (!key)
676 continue;
6d10e46b
JB
677 ieee80211_key_replace(key->sdata, key->sta,
678 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
679 key, NULL);
6d10e46b 680 __ieee80211_key_destroy(key, true);
c8782078 681 }
6d10e46b
JB
682
683 mutex_unlock(&local->key_mtx);
684}
685
8d1f7ecd
JB
686void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
687{
688 struct ieee80211_sub_if_data *sdata;
689
690 sdata = container_of(wk, struct ieee80211_sub_if_data,
691 dec_tailroom_needed_wk.work);
692
693 /*
694 * The reason for the delayed tailroom needed decrementing is to
695 * make roaming faster: during roaming, all keys are first deleted
696 * and then new keys are installed. The first new key causes the
697 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
698 * the cost of synchronize_net() (which can be slow). Avoid this
699 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
700 * key removal for a while, so if we roam the value is larger than
701 * zero and no 0->1 transition happens.
702 *
703 * The cost is that if the AP switching was from an AP with keys
704 * to one without, we still allocate tailroom while it would no
705 * longer be needed. However, in the typical (fast) roaming case
706 * within an ESS this usually won't happen.
707 */
708
709 mutex_lock(&sdata->local->key_mtx);
710 sdata->crypto_tx_tailroom_needed_cnt -=
711 sdata->crypto_tx_tailroom_pending_dec;
712 sdata->crypto_tx_tailroom_pending_dec = 0;
713 mutex_unlock(&sdata->local->key_mtx);
714}
c68f4b89
JB
715
716void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
717 const u8 *replay_ctr, gfp_t gfp)
718{
719 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
720
721 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
722
723 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
724}
725EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
3ea542d3
JB
726
727void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
728 struct ieee80211_key_seq *seq)
729{
730 struct ieee80211_key *key;
731 u64 pn64;
732
733 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
734 return;
735
736 key = container_of(keyconf, struct ieee80211_key, conf);
737
738 switch (key->conf.cipher) {
739 case WLAN_CIPHER_SUITE_TKIP:
740 seq->tkip.iv32 = key->u.tkip.tx.iv32;
741 seq->tkip.iv16 = key->u.tkip.tx.iv16;
742 break;
743 case WLAN_CIPHER_SUITE_CCMP:
744 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
745 seq->ccmp.pn[5] = pn64;
746 seq->ccmp.pn[4] = pn64 >> 8;
747 seq->ccmp.pn[3] = pn64 >> 16;
748 seq->ccmp.pn[2] = pn64 >> 24;
749 seq->ccmp.pn[1] = pn64 >> 32;
750 seq->ccmp.pn[0] = pn64 >> 40;
751 break;
752 case WLAN_CIPHER_SUITE_AES_CMAC:
753 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
754 seq->ccmp.pn[5] = pn64;
755 seq->ccmp.pn[4] = pn64 >> 8;
756 seq->ccmp.pn[3] = pn64 >> 16;
757 seq->ccmp.pn[2] = pn64 >> 24;
758 seq->ccmp.pn[1] = pn64 >> 32;
759 seq->ccmp.pn[0] = pn64 >> 40;
760 break;
761 default:
762 WARN_ON(1);
763 }
764}
765EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
766
767void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
768 int tid, struct ieee80211_key_seq *seq)
769{
770 struct ieee80211_key *key;
771 const u8 *pn;
772
773 key = container_of(keyconf, struct ieee80211_key, conf);
774
775 switch (key->conf.cipher) {
776 case WLAN_CIPHER_SUITE_TKIP:
5a306f58 777 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
3ea542d3
JB
778 return;
779 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
780 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
781 break;
782 case WLAN_CIPHER_SUITE_CCMP:
5a306f58 783 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
3ea542d3
JB
784 return;
785 if (tid < 0)
5a306f58 786 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
3ea542d3
JB
787 else
788 pn = key->u.ccmp.rx_pn[tid];
4325f6ca 789 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
3ea542d3
JB
790 break;
791 case WLAN_CIPHER_SUITE_AES_CMAC:
792 if (WARN_ON(tid != 0))
793 return;
794 pn = key->u.aes_cmac.rx_pn;
4325f6ca 795 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
3ea542d3
JB
796 break;
797 }
798}
799EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
27b3eb9c
JB
800
801void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
802 struct ieee80211_key_seq *seq)
803{
804 struct ieee80211_key *key;
805 u64 pn64;
806
807 key = container_of(keyconf, struct ieee80211_key, conf);
808
809 switch (key->conf.cipher) {
810 case WLAN_CIPHER_SUITE_TKIP:
811 key->u.tkip.tx.iv32 = seq->tkip.iv32;
812 key->u.tkip.tx.iv16 = seq->tkip.iv16;
813 break;
814 case WLAN_CIPHER_SUITE_CCMP:
815 pn64 = (u64)seq->ccmp.pn[5] |
816 ((u64)seq->ccmp.pn[4] << 8) |
817 ((u64)seq->ccmp.pn[3] << 16) |
818 ((u64)seq->ccmp.pn[2] << 24) |
819 ((u64)seq->ccmp.pn[1] << 32) |
820 ((u64)seq->ccmp.pn[0] << 40);
821 atomic64_set(&key->u.ccmp.tx_pn, pn64);
822 break;
823 case WLAN_CIPHER_SUITE_AES_CMAC:
824 pn64 = (u64)seq->aes_cmac.pn[5] |
825 ((u64)seq->aes_cmac.pn[4] << 8) |
826 ((u64)seq->aes_cmac.pn[3] << 16) |
827 ((u64)seq->aes_cmac.pn[2] << 24) |
828 ((u64)seq->aes_cmac.pn[1] << 32) |
829 ((u64)seq->aes_cmac.pn[0] << 40);
830 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
831 break;
832 default:
833 WARN_ON(1);
834 break;
835 }
836}
837EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
838
839void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
840 int tid, struct ieee80211_key_seq *seq)
841{
842 struct ieee80211_key *key;
843 u8 *pn;
844
845 key = container_of(keyconf, struct ieee80211_key, conf);
846
847 switch (key->conf.cipher) {
848 case WLAN_CIPHER_SUITE_TKIP:
849 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
850 return;
851 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
852 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
853 break;
854 case WLAN_CIPHER_SUITE_CCMP:
855 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
856 return;
857 if (tid < 0)
858 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
859 else
860 pn = key->u.ccmp.rx_pn[tid];
861 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
862 break;
863 case WLAN_CIPHER_SUITE_AES_CMAC:
864 if (WARN_ON(tid != 0))
865 return;
866 pn = key->u.aes_cmac.rx_pn;
867 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
868 break;
869 default:
870 WARN_ON(1);
871 break;
872 }
873}
874EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
875
876void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
877{
878 struct ieee80211_key *key;
879
880 key = container_of(keyconf, struct ieee80211_key, conf);
881
882 assert_key_lock(key->local);
883
884 /*
885 * if key was uploaded, we assume the driver will/has remove(d)
886 * it, so adjust bookkeeping accordingly
887 */
888 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
889 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
890
1e359a5d
JB
891 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
892 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
893 (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
27b3eb9c
JB
894 increment_tailroom_need_count(key->sdata);
895 }
896
897 ieee80211_key_free(key, false);
898}
899EXPORT_SYMBOL_GPL(ieee80211_remove_key);
900
901struct ieee80211_key_conf *
902ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
903 struct ieee80211_key_conf *keyconf)
904{
905 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
906 struct ieee80211_local *local = sdata->local;
907 struct ieee80211_key *key;
908 int err;
909
910 if (WARN_ON(!local->wowlan))
911 return ERR_PTR(-EINVAL);
912
913 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
914 return ERR_PTR(-EINVAL);
915
916 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
917 keyconf->keylen, keyconf->key,
2475b1cc 918 0, NULL, NULL);
27b3eb9c 919 if (IS_ERR(key))
c5dc164d 920 return ERR_CAST(key);
27b3eb9c
JB
921
922 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
923 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
924
925 err = ieee80211_key_link(key, sdata, NULL);
926 if (err)
927 return ERR_PTR(err);
928
929 return &key->conf;
930}
931EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);