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