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"
32 * DOC: Key handling basics
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.
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.
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.
54 static const u8 bcast_addr
[ETH_ALEN
] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
56 static void assert_key_lock(struct ieee80211_local
*local
)
58 lockdep_assert_held(&local
->key_mtx
);
61 static void increment_tailroom_need_count(struct ieee80211_sub_if_data
*sdata
)
64 * When this count is zero, SKB resizing for allocating tailroom
65 * for IV or MMIC is skipped. But, this check has created two race
66 * cases in xmit path while transiting from zero count to one:
68 * 1. SKB resize was skipped because no key was added but just before
69 * the xmit key is added and SW encryption kicks off.
71 * 2. SKB resize was skipped because all the keys were hw planted but
72 * just before xmit one of the key is deleted and SW encryption kicks
75 * In both the above case SW encryption will find not enough space for
76 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
78 * Solution has been explained at
79 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
82 if (!sdata
->crypto_tx_tailroom_needed_cnt
++) {
84 * Flush all XMIT packets currently using HW encryption or no
85 * encryption at all if the count transition is from 0 -> 1.
91 static int ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
93 struct ieee80211_sub_if_data
*sdata
;
95 int ret
= -EOPNOTSUPP
;
99 if (key
->flags
& KEY_FLAG_TAINTED
) {
100 /* If we get here, it's during resume and the key is
101 * tainted so shouldn't be used/programmed any more.
102 * However, its flags may still indicate that it was
103 * programmed into the device (since we're in resume)
104 * so clear that flag now to avoid trying to remove
107 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
111 if (!key
->local
->ops
->set_key
)
112 goto out_unsupported
;
114 assert_key_lock(key
->local
);
119 * If this is a per-STA GTK, check if it
120 * is supported; if not, return.
122 if (sta
&& !(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
) &&
123 !(key
->local
->hw
.flags
& IEEE80211_HW_SUPPORTS_PER_STA_GTK
))
124 goto out_unsupported
;
126 if (sta
&& !sta
->uploaded
)
127 goto out_unsupported
;
130 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
) {
132 * The driver doesn't know anything about VLAN interfaces.
133 * Hence, don't send GTKs for VLAN interfaces to the driver.
135 if (!(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
136 goto out_unsupported
;
139 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
,
140 sta
? &sta
->sta
: NULL
, &key
->conf
);
143 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
145 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
146 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
147 sdata
->crypto_tx_tailroom_needed_cnt
--;
149 WARN_ON((key
->conf
.flags
& IEEE80211_KEY_FLAG_PUT_IV_SPACE
) &&
150 (key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
));
155 if (ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
&& ret
!= 1)
157 "failed to set key (%d, %pM) to hardware (%d)\n",
159 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
162 switch (key
->conf
.cipher
) {
163 case WLAN_CIPHER_SUITE_WEP40
:
164 case WLAN_CIPHER_SUITE_WEP104
:
165 case WLAN_CIPHER_SUITE_TKIP
:
166 case WLAN_CIPHER_SUITE_CCMP
:
167 case WLAN_CIPHER_SUITE_CCMP_256
:
168 case WLAN_CIPHER_SUITE_AES_CMAC
:
169 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
170 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
171 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
172 case WLAN_CIPHER_SUITE_GCMP
:
173 case WLAN_CIPHER_SUITE_GCMP_256
:
174 /* all of these we can do in software - if driver can */
177 if (key
->local
->hw
.flags
& IEEE80211_HW_SW_CRYPTO_CONTROL
)
185 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
187 struct ieee80211_sub_if_data
*sdata
;
188 struct sta_info
*sta
;
193 if (!key
|| !key
->local
->ops
->set_key
)
196 assert_key_lock(key
->local
);
198 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
204 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
205 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
206 increment_tailroom_need_count(sdata
);
208 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
209 sta
? &sta
->sta
: NULL
, &key
->conf
);
213 "failed to remove key (%d, %pM) from hardware (%d)\n",
215 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
217 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
220 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
221 int idx
, bool uni
, bool multi
)
223 struct ieee80211_key
*key
= NULL
;
225 assert_key_lock(sdata
->local
);
227 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
228 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
231 rcu_assign_pointer(sdata
->default_unicast_key
, key
);
232 ieee80211_check_fast_xmit_iface(sdata
);
233 drv_set_default_unicast_key(sdata
->local
, sdata
, idx
);
237 rcu_assign_pointer(sdata
->default_multicast_key
, key
);
239 ieee80211_debugfs_key_update_default(sdata
);
242 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
,
243 bool uni
, bool multi
)
245 mutex_lock(&sdata
->local
->key_mtx
);
246 __ieee80211_set_default_key(sdata
, idx
, uni
, multi
);
247 mutex_unlock(&sdata
->local
->key_mtx
);
251 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
253 struct ieee80211_key
*key
= NULL
;
255 assert_key_lock(sdata
->local
);
257 if (idx
>= NUM_DEFAULT_KEYS
&&
258 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
259 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
261 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
263 ieee80211_debugfs_key_update_default(sdata
);
266 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
269 mutex_lock(&sdata
->local
->key_mtx
);
270 __ieee80211_set_default_mgmt_key(sdata
, idx
);
271 mutex_unlock(&sdata
->local
->key_mtx
);
275 static void ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
276 struct sta_info
*sta
,
278 struct ieee80211_key
*old
,
279 struct ieee80211_key
*new)
282 bool defunikey
, defmultikey
, defmgmtkey
;
284 /* caller must provide at least one old/new */
285 if (WARN_ON(!new && !old
))
289 list_add_tail(&new->list
, &sdata
->key_list
);
291 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
294 idx
= old
->conf
.keyidx
;
296 idx
= new->conf
.keyidx
;
300 rcu_assign_pointer(sta
->ptk
[idx
], new);
302 ieee80211_check_fast_xmit(sta
);
304 rcu_assign_pointer(sta
->gtk
[idx
], new);
309 old
== key_mtx_dereference(sdata
->local
,
310 sdata
->default_unicast_key
);
312 old
== key_mtx_dereference(sdata
->local
,
313 sdata
->default_multicast_key
);
315 old
== key_mtx_dereference(sdata
->local
,
316 sdata
->default_mgmt_key
);
318 if (defunikey
&& !new)
319 __ieee80211_set_default_key(sdata
, -1, true, false);
320 if (defmultikey
&& !new)
321 __ieee80211_set_default_key(sdata
, -1, false, true);
322 if (defmgmtkey
&& !new)
323 __ieee80211_set_default_mgmt_key(sdata
, -1);
325 rcu_assign_pointer(sdata
->keys
[idx
], new);
326 if (defunikey
&& new)
327 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
329 if (defmultikey
&& new)
330 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
332 if (defmgmtkey
&& new)
333 __ieee80211_set_default_mgmt_key(sdata
,
338 list_del(&old
->list
);
341 struct ieee80211_key
*
342 ieee80211_key_alloc(u32 cipher
, int idx
, size_t key_len
,
344 size_t seq_len
, const u8
*seq
,
345 const struct ieee80211_cipher_scheme
*cs
)
347 struct ieee80211_key
*key
;
350 if (WARN_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
))
351 return ERR_PTR(-EINVAL
);
353 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
355 return ERR_PTR(-ENOMEM
);
358 * Default to software encryption; we'll later upload the
359 * key to the hardware if possible.
364 key
->conf
.cipher
= cipher
;
365 key
->conf
.keyidx
= idx
;
366 key
->conf
.keylen
= key_len
;
368 case WLAN_CIPHER_SUITE_WEP40
:
369 case WLAN_CIPHER_SUITE_WEP104
:
370 key
->conf
.iv_len
= IEEE80211_WEP_IV_LEN
;
371 key
->conf
.icv_len
= IEEE80211_WEP_ICV_LEN
;
373 case WLAN_CIPHER_SUITE_TKIP
:
374 key
->conf
.iv_len
= IEEE80211_TKIP_IV_LEN
;
375 key
->conf
.icv_len
= IEEE80211_TKIP_ICV_LEN
;
377 for (i
= 0; i
< IEEE80211_NUM_TIDS
; i
++) {
378 key
->u
.tkip
.rx
[i
].iv32
=
379 get_unaligned_le32(&seq
[2]);
380 key
->u
.tkip
.rx
[i
].iv16
=
381 get_unaligned_le16(seq
);
384 spin_lock_init(&key
->u
.tkip
.txlock
);
386 case WLAN_CIPHER_SUITE_CCMP
:
387 key
->conf
.iv_len
= IEEE80211_CCMP_HDR_LEN
;
388 key
->conf
.icv_len
= IEEE80211_CCMP_MIC_LEN
;
390 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
391 for (j
= 0; j
< IEEE80211_CCMP_PN_LEN
; j
++)
392 key
->u
.ccmp
.rx_pn
[i
][j
] =
393 seq
[IEEE80211_CCMP_PN_LEN
- j
- 1];
396 * Initialize AES key state here as an optimization so that
397 * it does not need to be initialized for every packet.
399 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
400 key_data
, key_len
, IEEE80211_CCMP_MIC_LEN
);
401 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
402 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
407 case WLAN_CIPHER_SUITE_CCMP_256
:
408 key
->conf
.iv_len
= IEEE80211_CCMP_256_HDR_LEN
;
409 key
->conf
.icv_len
= IEEE80211_CCMP_256_MIC_LEN
;
410 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
411 for (j
= 0; j
< IEEE80211_CCMP_256_PN_LEN
; j
++)
412 key
->u
.ccmp
.rx_pn
[i
][j
] =
413 seq
[IEEE80211_CCMP_256_PN_LEN
- j
- 1];
414 /* Initialize AES key state here as an optimization so that
415 * it does not need to be initialized for every packet.
417 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
418 key_data
, key_len
, IEEE80211_CCMP_256_MIC_LEN
);
419 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
420 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
425 case WLAN_CIPHER_SUITE_AES_CMAC
:
426 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
427 key
->conf
.iv_len
= 0;
428 if (cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
429 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
431 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie_16
);
433 for (j
= 0; j
< IEEE80211_CMAC_PN_LEN
; j
++)
434 key
->u
.aes_cmac
.rx_pn
[j
] =
435 seq
[IEEE80211_CMAC_PN_LEN
- j
- 1];
437 * Initialize AES key state here as an optimization so that
438 * it does not need to be initialized for every packet.
440 key
->u
.aes_cmac
.tfm
=
441 ieee80211_aes_cmac_key_setup(key_data
, key_len
);
442 if (IS_ERR(key
->u
.aes_cmac
.tfm
)) {
443 err
= PTR_ERR(key
->u
.aes_cmac
.tfm
);
448 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
449 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
450 key
->conf
.iv_len
= 0;
451 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie_16
);
453 for (j
= 0; j
< IEEE80211_GMAC_PN_LEN
; j
++)
454 key
->u
.aes_gmac
.rx_pn
[j
] =
455 seq
[IEEE80211_GMAC_PN_LEN
- j
- 1];
456 /* Initialize AES key state here as an optimization so that
457 * it does not need to be initialized for every packet.
459 key
->u
.aes_gmac
.tfm
=
460 ieee80211_aes_gmac_key_setup(key_data
, key_len
);
461 if (IS_ERR(key
->u
.aes_gmac
.tfm
)) {
462 err
= PTR_ERR(key
->u
.aes_gmac
.tfm
);
467 case WLAN_CIPHER_SUITE_GCMP
:
468 case WLAN_CIPHER_SUITE_GCMP_256
:
469 key
->conf
.iv_len
= IEEE80211_GCMP_HDR_LEN
;
470 key
->conf
.icv_len
= IEEE80211_GCMP_MIC_LEN
;
471 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
472 for (j
= 0; j
< IEEE80211_GCMP_PN_LEN
; j
++)
473 key
->u
.gcmp
.rx_pn
[i
][j
] =
474 seq
[IEEE80211_GCMP_PN_LEN
- j
- 1];
475 /* Initialize AES key state here as an optimization so that
476 * it does not need to be initialized for every packet.
478 key
->u
.gcmp
.tfm
= ieee80211_aes_gcm_key_setup_encrypt(key_data
,
480 if (IS_ERR(key
->u
.gcmp
.tfm
)) {
481 err
= PTR_ERR(key
->u
.gcmp
.tfm
);
488 if (seq_len
&& seq_len
!= cs
->pn_len
) {
490 return ERR_PTR(-EINVAL
);
493 key
->conf
.iv_len
= cs
->hdr_len
;
494 key
->conf
.icv_len
= cs
->mic_len
;
495 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
496 for (j
= 0; j
< seq_len
; j
++)
497 key
->u
.gen
.rx_pn
[i
][j
] =
498 seq
[seq_len
- j
- 1];
499 key
->flags
|= KEY_FLAG_CIPHER_SCHEME
;
502 memcpy(key
->conf
.key
, key_data
, key_len
);
503 INIT_LIST_HEAD(&key
->list
);
508 static void ieee80211_key_free_common(struct ieee80211_key
*key
)
510 switch (key
->conf
.cipher
) {
511 case WLAN_CIPHER_SUITE_CCMP
:
512 case WLAN_CIPHER_SUITE_CCMP_256
:
513 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
515 case WLAN_CIPHER_SUITE_AES_CMAC
:
516 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
517 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
519 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
520 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
521 ieee80211_aes_gmac_key_free(key
->u
.aes_gmac
.tfm
);
523 case WLAN_CIPHER_SUITE_GCMP
:
524 case WLAN_CIPHER_SUITE_GCMP_256
:
525 ieee80211_aes_gcm_key_free(key
->u
.gcmp
.tfm
);
531 static void __ieee80211_key_destroy(struct ieee80211_key
*key
,
535 ieee80211_key_disable_hw_accel(key
);
538 struct ieee80211_sub_if_data
*sdata
= key
->sdata
;
540 ieee80211_debugfs_key_remove(key
);
542 if (delay_tailroom
) {
543 /* see ieee80211_delayed_tailroom_dec */
544 sdata
->crypto_tx_tailroom_pending_dec
++;
545 schedule_delayed_work(&sdata
->dec_tailroom_needed_wk
,
548 sdata
->crypto_tx_tailroom_needed_cnt
--;
552 ieee80211_key_free_common(key
);
555 static void ieee80211_key_destroy(struct ieee80211_key
*key
,
562 * Synchronize so the TX path can no longer be using
563 * this key before we free/remove it.
567 __ieee80211_key_destroy(key
, delay_tailroom
);
570 void ieee80211_key_free_unused(struct ieee80211_key
*key
)
572 WARN_ON(key
->sdata
|| key
->local
);
573 ieee80211_key_free_common(key
);
576 int ieee80211_key_link(struct ieee80211_key
*key
,
577 struct ieee80211_sub_if_data
*sdata
,
578 struct sta_info
*sta
)
580 struct ieee80211_local
*local
= sdata
->local
;
581 struct ieee80211_key
*old_key
;
585 pairwise
= key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
;
586 idx
= key
->conf
.keyidx
;
587 key
->local
= sdata
->local
;
591 mutex_lock(&sdata
->local
->key_mtx
);
594 old_key
= key_mtx_dereference(sdata
->local
, sta
->ptk
[idx
]);
596 old_key
= key_mtx_dereference(sdata
->local
, sta
->gtk
[idx
]);
598 old_key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
600 increment_tailroom_need_count(sdata
);
602 ieee80211_key_replace(sdata
, sta
, pairwise
, old_key
, key
);
603 ieee80211_key_destroy(old_key
, true);
605 ieee80211_debugfs_key_add(key
);
607 if (!local
->wowlan
) {
608 ret
= ieee80211_key_enable_hw_accel(key
);
610 ieee80211_key_free(key
, true);
615 mutex_unlock(&sdata
->local
->key_mtx
);
620 void ieee80211_key_free(struct ieee80211_key
*key
, bool delay_tailroom
)
626 * Replace key with nothingness if it was ever used.
629 ieee80211_key_replace(key
->sdata
, key
->sta
,
630 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
632 ieee80211_key_destroy(key
, delay_tailroom
);
635 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
637 struct ieee80211_key
*key
;
641 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
644 mutex_lock(&sdata
->local
->key_mtx
);
646 sdata
->crypto_tx_tailroom_needed_cnt
= 0;
648 list_for_each_entry(key
, &sdata
->key_list
, list
) {
649 increment_tailroom_need_count(sdata
);
650 ieee80211_key_enable_hw_accel(key
);
653 mutex_unlock(&sdata
->local
->key_mtx
);
656 void ieee80211_iter_keys(struct ieee80211_hw
*hw
,
657 struct ieee80211_vif
*vif
,
658 void (*iter
)(struct ieee80211_hw
*hw
,
659 struct ieee80211_vif
*vif
,
660 struct ieee80211_sta
*sta
,
661 struct ieee80211_key_conf
*key
,
665 struct ieee80211_local
*local
= hw_to_local(hw
);
666 struct ieee80211_key
*key
, *tmp
;
667 struct ieee80211_sub_if_data
*sdata
;
671 mutex_lock(&local
->key_mtx
);
673 sdata
= vif_to_sdata(vif
);
674 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
675 iter(hw
, &sdata
->vif
,
676 key
->sta
? &key
->sta
->sta
: NULL
,
677 &key
->conf
, iter_data
);
679 list_for_each_entry(sdata
, &local
->interfaces
, list
)
680 list_for_each_entry_safe(key
, tmp
,
681 &sdata
->key_list
, list
)
682 iter(hw
, &sdata
->vif
,
683 key
->sta
? &key
->sta
->sta
: NULL
,
684 &key
->conf
, iter_data
);
686 mutex_unlock(&local
->key_mtx
);
688 EXPORT_SYMBOL(ieee80211_iter_keys
);
690 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data
*sdata
,
691 struct list_head
*keys
)
693 struct ieee80211_key
*key
, *tmp
;
695 sdata
->crypto_tx_tailroom_needed_cnt
-=
696 sdata
->crypto_tx_tailroom_pending_dec
;
697 sdata
->crypto_tx_tailroom_pending_dec
= 0;
699 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
701 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
) {
702 ieee80211_key_replace(key
->sdata
, key
->sta
,
703 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
705 list_add_tail(&key
->list
, keys
);
708 ieee80211_debugfs_key_update_default(sdata
);
711 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
,
712 bool force_synchronize
)
714 struct ieee80211_local
*local
= sdata
->local
;
715 struct ieee80211_sub_if_data
*vlan
;
716 struct ieee80211_key
*key
, *tmp
;
719 cancel_delayed_work_sync(&sdata
->dec_tailroom_needed_wk
);
721 mutex_lock(&local
->key_mtx
);
723 ieee80211_free_keys_iface(sdata
, &keys
);
725 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
726 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
727 ieee80211_free_keys_iface(vlan
, &keys
);
730 if (!list_empty(&keys
) || force_synchronize
)
732 list_for_each_entry_safe(key
, tmp
, &keys
, list
)
733 __ieee80211_key_destroy(key
, false);
735 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
||
736 sdata
->crypto_tx_tailroom_pending_dec
);
737 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
738 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
739 WARN_ON_ONCE(vlan
->crypto_tx_tailroom_needed_cnt
||
740 vlan
->crypto_tx_tailroom_pending_dec
);
743 mutex_unlock(&local
->key_mtx
);
746 void ieee80211_free_sta_keys(struct ieee80211_local
*local
,
747 struct sta_info
*sta
)
749 struct ieee80211_key
*key
;
752 mutex_lock(&local
->key_mtx
);
753 for (i
= 0; i
< ARRAY_SIZE(sta
->gtk
); i
++) {
754 key
= key_mtx_dereference(local
, sta
->gtk
[i
]);
757 ieee80211_key_replace(key
->sdata
, key
->sta
,
758 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
760 __ieee80211_key_destroy(key
, true);
763 for (i
= 0; i
< NUM_DEFAULT_KEYS
; i
++) {
764 key
= key_mtx_dereference(local
, sta
->ptk
[i
]);
767 ieee80211_key_replace(key
->sdata
, key
->sta
,
768 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
770 __ieee80211_key_destroy(key
, true);
773 mutex_unlock(&local
->key_mtx
);
776 void ieee80211_delayed_tailroom_dec(struct work_struct
*wk
)
778 struct ieee80211_sub_if_data
*sdata
;
780 sdata
= container_of(wk
, struct ieee80211_sub_if_data
,
781 dec_tailroom_needed_wk
.work
);
784 * The reason for the delayed tailroom needed decrementing is to
785 * make roaming faster: during roaming, all keys are first deleted
786 * and then new keys are installed. The first new key causes the
787 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
788 * the cost of synchronize_net() (which can be slow). Avoid this
789 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
790 * key removal for a while, so if we roam the value is larger than
791 * zero and no 0->1 transition happens.
793 * The cost is that if the AP switching was from an AP with keys
794 * to one without, we still allocate tailroom while it would no
795 * longer be needed. However, in the typical (fast) roaming case
796 * within an ESS this usually won't happen.
799 mutex_lock(&sdata
->local
->key_mtx
);
800 sdata
->crypto_tx_tailroom_needed_cnt
-=
801 sdata
->crypto_tx_tailroom_pending_dec
;
802 sdata
->crypto_tx_tailroom_pending_dec
= 0;
803 mutex_unlock(&sdata
->local
->key_mtx
);
806 void ieee80211_gtk_rekey_notify(struct ieee80211_vif
*vif
, const u8
*bssid
,
807 const u8
*replay_ctr
, gfp_t gfp
)
809 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
811 trace_api_gtk_rekey_notify(sdata
, bssid
, replay_ctr
);
813 cfg80211_gtk_rekey_notify(sdata
->dev
, bssid
, replay_ctr
, gfp
);
815 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify
);
817 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
818 struct ieee80211_key_seq
*seq
)
820 struct ieee80211_key
*key
;
823 if (WARN_ON(!(keyconf
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)))
826 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
828 switch (key
->conf
.cipher
) {
829 case WLAN_CIPHER_SUITE_TKIP
:
830 seq
->tkip
.iv32
= key
->u
.tkip
.tx
.iv32
;
831 seq
->tkip
.iv16
= key
->u
.tkip
.tx
.iv16
;
833 case WLAN_CIPHER_SUITE_CCMP
:
834 case WLAN_CIPHER_SUITE_CCMP_256
:
835 case WLAN_CIPHER_SUITE_AES_CMAC
:
836 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
837 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
838 offsetof(typeof(*seq
), aes_cmac
));
839 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
840 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
841 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
842 offsetof(typeof(*seq
), aes_gmac
));
843 case WLAN_CIPHER_SUITE_GCMP
:
844 case WLAN_CIPHER_SUITE_GCMP_256
:
845 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
846 offsetof(typeof(*seq
), gcmp
));
847 pn64
= atomic64_read(&key
->conf
.tx_pn
);
848 seq
->ccmp
.pn
[5] = pn64
;
849 seq
->ccmp
.pn
[4] = pn64
>> 8;
850 seq
->ccmp
.pn
[3] = pn64
>> 16;
851 seq
->ccmp
.pn
[2] = pn64
>> 24;
852 seq
->ccmp
.pn
[1] = pn64
>> 32;
853 seq
->ccmp
.pn
[0] = pn64
>> 40;
859 EXPORT_SYMBOL(ieee80211_get_key_tx_seq
);
861 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
862 int tid
, struct ieee80211_key_seq
*seq
)
864 struct ieee80211_key
*key
;
867 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
869 switch (key
->conf
.cipher
) {
870 case WLAN_CIPHER_SUITE_TKIP
:
871 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
873 seq
->tkip
.iv32
= key
->u
.tkip
.rx
[tid
].iv32
;
874 seq
->tkip
.iv16
= key
->u
.tkip
.rx
[tid
].iv16
;
876 case WLAN_CIPHER_SUITE_CCMP
:
877 case WLAN_CIPHER_SUITE_CCMP_256
:
878 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
881 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
883 pn
= key
->u
.ccmp
.rx_pn
[tid
];
884 memcpy(seq
->ccmp
.pn
, pn
, IEEE80211_CCMP_PN_LEN
);
886 case WLAN_CIPHER_SUITE_AES_CMAC
:
887 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
888 if (WARN_ON(tid
!= 0))
890 pn
= key
->u
.aes_cmac
.rx_pn
;
891 memcpy(seq
->aes_cmac
.pn
, pn
, IEEE80211_CMAC_PN_LEN
);
893 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
894 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
895 if (WARN_ON(tid
!= 0))
897 pn
= key
->u
.aes_gmac
.rx_pn
;
898 memcpy(seq
->aes_gmac
.pn
, pn
, IEEE80211_GMAC_PN_LEN
);
900 case WLAN_CIPHER_SUITE_GCMP
:
901 case WLAN_CIPHER_SUITE_GCMP_256
:
902 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
905 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
907 pn
= key
->u
.gcmp
.rx_pn
[tid
];
908 memcpy(seq
->gcmp
.pn
, pn
, IEEE80211_GCMP_PN_LEN
);
912 EXPORT_SYMBOL(ieee80211_get_key_rx_seq
);
914 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
915 struct ieee80211_key_seq
*seq
)
917 struct ieee80211_key
*key
;
920 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
922 switch (key
->conf
.cipher
) {
923 case WLAN_CIPHER_SUITE_TKIP
:
924 key
->u
.tkip
.tx
.iv32
= seq
->tkip
.iv32
;
925 key
->u
.tkip
.tx
.iv16
= seq
->tkip
.iv16
;
927 case WLAN_CIPHER_SUITE_CCMP
:
928 case WLAN_CIPHER_SUITE_CCMP_256
:
929 case WLAN_CIPHER_SUITE_AES_CMAC
:
930 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
931 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
932 offsetof(typeof(*seq
), aes_cmac
));
933 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
934 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
935 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
936 offsetof(typeof(*seq
), aes_gmac
));
937 case WLAN_CIPHER_SUITE_GCMP
:
938 case WLAN_CIPHER_SUITE_GCMP_256
:
939 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
940 offsetof(typeof(*seq
), gcmp
));
941 pn64
= (u64
)seq
->ccmp
.pn
[5] |
942 ((u64
)seq
->ccmp
.pn
[4] << 8) |
943 ((u64
)seq
->ccmp
.pn
[3] << 16) |
944 ((u64
)seq
->ccmp
.pn
[2] << 24) |
945 ((u64
)seq
->ccmp
.pn
[1] << 32) |
946 ((u64
)seq
->ccmp
.pn
[0] << 40);
947 atomic64_set(&key
->conf
.tx_pn
, pn64
);
954 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq
);
956 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
957 int tid
, struct ieee80211_key_seq
*seq
)
959 struct ieee80211_key
*key
;
962 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
964 switch (key
->conf
.cipher
) {
965 case WLAN_CIPHER_SUITE_TKIP
:
966 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
968 key
->u
.tkip
.rx
[tid
].iv32
= seq
->tkip
.iv32
;
969 key
->u
.tkip
.rx
[tid
].iv16
= seq
->tkip
.iv16
;
971 case WLAN_CIPHER_SUITE_CCMP
:
972 case WLAN_CIPHER_SUITE_CCMP_256
:
973 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
976 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
978 pn
= key
->u
.ccmp
.rx_pn
[tid
];
979 memcpy(pn
, seq
->ccmp
.pn
, IEEE80211_CCMP_PN_LEN
);
981 case WLAN_CIPHER_SUITE_AES_CMAC
:
982 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
983 if (WARN_ON(tid
!= 0))
985 pn
= key
->u
.aes_cmac
.rx_pn
;
986 memcpy(pn
, seq
->aes_cmac
.pn
, IEEE80211_CMAC_PN_LEN
);
988 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
989 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
990 if (WARN_ON(tid
!= 0))
992 pn
= key
->u
.aes_gmac
.rx_pn
;
993 memcpy(pn
, seq
->aes_gmac
.pn
, IEEE80211_GMAC_PN_LEN
);
995 case WLAN_CIPHER_SUITE_GCMP
:
996 case WLAN_CIPHER_SUITE_GCMP_256
:
997 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
1000 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
1002 pn
= key
->u
.gcmp
.rx_pn
[tid
];
1003 memcpy(pn
, seq
->gcmp
.pn
, IEEE80211_GCMP_PN_LEN
);
1010 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq
);
1012 void ieee80211_remove_key(struct ieee80211_key_conf
*keyconf
)
1014 struct ieee80211_key
*key
;
1016 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
1018 assert_key_lock(key
->local
);
1021 * if key was uploaded, we assume the driver will/has remove(d)
1022 * it, so adjust bookkeeping accordingly
1024 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) {
1025 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
1027 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
1028 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
1029 increment_tailroom_need_count(key
->sdata
);
1032 ieee80211_key_free(key
, false);
1034 EXPORT_SYMBOL_GPL(ieee80211_remove_key
);
1036 struct ieee80211_key_conf
*
1037 ieee80211_gtk_rekey_add(struct ieee80211_vif
*vif
,
1038 struct ieee80211_key_conf
*keyconf
)
1040 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
1041 struct ieee80211_local
*local
= sdata
->local
;
1042 struct ieee80211_key
*key
;
1045 if (WARN_ON(!local
->wowlan
))
1046 return ERR_PTR(-EINVAL
);
1048 if (WARN_ON(vif
->type
!= NL80211_IFTYPE_STATION
))
1049 return ERR_PTR(-EINVAL
);
1051 key
= ieee80211_key_alloc(keyconf
->cipher
, keyconf
->keyidx
,
1052 keyconf
->keylen
, keyconf
->key
,
1055 return ERR_CAST(key
);
1057 if (sdata
->u
.mgd
.mfp
!= IEEE80211_MFP_DISABLED
)
1058 key
->conf
.flags
|= IEEE80211_KEY_FLAG_RX_MGMT
;
1060 err
= ieee80211_key_link(key
, sdata
, NULL
);
1062 return ERR_PTR(err
);
1066 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add
);