2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
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.
13 #include <linux/if_ether.h>
14 #include <linux/etherdevice.h>
15 #include <linux/list.h>
16 #include <linux/rcupdate.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include <linux/export.h>
20 #include <net/mac80211.h>
21 #include <asm/unaligned.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "debugfs_key.h"
31 * DOC: Key handling basics
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.
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.
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.
53 static const u8 bcast_addr
[ETH_ALEN
] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
55 static void assert_key_lock(struct ieee80211_local
*local
)
57 lockdep_assert_held(&local
->key_mtx
);
60 static void increment_tailroom_need_count(struct ieee80211_sub_if_data
*sdata
)
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:
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.
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
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)
77 * Solution has been explained at
78 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
81 if (!sdata
->crypto_tx_tailroom_needed_cnt
++) {
83 * Flush all XMIT packets currently using HW encryption or no
84 * encryption at all if the count transition is from 0 -> 1.
90 static int ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
92 struct ieee80211_sub_if_data
*sdata
;
94 int ret
= -EOPNOTSUPP
;
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
106 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
110 if (!key
->local
->ops
->set_key
)
111 goto out_unsupported
;
113 assert_key_lock(key
->local
);
118 * If this is a per-STA GTK, check if it
119 * is supported; if not, return.
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
;
125 if (sta
&& !sta
->uploaded
)
126 goto out_unsupported
;
129 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
) {
131 * The driver doesn't know anything about VLAN interfaces.
132 * Hence, don't send GTKs for VLAN interfaces to the driver.
134 if (!(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
135 goto out_unsupported
;
138 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
,
139 sta
? &sta
->sta
: NULL
, &key
->conf
);
142 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
144 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
145 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
146 sdata
->crypto_tx_tailroom_needed_cnt
--;
148 WARN_ON((key
->conf
.flags
& IEEE80211_KEY_FLAG_PUT_IV_SPACE
) &&
149 (key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
));
154 if (ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
&& ret
!= 1)
156 "failed to set key (%d, %pM) to hardware (%d)\n",
158 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
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_CCMP_256
:
167 case WLAN_CIPHER_SUITE_AES_CMAC
:
168 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
169 case WLAN_CIPHER_SUITE_GCMP
:
170 case WLAN_CIPHER_SUITE_GCMP_256
:
171 /* all of these we can do in software - if driver can */
174 if (key
->local
->hw
.flags
& IEEE80211_HW_SW_CRYPTO_CONTROL
)
182 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
184 struct ieee80211_sub_if_data
*sdata
;
185 struct sta_info
*sta
;
190 if (!key
|| !key
->local
->ops
->set_key
)
193 assert_key_lock(key
->local
);
195 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
201 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
202 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
203 increment_tailroom_need_count(sdata
);
205 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
206 sta
? &sta
->sta
: NULL
, &key
->conf
);
210 "failed to remove key (%d, %pM) from hardware (%d)\n",
212 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
214 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
217 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
218 int idx
, bool uni
, bool multi
)
220 struct ieee80211_key
*key
= NULL
;
222 assert_key_lock(sdata
->local
);
224 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
225 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
228 rcu_assign_pointer(sdata
->default_unicast_key
, key
);
229 drv_set_default_unicast_key(sdata
->local
, sdata
, idx
);
233 rcu_assign_pointer(sdata
->default_multicast_key
, key
);
235 ieee80211_debugfs_key_update_default(sdata
);
238 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
,
239 bool uni
, bool multi
)
241 mutex_lock(&sdata
->local
->key_mtx
);
242 __ieee80211_set_default_key(sdata
, idx
, uni
, multi
);
243 mutex_unlock(&sdata
->local
->key_mtx
);
247 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
249 struct ieee80211_key
*key
= NULL
;
251 assert_key_lock(sdata
->local
);
253 if (idx
>= NUM_DEFAULT_KEYS
&&
254 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
255 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
257 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
259 ieee80211_debugfs_key_update_default(sdata
);
262 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
265 mutex_lock(&sdata
->local
->key_mtx
);
266 __ieee80211_set_default_mgmt_key(sdata
, idx
);
267 mutex_unlock(&sdata
->local
->key_mtx
);
271 static void ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
272 struct sta_info
*sta
,
274 struct ieee80211_key
*old
,
275 struct ieee80211_key
*new)
278 bool defunikey
, defmultikey
, defmgmtkey
;
280 /* caller must provide at least one old/new */
281 if (WARN_ON(!new && !old
))
285 list_add_tail(&new->list
, &sdata
->key_list
);
287 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
290 idx
= old
->conf
.keyidx
;
292 idx
= new->conf
.keyidx
;
296 rcu_assign_pointer(sta
->ptk
[idx
], new);
299 rcu_assign_pointer(sta
->gtk
[idx
], new);
304 old
== key_mtx_dereference(sdata
->local
,
305 sdata
->default_unicast_key
);
307 old
== key_mtx_dereference(sdata
->local
,
308 sdata
->default_multicast_key
);
310 old
== key_mtx_dereference(sdata
->local
,
311 sdata
->default_mgmt_key
);
313 if (defunikey
&& !new)
314 __ieee80211_set_default_key(sdata
, -1, true, false);
315 if (defmultikey
&& !new)
316 __ieee80211_set_default_key(sdata
, -1, false, true);
317 if (defmgmtkey
&& !new)
318 __ieee80211_set_default_mgmt_key(sdata
, -1);
320 rcu_assign_pointer(sdata
->keys
[idx
], new);
321 if (defunikey
&& new)
322 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
324 if (defmultikey
&& new)
325 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
327 if (defmgmtkey
&& new)
328 __ieee80211_set_default_mgmt_key(sdata
,
333 list_del(&old
->list
);
336 struct ieee80211_key
*
337 ieee80211_key_alloc(u32 cipher
, int idx
, size_t key_len
,
339 size_t seq_len
, const u8
*seq
,
340 const struct ieee80211_cipher_scheme
*cs
)
342 struct ieee80211_key
*key
;
345 if (WARN_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
))
346 return ERR_PTR(-EINVAL
);
348 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
350 return ERR_PTR(-ENOMEM
);
353 * Default to software encryption; we'll later upload the
354 * key to the hardware if possible.
359 key
->conf
.cipher
= cipher
;
360 key
->conf
.keyidx
= idx
;
361 key
->conf
.keylen
= key_len
;
363 case WLAN_CIPHER_SUITE_WEP40
:
364 case WLAN_CIPHER_SUITE_WEP104
:
365 key
->conf
.iv_len
= IEEE80211_WEP_IV_LEN
;
366 key
->conf
.icv_len
= IEEE80211_WEP_ICV_LEN
;
368 case WLAN_CIPHER_SUITE_TKIP
:
369 key
->conf
.iv_len
= IEEE80211_TKIP_IV_LEN
;
370 key
->conf
.icv_len
= IEEE80211_TKIP_ICV_LEN
;
372 for (i
= 0; i
< IEEE80211_NUM_TIDS
; i
++) {
373 key
->u
.tkip
.rx
[i
].iv32
=
374 get_unaligned_le32(&seq
[2]);
375 key
->u
.tkip
.rx
[i
].iv16
=
376 get_unaligned_le16(seq
);
379 spin_lock_init(&key
->u
.tkip
.txlock
);
381 case WLAN_CIPHER_SUITE_CCMP
:
382 key
->conf
.iv_len
= IEEE80211_CCMP_HDR_LEN
;
383 key
->conf
.icv_len
= IEEE80211_CCMP_MIC_LEN
;
385 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
386 for (j
= 0; j
< IEEE80211_CCMP_PN_LEN
; j
++)
387 key
->u
.ccmp
.rx_pn
[i
][j
] =
388 seq
[IEEE80211_CCMP_PN_LEN
- j
- 1];
391 * Initialize AES key state here as an optimization so that
392 * it does not need to be initialized for every packet.
394 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
395 key_data
, key_len
, IEEE80211_CCMP_MIC_LEN
);
396 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
397 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
402 case WLAN_CIPHER_SUITE_CCMP_256
:
403 key
->conf
.iv_len
= IEEE80211_CCMP_256_HDR_LEN
;
404 key
->conf
.icv_len
= IEEE80211_CCMP_256_MIC_LEN
;
405 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
406 for (j
= 0; j
< IEEE80211_CCMP_256_PN_LEN
; j
++)
407 key
->u
.ccmp
.rx_pn
[i
][j
] =
408 seq
[IEEE80211_CCMP_256_PN_LEN
- j
- 1];
409 /* Initialize AES key state here as an optimization so that
410 * it does not need to be initialized for every packet.
412 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
413 key_data
, key_len
, IEEE80211_CCMP_256_MIC_LEN
);
414 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
415 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
420 case WLAN_CIPHER_SUITE_AES_CMAC
:
421 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
422 key
->conf
.iv_len
= 0;
423 if (cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
424 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
426 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie_16
);
428 for (j
= 0; j
< IEEE80211_CMAC_PN_LEN
; j
++)
429 key
->u
.aes_cmac
.rx_pn
[j
] =
430 seq
[IEEE80211_CMAC_PN_LEN
- j
- 1];
432 * Initialize AES key state here as an optimization so that
433 * it does not need to be initialized for every packet.
435 key
->u
.aes_cmac
.tfm
=
436 ieee80211_aes_cmac_key_setup(key_data
, key_len
);
437 if (IS_ERR(key
->u
.aes_cmac
.tfm
)) {
438 err
= PTR_ERR(key
->u
.aes_cmac
.tfm
);
443 case WLAN_CIPHER_SUITE_GCMP
:
444 case WLAN_CIPHER_SUITE_GCMP_256
:
445 key
->conf
.iv_len
= IEEE80211_GCMP_HDR_LEN
;
446 key
->conf
.icv_len
= IEEE80211_GCMP_MIC_LEN
;
447 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
448 for (j
= 0; j
< IEEE80211_GCMP_PN_LEN
; j
++)
449 key
->u
.gcmp
.rx_pn
[i
][j
] =
450 seq
[IEEE80211_GCMP_PN_LEN
- j
- 1];
451 /* Initialize AES key state here as an optimization so that
452 * it does not need to be initialized for every packet.
454 key
->u
.gcmp
.tfm
= ieee80211_aes_gcm_key_setup_encrypt(key_data
,
456 if (IS_ERR(key
->u
.gcmp
.tfm
)) {
457 err
= PTR_ERR(key
->u
.gcmp
.tfm
);
464 size_t len
= (seq_len
> MAX_PN_LEN
) ?
465 MAX_PN_LEN
: seq_len
;
467 key
->conf
.iv_len
= cs
->hdr_len
;
468 key
->conf
.icv_len
= cs
->mic_len
;
469 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
470 for (j
= 0; j
< len
; j
++)
471 key
->u
.gen
.rx_pn
[i
][j
] =
475 memcpy(key
->conf
.key
, key_data
, key_len
);
476 INIT_LIST_HEAD(&key
->list
);
481 static void ieee80211_key_free_common(struct ieee80211_key
*key
)
483 switch (key
->conf
.cipher
) {
484 case WLAN_CIPHER_SUITE_CCMP
:
485 case WLAN_CIPHER_SUITE_CCMP_256
:
486 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
488 case WLAN_CIPHER_SUITE_AES_CMAC
:
489 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
490 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
492 case WLAN_CIPHER_SUITE_GCMP
:
493 case WLAN_CIPHER_SUITE_GCMP_256
:
494 ieee80211_aes_gcm_key_free(key
->u
.gcmp
.tfm
);
500 static void __ieee80211_key_destroy(struct ieee80211_key
*key
,
504 ieee80211_key_disable_hw_accel(key
);
507 struct ieee80211_sub_if_data
*sdata
= key
->sdata
;
509 ieee80211_debugfs_key_remove(key
);
511 if (delay_tailroom
) {
512 /* see ieee80211_delayed_tailroom_dec */
513 sdata
->crypto_tx_tailroom_pending_dec
++;
514 schedule_delayed_work(&sdata
->dec_tailroom_needed_wk
,
517 sdata
->crypto_tx_tailroom_needed_cnt
--;
521 ieee80211_key_free_common(key
);
524 static void ieee80211_key_destroy(struct ieee80211_key
*key
,
531 * Synchronize so the TX path can no longer be using
532 * this key before we free/remove it.
536 __ieee80211_key_destroy(key
, delay_tailroom
);
539 void ieee80211_key_free_unused(struct ieee80211_key
*key
)
541 WARN_ON(key
->sdata
|| key
->local
);
542 ieee80211_key_free_common(key
);
545 int ieee80211_key_link(struct ieee80211_key
*key
,
546 struct ieee80211_sub_if_data
*sdata
,
547 struct sta_info
*sta
)
549 struct ieee80211_local
*local
= sdata
->local
;
550 struct ieee80211_key
*old_key
;
554 pairwise
= key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
;
555 idx
= key
->conf
.keyidx
;
556 key
->local
= sdata
->local
;
560 mutex_lock(&sdata
->local
->key_mtx
);
563 old_key
= key_mtx_dereference(sdata
->local
, sta
->ptk
[idx
]);
565 old_key
= key_mtx_dereference(sdata
->local
, sta
->gtk
[idx
]);
567 old_key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
569 increment_tailroom_need_count(sdata
);
571 ieee80211_key_replace(sdata
, sta
, pairwise
, old_key
, key
);
572 ieee80211_key_destroy(old_key
, true);
574 ieee80211_debugfs_key_add(key
);
576 if (!local
->wowlan
) {
577 ret
= ieee80211_key_enable_hw_accel(key
);
579 ieee80211_key_free(key
, true);
584 mutex_unlock(&sdata
->local
->key_mtx
);
589 void ieee80211_key_free(struct ieee80211_key
*key
, bool delay_tailroom
)
595 * Replace key with nothingness if it was ever used.
598 ieee80211_key_replace(key
->sdata
, key
->sta
,
599 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
601 ieee80211_key_destroy(key
, delay_tailroom
);
604 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
606 struct ieee80211_key
*key
;
610 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
613 mutex_lock(&sdata
->local
->key_mtx
);
615 sdata
->crypto_tx_tailroom_needed_cnt
= 0;
617 list_for_each_entry(key
, &sdata
->key_list
, list
) {
618 increment_tailroom_need_count(sdata
);
619 ieee80211_key_enable_hw_accel(key
);
622 mutex_unlock(&sdata
->local
->key_mtx
);
625 void ieee80211_iter_keys(struct ieee80211_hw
*hw
,
626 struct ieee80211_vif
*vif
,
627 void (*iter
)(struct ieee80211_hw
*hw
,
628 struct ieee80211_vif
*vif
,
629 struct ieee80211_sta
*sta
,
630 struct ieee80211_key_conf
*key
,
634 struct ieee80211_local
*local
= hw_to_local(hw
);
635 struct ieee80211_key
*key
, *tmp
;
636 struct ieee80211_sub_if_data
*sdata
;
640 mutex_lock(&local
->key_mtx
);
642 sdata
= vif_to_sdata(vif
);
643 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
644 iter(hw
, &sdata
->vif
,
645 key
->sta
? &key
->sta
->sta
: NULL
,
646 &key
->conf
, iter_data
);
648 list_for_each_entry(sdata
, &local
->interfaces
, list
)
649 list_for_each_entry_safe(key
, tmp
,
650 &sdata
->key_list
, list
)
651 iter(hw
, &sdata
->vif
,
652 key
->sta
? &key
->sta
->sta
: NULL
,
653 &key
->conf
, iter_data
);
655 mutex_unlock(&local
->key_mtx
);
657 EXPORT_SYMBOL(ieee80211_iter_keys
);
659 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data
*sdata
,
660 struct list_head
*keys
)
662 struct ieee80211_key
*key
, *tmp
;
664 sdata
->crypto_tx_tailroom_needed_cnt
-=
665 sdata
->crypto_tx_tailroom_pending_dec
;
666 sdata
->crypto_tx_tailroom_pending_dec
= 0;
668 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
670 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
) {
671 ieee80211_key_replace(key
->sdata
, key
->sta
,
672 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
674 list_add_tail(&key
->list
, keys
);
677 ieee80211_debugfs_key_update_default(sdata
);
680 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
,
681 bool force_synchronize
)
683 struct ieee80211_local
*local
= sdata
->local
;
684 struct ieee80211_sub_if_data
*vlan
;
685 struct ieee80211_key
*key
, *tmp
;
688 cancel_delayed_work_sync(&sdata
->dec_tailroom_needed_wk
);
690 mutex_lock(&local
->key_mtx
);
692 ieee80211_free_keys_iface(sdata
, &keys
);
694 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
695 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
696 ieee80211_free_keys_iface(vlan
, &keys
);
699 if (!list_empty(&keys
) || force_synchronize
)
701 list_for_each_entry_safe(key
, tmp
, &keys
, list
)
702 __ieee80211_key_destroy(key
, false);
704 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
||
705 sdata
->crypto_tx_tailroom_pending_dec
);
706 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
707 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
708 WARN_ON_ONCE(vlan
->crypto_tx_tailroom_needed_cnt
||
709 vlan
->crypto_tx_tailroom_pending_dec
);
712 mutex_unlock(&local
->key_mtx
);
715 void ieee80211_free_sta_keys(struct ieee80211_local
*local
,
716 struct sta_info
*sta
)
718 struct ieee80211_key
*key
;
721 mutex_lock(&local
->key_mtx
);
722 for (i
= 0; i
< ARRAY_SIZE(sta
->gtk
); i
++) {
723 key
= key_mtx_dereference(local
, sta
->gtk
[i
]);
726 ieee80211_key_replace(key
->sdata
, key
->sta
,
727 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
729 __ieee80211_key_destroy(key
, true);
732 for (i
= 0; i
< NUM_DEFAULT_KEYS
; i
++) {
733 key
= key_mtx_dereference(local
, sta
->ptk
[i
]);
736 ieee80211_key_replace(key
->sdata
, key
->sta
,
737 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
739 __ieee80211_key_destroy(key
, true);
742 mutex_unlock(&local
->key_mtx
);
745 void ieee80211_delayed_tailroom_dec(struct work_struct
*wk
)
747 struct ieee80211_sub_if_data
*sdata
;
749 sdata
= container_of(wk
, struct ieee80211_sub_if_data
,
750 dec_tailroom_needed_wk
.work
);
753 * The reason for the delayed tailroom needed decrementing is to
754 * make roaming faster: during roaming, all keys are first deleted
755 * and then new keys are installed. The first new key causes the
756 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
757 * the cost of synchronize_net() (which can be slow). Avoid this
758 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
759 * key removal for a while, so if we roam the value is larger than
760 * zero and no 0->1 transition happens.
762 * The cost is that if the AP switching was from an AP with keys
763 * to one without, we still allocate tailroom while it would no
764 * longer be needed. However, in the typical (fast) roaming case
765 * within an ESS this usually won't happen.
768 mutex_lock(&sdata
->local
->key_mtx
);
769 sdata
->crypto_tx_tailroom_needed_cnt
-=
770 sdata
->crypto_tx_tailroom_pending_dec
;
771 sdata
->crypto_tx_tailroom_pending_dec
= 0;
772 mutex_unlock(&sdata
->local
->key_mtx
);
775 void ieee80211_gtk_rekey_notify(struct ieee80211_vif
*vif
, const u8
*bssid
,
776 const u8
*replay_ctr
, gfp_t gfp
)
778 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
780 trace_api_gtk_rekey_notify(sdata
, bssid
, replay_ctr
);
782 cfg80211_gtk_rekey_notify(sdata
->dev
, bssid
, replay_ctr
, gfp
);
784 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify
);
786 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
787 struct ieee80211_key_seq
*seq
)
789 struct ieee80211_key
*key
;
792 if (WARN_ON(!(keyconf
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)))
795 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
797 switch (key
->conf
.cipher
) {
798 case WLAN_CIPHER_SUITE_TKIP
:
799 seq
->tkip
.iv32
= key
->u
.tkip
.tx
.iv32
;
800 seq
->tkip
.iv16
= key
->u
.tkip
.tx
.iv16
;
802 case WLAN_CIPHER_SUITE_CCMP
:
803 case WLAN_CIPHER_SUITE_CCMP_256
:
804 pn64
= atomic64_read(&key
->u
.ccmp
.tx_pn
);
805 seq
->ccmp
.pn
[5] = pn64
;
806 seq
->ccmp
.pn
[4] = pn64
>> 8;
807 seq
->ccmp
.pn
[3] = pn64
>> 16;
808 seq
->ccmp
.pn
[2] = pn64
>> 24;
809 seq
->ccmp
.pn
[1] = pn64
>> 32;
810 seq
->ccmp
.pn
[0] = pn64
>> 40;
812 case WLAN_CIPHER_SUITE_AES_CMAC
:
813 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
814 pn64
= atomic64_read(&key
->u
.aes_cmac
.tx_pn
);
815 seq
->ccmp
.pn
[5] = pn64
;
816 seq
->ccmp
.pn
[4] = pn64
>> 8;
817 seq
->ccmp
.pn
[3] = pn64
>> 16;
818 seq
->ccmp
.pn
[2] = pn64
>> 24;
819 seq
->ccmp
.pn
[1] = pn64
>> 32;
820 seq
->ccmp
.pn
[0] = pn64
>> 40;
822 case WLAN_CIPHER_SUITE_GCMP
:
823 case WLAN_CIPHER_SUITE_GCMP_256
:
824 pn64
= atomic64_read(&key
->u
.gcmp
.tx_pn
);
825 seq
->gcmp
.pn
[5] = pn64
;
826 seq
->gcmp
.pn
[4] = pn64
>> 8;
827 seq
->gcmp
.pn
[3] = pn64
>> 16;
828 seq
->gcmp
.pn
[2] = pn64
>> 24;
829 seq
->gcmp
.pn
[1] = pn64
>> 32;
830 seq
->gcmp
.pn
[0] = pn64
>> 40;
836 EXPORT_SYMBOL(ieee80211_get_key_tx_seq
);
838 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
839 int tid
, struct ieee80211_key_seq
*seq
)
841 struct ieee80211_key
*key
;
844 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
846 switch (key
->conf
.cipher
) {
847 case WLAN_CIPHER_SUITE_TKIP
:
848 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
850 seq
->tkip
.iv32
= key
->u
.tkip
.rx
[tid
].iv32
;
851 seq
->tkip
.iv16
= key
->u
.tkip
.rx
[tid
].iv16
;
853 case WLAN_CIPHER_SUITE_CCMP
:
854 case WLAN_CIPHER_SUITE_CCMP_256
:
855 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
858 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
860 pn
= key
->u
.ccmp
.rx_pn
[tid
];
861 memcpy(seq
->ccmp
.pn
, pn
, IEEE80211_CCMP_PN_LEN
);
863 case WLAN_CIPHER_SUITE_AES_CMAC
:
864 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
865 if (WARN_ON(tid
!= 0))
867 pn
= key
->u
.aes_cmac
.rx_pn
;
868 memcpy(seq
->aes_cmac
.pn
, pn
, IEEE80211_CMAC_PN_LEN
);
870 case WLAN_CIPHER_SUITE_GCMP
:
871 case WLAN_CIPHER_SUITE_GCMP_256
:
872 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
875 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
877 pn
= key
->u
.gcmp
.rx_pn
[tid
];
878 memcpy(seq
->gcmp
.pn
, pn
, IEEE80211_GCMP_PN_LEN
);
882 EXPORT_SYMBOL(ieee80211_get_key_rx_seq
);
884 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
885 struct ieee80211_key_seq
*seq
)
887 struct ieee80211_key
*key
;
890 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
892 switch (key
->conf
.cipher
) {
893 case WLAN_CIPHER_SUITE_TKIP
:
894 key
->u
.tkip
.tx
.iv32
= seq
->tkip
.iv32
;
895 key
->u
.tkip
.tx
.iv16
= seq
->tkip
.iv16
;
897 case WLAN_CIPHER_SUITE_CCMP
:
898 case WLAN_CIPHER_SUITE_CCMP_256
:
899 pn64
= (u64
)seq
->ccmp
.pn
[5] |
900 ((u64
)seq
->ccmp
.pn
[4] << 8) |
901 ((u64
)seq
->ccmp
.pn
[3] << 16) |
902 ((u64
)seq
->ccmp
.pn
[2] << 24) |
903 ((u64
)seq
->ccmp
.pn
[1] << 32) |
904 ((u64
)seq
->ccmp
.pn
[0] << 40);
905 atomic64_set(&key
->u
.ccmp
.tx_pn
, pn64
);
907 case WLAN_CIPHER_SUITE_AES_CMAC
:
908 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
909 pn64
= (u64
)seq
->aes_cmac
.pn
[5] |
910 ((u64
)seq
->aes_cmac
.pn
[4] << 8) |
911 ((u64
)seq
->aes_cmac
.pn
[3] << 16) |
912 ((u64
)seq
->aes_cmac
.pn
[2] << 24) |
913 ((u64
)seq
->aes_cmac
.pn
[1] << 32) |
914 ((u64
)seq
->aes_cmac
.pn
[0] << 40);
915 atomic64_set(&key
->u
.aes_cmac
.tx_pn
, pn64
);
917 case WLAN_CIPHER_SUITE_GCMP
:
918 case WLAN_CIPHER_SUITE_GCMP_256
:
919 pn64
= (u64
)seq
->gcmp
.pn
[5] |
920 ((u64
)seq
->gcmp
.pn
[4] << 8) |
921 ((u64
)seq
->gcmp
.pn
[3] << 16) |
922 ((u64
)seq
->gcmp
.pn
[2] << 24) |
923 ((u64
)seq
->gcmp
.pn
[1] << 32) |
924 ((u64
)seq
->gcmp
.pn
[0] << 40);
925 atomic64_set(&key
->u
.gcmp
.tx_pn
, pn64
);
932 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq
);
934 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
935 int tid
, struct ieee80211_key_seq
*seq
)
937 struct ieee80211_key
*key
;
940 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
942 switch (key
->conf
.cipher
) {
943 case WLAN_CIPHER_SUITE_TKIP
:
944 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
946 key
->u
.tkip
.rx
[tid
].iv32
= seq
->tkip
.iv32
;
947 key
->u
.tkip
.rx
[tid
].iv16
= seq
->tkip
.iv16
;
949 case WLAN_CIPHER_SUITE_CCMP
:
950 case WLAN_CIPHER_SUITE_CCMP_256
:
951 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
954 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
956 pn
= key
->u
.ccmp
.rx_pn
[tid
];
957 memcpy(pn
, seq
->ccmp
.pn
, IEEE80211_CCMP_PN_LEN
);
959 case WLAN_CIPHER_SUITE_AES_CMAC
:
960 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
961 if (WARN_ON(tid
!= 0))
963 pn
= key
->u
.aes_cmac
.rx_pn
;
964 memcpy(pn
, seq
->aes_cmac
.pn
, IEEE80211_CMAC_PN_LEN
);
966 case WLAN_CIPHER_SUITE_GCMP
:
967 case WLAN_CIPHER_SUITE_GCMP_256
:
968 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
971 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
973 pn
= key
->u
.gcmp
.rx_pn
[tid
];
974 memcpy(pn
, seq
->gcmp
.pn
, IEEE80211_GCMP_PN_LEN
);
981 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq
);
983 void ieee80211_remove_key(struct ieee80211_key_conf
*keyconf
)
985 struct ieee80211_key
*key
;
987 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
989 assert_key_lock(key
->local
);
992 * if key was uploaded, we assume the driver will/has remove(d)
993 * it, so adjust bookkeeping accordingly
995 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) {
996 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
998 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
999 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
1000 increment_tailroom_need_count(key
->sdata
);
1003 ieee80211_key_free(key
, false);
1005 EXPORT_SYMBOL_GPL(ieee80211_remove_key
);
1007 struct ieee80211_key_conf
*
1008 ieee80211_gtk_rekey_add(struct ieee80211_vif
*vif
,
1009 struct ieee80211_key_conf
*keyconf
)
1011 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
1012 struct ieee80211_local
*local
= sdata
->local
;
1013 struct ieee80211_key
*key
;
1016 if (WARN_ON(!local
->wowlan
))
1017 return ERR_PTR(-EINVAL
);
1019 if (WARN_ON(vif
->type
!= NL80211_IFTYPE_STATION
))
1020 return ERR_PTR(-EINVAL
);
1022 key
= ieee80211_key_alloc(keyconf
->cipher
, keyconf
->keyidx
,
1023 keyconf
->keylen
, keyconf
->key
,
1026 return ERR_CAST(key
);
1028 if (sdata
->u
.mgd
.mfp
!= IEEE80211_MFP_DISABLED
)
1029 key
->conf
.flags
|= IEEE80211_KEY_FLAG_RX_MGMT
;
1031 err
= ieee80211_key_link(key
, sdata
, NULL
);
1033 return ERR_PTR(err
);
1037 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add
);