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
);
62 update_vlan_tailroom_need_count(struct ieee80211_sub_if_data
*sdata
, int delta
)
64 struct ieee80211_sub_if_data
*vlan
;
66 if (sdata
->vif
.type
!= NL80211_IFTYPE_AP
)
69 mutex_lock(&sdata
->local
->mtx
);
71 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
72 vlan
->crypto_tx_tailroom_needed_cnt
+= delta
;
74 mutex_unlock(&sdata
->local
->mtx
);
77 static void increment_tailroom_need_count(struct ieee80211_sub_if_data
*sdata
)
80 * When this count is zero, SKB resizing for allocating tailroom
81 * for IV or MMIC is skipped. But, this check has created two race
82 * cases in xmit path while transiting from zero count to one:
84 * 1. SKB resize was skipped because no key was added but just before
85 * the xmit key is added and SW encryption kicks off.
87 * 2. SKB resize was skipped because all the keys were hw planted but
88 * just before xmit one of the key is deleted and SW encryption kicks
91 * In both the above case SW encryption will find not enough space for
92 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
94 * Solution has been explained at
95 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
98 update_vlan_tailroom_need_count(sdata
, 1);
100 if (!sdata
->crypto_tx_tailroom_needed_cnt
++) {
102 * Flush all XMIT packets currently using HW encryption or no
103 * encryption at all if the count transition is from 0 -> 1.
109 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data
*sdata
,
112 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
< delta
);
114 update_vlan_tailroom_need_count(sdata
, -delta
);
115 sdata
->crypto_tx_tailroom_needed_cnt
-= delta
;
118 static int ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
120 struct ieee80211_sub_if_data
*sdata
;
121 struct sta_info
*sta
;
122 int ret
= -EOPNOTSUPP
;
126 if (key
->flags
& KEY_FLAG_TAINTED
) {
127 /* If we get here, it's during resume and the key is
128 * tainted so shouldn't be used/programmed any more.
129 * However, its flags may still indicate that it was
130 * programmed into the device (since we're in resume)
131 * so clear that flag now to avoid trying to remove
134 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
138 if (!key
->local
->ops
->set_key
)
139 goto out_unsupported
;
141 assert_key_lock(key
->local
);
146 * If this is a per-STA GTK, check if it
147 * is supported; if not, return.
149 if (sta
&& !(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
) &&
150 !(key
->local
->hw
.flags
& IEEE80211_HW_SUPPORTS_PER_STA_GTK
))
151 goto out_unsupported
;
153 if (sta
&& !sta
->uploaded
)
154 goto out_unsupported
;
157 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
) {
159 * The driver doesn't know anything about VLAN interfaces.
160 * Hence, don't send GTKs for VLAN interfaces to the driver.
162 if (!(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
163 goto out_unsupported
;
166 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
,
167 sta
? &sta
->sta
: NULL
, &key
->conf
);
170 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
172 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
173 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
174 decrease_tailroom_need_count(sdata
, 1);
176 WARN_ON((key
->conf
.flags
& IEEE80211_KEY_FLAG_PUT_IV_SPACE
) &&
177 (key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
));
182 if (ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
&& ret
!= 1)
184 "failed to set key (%d, %pM) to hardware (%d)\n",
186 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
189 switch (key
->conf
.cipher
) {
190 case WLAN_CIPHER_SUITE_WEP40
:
191 case WLAN_CIPHER_SUITE_WEP104
:
192 case WLAN_CIPHER_SUITE_TKIP
:
193 case WLAN_CIPHER_SUITE_CCMP
:
194 case WLAN_CIPHER_SUITE_CCMP_256
:
195 case WLAN_CIPHER_SUITE_AES_CMAC
:
196 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
197 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
198 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
199 case WLAN_CIPHER_SUITE_GCMP
:
200 case WLAN_CIPHER_SUITE_GCMP_256
:
201 /* all of these we can do in software - if driver can */
204 if (key
->local
->hw
.flags
& IEEE80211_HW_SW_CRYPTO_CONTROL
)
212 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
214 struct ieee80211_sub_if_data
*sdata
;
215 struct sta_info
*sta
;
220 if (!key
|| !key
->local
->ops
->set_key
)
223 assert_key_lock(key
->local
);
225 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
231 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
232 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
233 increment_tailroom_need_count(sdata
);
235 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
236 sta
? &sta
->sta
: NULL
, &key
->conf
);
240 "failed to remove key (%d, %pM) from hardware (%d)\n",
242 sta
? sta
->sta
.addr
: bcast_addr
, ret
);
244 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
247 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
248 int idx
, bool uni
, bool multi
)
250 struct ieee80211_key
*key
= NULL
;
252 assert_key_lock(sdata
->local
);
254 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
255 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
258 rcu_assign_pointer(sdata
->default_unicast_key
, key
);
259 ieee80211_check_fast_xmit_iface(sdata
);
260 drv_set_default_unicast_key(sdata
->local
, sdata
, idx
);
264 rcu_assign_pointer(sdata
->default_multicast_key
, key
);
266 ieee80211_debugfs_key_update_default(sdata
);
269 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
,
270 bool uni
, bool multi
)
272 mutex_lock(&sdata
->local
->key_mtx
);
273 __ieee80211_set_default_key(sdata
, idx
, uni
, multi
);
274 mutex_unlock(&sdata
->local
->key_mtx
);
278 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
280 struct ieee80211_key
*key
= NULL
;
282 assert_key_lock(sdata
->local
);
284 if (idx
>= NUM_DEFAULT_KEYS
&&
285 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
286 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
288 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
290 ieee80211_debugfs_key_update_default(sdata
);
293 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
296 mutex_lock(&sdata
->local
->key_mtx
);
297 __ieee80211_set_default_mgmt_key(sdata
, idx
);
298 mutex_unlock(&sdata
->local
->key_mtx
);
302 static void ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
303 struct sta_info
*sta
,
305 struct ieee80211_key
*old
,
306 struct ieee80211_key
*new)
309 bool defunikey
, defmultikey
, defmgmtkey
;
311 /* caller must provide at least one old/new */
312 if (WARN_ON(!new && !old
))
316 list_add_tail(&new->list
, &sdata
->key_list
);
318 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
321 idx
= old
->conf
.keyidx
;
323 idx
= new->conf
.keyidx
;
327 rcu_assign_pointer(sta
->ptk
[idx
], new);
329 ieee80211_check_fast_xmit(sta
);
331 rcu_assign_pointer(sta
->gtk
[idx
], new);
336 old
== key_mtx_dereference(sdata
->local
,
337 sdata
->default_unicast_key
);
339 old
== key_mtx_dereference(sdata
->local
,
340 sdata
->default_multicast_key
);
342 old
== key_mtx_dereference(sdata
->local
,
343 sdata
->default_mgmt_key
);
345 if (defunikey
&& !new)
346 __ieee80211_set_default_key(sdata
, -1, true, false);
347 if (defmultikey
&& !new)
348 __ieee80211_set_default_key(sdata
, -1, false, true);
349 if (defmgmtkey
&& !new)
350 __ieee80211_set_default_mgmt_key(sdata
, -1);
352 rcu_assign_pointer(sdata
->keys
[idx
], new);
353 if (defunikey
&& new)
354 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
356 if (defmultikey
&& new)
357 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
359 if (defmgmtkey
&& new)
360 __ieee80211_set_default_mgmt_key(sdata
,
365 list_del(&old
->list
);
368 struct ieee80211_key
*
369 ieee80211_key_alloc(u32 cipher
, int idx
, size_t key_len
,
371 size_t seq_len
, const u8
*seq
,
372 const struct ieee80211_cipher_scheme
*cs
)
374 struct ieee80211_key
*key
;
377 if (WARN_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
))
378 return ERR_PTR(-EINVAL
);
380 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
382 return ERR_PTR(-ENOMEM
);
385 * Default to software encryption; we'll later upload the
386 * key to the hardware if possible.
391 key
->conf
.cipher
= cipher
;
392 key
->conf
.keyidx
= idx
;
393 key
->conf
.keylen
= key_len
;
395 case WLAN_CIPHER_SUITE_WEP40
:
396 case WLAN_CIPHER_SUITE_WEP104
:
397 key
->conf
.iv_len
= IEEE80211_WEP_IV_LEN
;
398 key
->conf
.icv_len
= IEEE80211_WEP_ICV_LEN
;
400 case WLAN_CIPHER_SUITE_TKIP
:
401 key
->conf
.iv_len
= IEEE80211_TKIP_IV_LEN
;
402 key
->conf
.icv_len
= IEEE80211_TKIP_ICV_LEN
;
404 for (i
= 0; i
< IEEE80211_NUM_TIDS
; i
++) {
405 key
->u
.tkip
.rx
[i
].iv32
=
406 get_unaligned_le32(&seq
[2]);
407 key
->u
.tkip
.rx
[i
].iv16
=
408 get_unaligned_le16(seq
);
411 spin_lock_init(&key
->u
.tkip
.txlock
);
413 case WLAN_CIPHER_SUITE_CCMP
:
414 key
->conf
.iv_len
= IEEE80211_CCMP_HDR_LEN
;
415 key
->conf
.icv_len
= IEEE80211_CCMP_MIC_LEN
;
417 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
418 for (j
= 0; j
< IEEE80211_CCMP_PN_LEN
; j
++)
419 key
->u
.ccmp
.rx_pn
[i
][j
] =
420 seq
[IEEE80211_CCMP_PN_LEN
- j
- 1];
423 * Initialize AES key state here as an optimization so that
424 * it does not need to be initialized for every packet.
426 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
427 key_data
, key_len
, IEEE80211_CCMP_MIC_LEN
);
428 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
429 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
434 case WLAN_CIPHER_SUITE_CCMP_256
:
435 key
->conf
.iv_len
= IEEE80211_CCMP_256_HDR_LEN
;
436 key
->conf
.icv_len
= IEEE80211_CCMP_256_MIC_LEN
;
437 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
438 for (j
= 0; j
< IEEE80211_CCMP_256_PN_LEN
; j
++)
439 key
->u
.ccmp
.rx_pn
[i
][j
] =
440 seq
[IEEE80211_CCMP_256_PN_LEN
- j
- 1];
441 /* Initialize AES key state here as an optimization so that
442 * it does not need to be initialized for every packet.
444 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(
445 key_data
, key_len
, IEEE80211_CCMP_256_MIC_LEN
);
446 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
447 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
452 case WLAN_CIPHER_SUITE_AES_CMAC
:
453 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
454 key
->conf
.iv_len
= 0;
455 if (cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
456 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
458 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie_16
);
460 for (j
= 0; j
< IEEE80211_CMAC_PN_LEN
; j
++)
461 key
->u
.aes_cmac
.rx_pn
[j
] =
462 seq
[IEEE80211_CMAC_PN_LEN
- j
- 1];
464 * Initialize AES key state here as an optimization so that
465 * it does not need to be initialized for every packet.
467 key
->u
.aes_cmac
.tfm
=
468 ieee80211_aes_cmac_key_setup(key_data
, key_len
);
469 if (IS_ERR(key
->u
.aes_cmac
.tfm
)) {
470 err
= PTR_ERR(key
->u
.aes_cmac
.tfm
);
475 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
476 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
477 key
->conf
.iv_len
= 0;
478 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie_16
);
480 for (j
= 0; j
< IEEE80211_GMAC_PN_LEN
; j
++)
481 key
->u
.aes_gmac
.rx_pn
[j
] =
482 seq
[IEEE80211_GMAC_PN_LEN
- j
- 1];
483 /* Initialize AES key state here as an optimization so that
484 * it does not need to be initialized for every packet.
486 key
->u
.aes_gmac
.tfm
=
487 ieee80211_aes_gmac_key_setup(key_data
, key_len
);
488 if (IS_ERR(key
->u
.aes_gmac
.tfm
)) {
489 err
= PTR_ERR(key
->u
.aes_gmac
.tfm
);
494 case WLAN_CIPHER_SUITE_GCMP
:
495 case WLAN_CIPHER_SUITE_GCMP_256
:
496 key
->conf
.iv_len
= IEEE80211_GCMP_HDR_LEN
;
497 key
->conf
.icv_len
= IEEE80211_GCMP_MIC_LEN
;
498 for (i
= 0; seq
&& i
< IEEE80211_NUM_TIDS
+ 1; i
++)
499 for (j
= 0; j
< IEEE80211_GCMP_PN_LEN
; j
++)
500 key
->u
.gcmp
.rx_pn
[i
][j
] =
501 seq
[IEEE80211_GCMP_PN_LEN
- j
- 1];
502 /* Initialize AES key state here as an optimization so that
503 * it does not need to be initialized for every packet.
505 key
->u
.gcmp
.tfm
= ieee80211_aes_gcm_key_setup_encrypt(key_data
,
507 if (IS_ERR(key
->u
.gcmp
.tfm
)) {
508 err
= PTR_ERR(key
->u
.gcmp
.tfm
);
515 if (seq_len
&& seq_len
!= cs
->pn_len
) {
517 return ERR_PTR(-EINVAL
);
520 key
->conf
.iv_len
= cs
->hdr_len
;
521 key
->conf
.icv_len
= cs
->mic_len
;
522 for (i
= 0; i
< IEEE80211_NUM_TIDS
+ 1; i
++)
523 for (j
= 0; j
< seq_len
; j
++)
524 key
->u
.gen
.rx_pn
[i
][j
] =
525 seq
[seq_len
- j
- 1];
526 key
->flags
|= KEY_FLAG_CIPHER_SCHEME
;
529 memcpy(key
->conf
.key
, key_data
, key_len
);
530 INIT_LIST_HEAD(&key
->list
);
535 static void ieee80211_key_free_common(struct ieee80211_key
*key
)
537 switch (key
->conf
.cipher
) {
538 case WLAN_CIPHER_SUITE_CCMP
:
539 case WLAN_CIPHER_SUITE_CCMP_256
:
540 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
542 case WLAN_CIPHER_SUITE_AES_CMAC
:
543 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
544 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
546 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
547 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
548 ieee80211_aes_gmac_key_free(key
->u
.aes_gmac
.tfm
);
550 case WLAN_CIPHER_SUITE_GCMP
:
551 case WLAN_CIPHER_SUITE_GCMP_256
:
552 ieee80211_aes_gcm_key_free(key
->u
.gcmp
.tfm
);
558 static void __ieee80211_key_destroy(struct ieee80211_key
*key
,
562 ieee80211_key_disable_hw_accel(key
);
565 struct ieee80211_sub_if_data
*sdata
= key
->sdata
;
567 ieee80211_debugfs_key_remove(key
);
569 if (delay_tailroom
) {
570 /* see ieee80211_delayed_tailroom_dec */
571 sdata
->crypto_tx_tailroom_pending_dec
++;
572 schedule_delayed_work(&sdata
->dec_tailroom_needed_wk
,
575 decrease_tailroom_need_count(sdata
, 1);
579 ieee80211_key_free_common(key
);
582 static void ieee80211_key_destroy(struct ieee80211_key
*key
,
589 * Synchronize so the TX path can no longer be using
590 * this key before we free/remove it.
594 __ieee80211_key_destroy(key
, delay_tailroom
);
597 void ieee80211_key_free_unused(struct ieee80211_key
*key
)
599 WARN_ON(key
->sdata
|| key
->local
);
600 ieee80211_key_free_common(key
);
603 int ieee80211_key_link(struct ieee80211_key
*key
,
604 struct ieee80211_sub_if_data
*sdata
,
605 struct sta_info
*sta
)
607 struct ieee80211_local
*local
= sdata
->local
;
608 struct ieee80211_key
*old_key
;
612 pairwise
= key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
;
613 idx
= key
->conf
.keyidx
;
614 key
->local
= sdata
->local
;
618 mutex_lock(&sdata
->local
->key_mtx
);
621 old_key
= key_mtx_dereference(sdata
->local
, sta
->ptk
[idx
]);
623 old_key
= key_mtx_dereference(sdata
->local
, sta
->gtk
[idx
]);
625 old_key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
627 increment_tailroom_need_count(sdata
);
629 ieee80211_key_replace(sdata
, sta
, pairwise
, old_key
, key
);
630 ieee80211_key_destroy(old_key
, true);
632 ieee80211_debugfs_key_add(key
);
634 if (!local
->wowlan
) {
635 ret
= ieee80211_key_enable_hw_accel(key
);
637 ieee80211_key_free(key
, true);
642 mutex_unlock(&sdata
->local
->key_mtx
);
647 void ieee80211_key_free(struct ieee80211_key
*key
, bool delay_tailroom
)
653 * Replace key with nothingness if it was ever used.
656 ieee80211_key_replace(key
->sdata
, key
->sta
,
657 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
659 ieee80211_key_destroy(key
, delay_tailroom
);
662 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
664 struct ieee80211_key
*key
;
665 struct ieee80211_sub_if_data
*vlan
;
669 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
672 mutex_lock(&sdata
->local
->key_mtx
);
674 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
||
675 sdata
->crypto_tx_tailroom_pending_dec
);
677 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
678 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
679 WARN_ON_ONCE(vlan
->crypto_tx_tailroom_needed_cnt
||
680 vlan
->crypto_tx_tailroom_pending_dec
);
683 list_for_each_entry(key
, &sdata
->key_list
, list
) {
684 increment_tailroom_need_count(sdata
);
685 ieee80211_key_enable_hw_accel(key
);
688 mutex_unlock(&sdata
->local
->key_mtx
);
691 void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data
*sdata
)
693 struct ieee80211_sub_if_data
*vlan
;
695 mutex_lock(&sdata
->local
->key_mtx
);
697 sdata
->crypto_tx_tailroom_needed_cnt
= 0;
699 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
700 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
701 vlan
->crypto_tx_tailroom_needed_cnt
= 0;
704 mutex_unlock(&sdata
->local
->key_mtx
);
707 void ieee80211_iter_keys(struct ieee80211_hw
*hw
,
708 struct ieee80211_vif
*vif
,
709 void (*iter
)(struct ieee80211_hw
*hw
,
710 struct ieee80211_vif
*vif
,
711 struct ieee80211_sta
*sta
,
712 struct ieee80211_key_conf
*key
,
716 struct ieee80211_local
*local
= hw_to_local(hw
);
717 struct ieee80211_key
*key
, *tmp
;
718 struct ieee80211_sub_if_data
*sdata
;
722 mutex_lock(&local
->key_mtx
);
724 sdata
= vif_to_sdata(vif
);
725 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
726 iter(hw
, &sdata
->vif
,
727 key
->sta
? &key
->sta
->sta
: NULL
,
728 &key
->conf
, iter_data
);
730 list_for_each_entry(sdata
, &local
->interfaces
, list
)
731 list_for_each_entry_safe(key
, tmp
,
732 &sdata
->key_list
, list
)
733 iter(hw
, &sdata
->vif
,
734 key
->sta
? &key
->sta
->sta
: NULL
,
735 &key
->conf
, iter_data
);
737 mutex_unlock(&local
->key_mtx
);
739 EXPORT_SYMBOL(ieee80211_iter_keys
);
741 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data
*sdata
,
742 struct list_head
*keys
)
744 struct ieee80211_key
*key
, *tmp
;
746 decrease_tailroom_need_count(sdata
,
747 sdata
->crypto_tx_tailroom_pending_dec
);
748 sdata
->crypto_tx_tailroom_pending_dec
= 0;
750 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
752 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
) {
753 ieee80211_key_replace(key
->sdata
, key
->sta
,
754 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
756 list_add_tail(&key
->list
, keys
);
759 ieee80211_debugfs_key_update_default(sdata
);
762 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
,
763 bool force_synchronize
)
765 struct ieee80211_local
*local
= sdata
->local
;
766 struct ieee80211_sub_if_data
*vlan
;
767 struct ieee80211_sub_if_data
*master
;
768 struct ieee80211_key
*key
, *tmp
;
771 cancel_delayed_work_sync(&sdata
->dec_tailroom_needed_wk
);
773 mutex_lock(&local
->key_mtx
);
775 ieee80211_free_keys_iface(sdata
, &keys
);
777 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
778 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
779 ieee80211_free_keys_iface(vlan
, &keys
);
782 if (!list_empty(&keys
) || force_synchronize
)
784 list_for_each_entry_safe(key
, tmp
, &keys
, list
)
785 __ieee80211_key_destroy(key
, false);
787 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
) {
789 master
= container_of(sdata
->bss
,
790 struct ieee80211_sub_if_data
,
793 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
!=
794 master
->crypto_tx_tailroom_needed_cnt
);
797 WARN_ON_ONCE(sdata
->crypto_tx_tailroom_needed_cnt
||
798 sdata
->crypto_tx_tailroom_pending_dec
);
801 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
) {
802 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
803 WARN_ON_ONCE(vlan
->crypto_tx_tailroom_needed_cnt
||
804 vlan
->crypto_tx_tailroom_pending_dec
);
807 mutex_unlock(&local
->key_mtx
);
810 void ieee80211_free_sta_keys(struct ieee80211_local
*local
,
811 struct sta_info
*sta
)
813 struct ieee80211_key
*key
;
816 mutex_lock(&local
->key_mtx
);
817 for (i
= 0; i
< ARRAY_SIZE(sta
->gtk
); i
++) {
818 key
= key_mtx_dereference(local
, sta
->gtk
[i
]);
821 ieee80211_key_replace(key
->sdata
, key
->sta
,
822 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
824 __ieee80211_key_destroy(key
, true);
827 for (i
= 0; i
< NUM_DEFAULT_KEYS
; i
++) {
828 key
= key_mtx_dereference(local
, sta
->ptk
[i
]);
831 ieee80211_key_replace(key
->sdata
, key
->sta
,
832 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
834 __ieee80211_key_destroy(key
, true);
837 mutex_unlock(&local
->key_mtx
);
840 void ieee80211_delayed_tailroom_dec(struct work_struct
*wk
)
842 struct ieee80211_sub_if_data
*sdata
;
844 sdata
= container_of(wk
, struct ieee80211_sub_if_data
,
845 dec_tailroom_needed_wk
.work
);
848 * The reason for the delayed tailroom needed decrementing is to
849 * make roaming faster: during roaming, all keys are first deleted
850 * and then new keys are installed. The first new key causes the
851 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
852 * the cost of synchronize_net() (which can be slow). Avoid this
853 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
854 * key removal for a while, so if we roam the value is larger than
855 * zero and no 0->1 transition happens.
857 * The cost is that if the AP switching was from an AP with keys
858 * to one without, we still allocate tailroom while it would no
859 * longer be needed. However, in the typical (fast) roaming case
860 * within an ESS this usually won't happen.
863 mutex_lock(&sdata
->local
->key_mtx
);
864 decrease_tailroom_need_count(sdata
,
865 sdata
->crypto_tx_tailroom_pending_dec
);
866 sdata
->crypto_tx_tailroom_pending_dec
= 0;
867 mutex_unlock(&sdata
->local
->key_mtx
);
870 void ieee80211_gtk_rekey_notify(struct ieee80211_vif
*vif
, const u8
*bssid
,
871 const u8
*replay_ctr
, gfp_t gfp
)
873 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
875 trace_api_gtk_rekey_notify(sdata
, bssid
, replay_ctr
);
877 cfg80211_gtk_rekey_notify(sdata
->dev
, bssid
, replay_ctr
, gfp
);
879 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify
);
881 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
882 struct ieee80211_key_seq
*seq
)
884 struct ieee80211_key
*key
;
887 if (WARN_ON(!(keyconf
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)))
890 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
892 switch (key
->conf
.cipher
) {
893 case WLAN_CIPHER_SUITE_TKIP
:
894 seq
->tkip
.iv32
= key
->u
.tkip
.tx
.iv32
;
895 seq
->tkip
.iv16
= key
->u
.tkip
.tx
.iv16
;
897 case WLAN_CIPHER_SUITE_CCMP
:
898 case WLAN_CIPHER_SUITE_CCMP_256
:
899 case WLAN_CIPHER_SUITE_AES_CMAC
:
900 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
901 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
902 offsetof(typeof(*seq
), aes_cmac
));
903 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
904 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
905 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
906 offsetof(typeof(*seq
), aes_gmac
));
907 case WLAN_CIPHER_SUITE_GCMP
:
908 case WLAN_CIPHER_SUITE_GCMP_256
:
909 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
910 offsetof(typeof(*seq
), gcmp
));
911 pn64
= atomic64_read(&key
->conf
.tx_pn
);
912 seq
->ccmp
.pn
[5] = pn64
;
913 seq
->ccmp
.pn
[4] = pn64
>> 8;
914 seq
->ccmp
.pn
[3] = pn64
>> 16;
915 seq
->ccmp
.pn
[2] = pn64
>> 24;
916 seq
->ccmp
.pn
[1] = pn64
>> 32;
917 seq
->ccmp
.pn
[0] = pn64
>> 40;
923 EXPORT_SYMBOL(ieee80211_get_key_tx_seq
);
925 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
926 int tid
, struct ieee80211_key_seq
*seq
)
928 struct ieee80211_key
*key
;
931 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
933 switch (key
->conf
.cipher
) {
934 case WLAN_CIPHER_SUITE_TKIP
:
935 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
937 seq
->tkip
.iv32
= key
->u
.tkip
.rx
[tid
].iv32
;
938 seq
->tkip
.iv16
= key
->u
.tkip
.rx
[tid
].iv16
;
940 case WLAN_CIPHER_SUITE_CCMP
:
941 case WLAN_CIPHER_SUITE_CCMP_256
:
942 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
945 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
947 pn
= key
->u
.ccmp
.rx_pn
[tid
];
948 memcpy(seq
->ccmp
.pn
, pn
, IEEE80211_CCMP_PN_LEN
);
950 case WLAN_CIPHER_SUITE_AES_CMAC
:
951 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
952 if (WARN_ON(tid
!= 0))
954 pn
= key
->u
.aes_cmac
.rx_pn
;
955 memcpy(seq
->aes_cmac
.pn
, pn
, IEEE80211_CMAC_PN_LEN
);
957 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
958 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
959 if (WARN_ON(tid
!= 0))
961 pn
= key
->u
.aes_gmac
.rx_pn
;
962 memcpy(seq
->aes_gmac
.pn
, pn
, IEEE80211_GMAC_PN_LEN
);
964 case WLAN_CIPHER_SUITE_GCMP
:
965 case WLAN_CIPHER_SUITE_GCMP_256
:
966 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
969 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
971 pn
= key
->u
.gcmp
.rx_pn
[tid
];
972 memcpy(seq
->gcmp
.pn
, pn
, IEEE80211_GCMP_PN_LEN
);
976 EXPORT_SYMBOL(ieee80211_get_key_rx_seq
);
978 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf
*keyconf
,
979 struct ieee80211_key_seq
*seq
)
981 struct ieee80211_key
*key
;
984 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
986 switch (key
->conf
.cipher
) {
987 case WLAN_CIPHER_SUITE_TKIP
:
988 key
->u
.tkip
.tx
.iv32
= seq
->tkip
.iv32
;
989 key
->u
.tkip
.tx
.iv16
= seq
->tkip
.iv16
;
991 case WLAN_CIPHER_SUITE_CCMP
:
992 case WLAN_CIPHER_SUITE_CCMP_256
:
993 case WLAN_CIPHER_SUITE_AES_CMAC
:
994 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
995 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
996 offsetof(typeof(*seq
), aes_cmac
));
997 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
998 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
999 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
1000 offsetof(typeof(*seq
), aes_gmac
));
1001 case WLAN_CIPHER_SUITE_GCMP
:
1002 case WLAN_CIPHER_SUITE_GCMP_256
:
1003 BUILD_BUG_ON(offsetof(typeof(*seq
), ccmp
) !=
1004 offsetof(typeof(*seq
), gcmp
));
1005 pn64
= (u64
)seq
->ccmp
.pn
[5] |
1006 ((u64
)seq
->ccmp
.pn
[4] << 8) |
1007 ((u64
)seq
->ccmp
.pn
[3] << 16) |
1008 ((u64
)seq
->ccmp
.pn
[2] << 24) |
1009 ((u64
)seq
->ccmp
.pn
[1] << 32) |
1010 ((u64
)seq
->ccmp
.pn
[0] << 40);
1011 atomic64_set(&key
->conf
.tx_pn
, pn64
);
1018 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq
);
1020 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf
*keyconf
,
1021 int tid
, struct ieee80211_key_seq
*seq
)
1023 struct ieee80211_key
*key
;
1026 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
1028 switch (key
->conf
.cipher
) {
1029 case WLAN_CIPHER_SUITE_TKIP
:
1030 if (WARN_ON(tid
< 0 || tid
>= IEEE80211_NUM_TIDS
))
1032 key
->u
.tkip
.rx
[tid
].iv32
= seq
->tkip
.iv32
;
1033 key
->u
.tkip
.rx
[tid
].iv16
= seq
->tkip
.iv16
;
1035 case WLAN_CIPHER_SUITE_CCMP
:
1036 case WLAN_CIPHER_SUITE_CCMP_256
:
1037 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
1040 pn
= key
->u
.ccmp
.rx_pn
[IEEE80211_NUM_TIDS
];
1042 pn
= key
->u
.ccmp
.rx_pn
[tid
];
1043 memcpy(pn
, seq
->ccmp
.pn
, IEEE80211_CCMP_PN_LEN
);
1045 case WLAN_CIPHER_SUITE_AES_CMAC
:
1046 case WLAN_CIPHER_SUITE_BIP_CMAC_256
:
1047 if (WARN_ON(tid
!= 0))
1049 pn
= key
->u
.aes_cmac
.rx_pn
;
1050 memcpy(pn
, seq
->aes_cmac
.pn
, IEEE80211_CMAC_PN_LEN
);
1052 case WLAN_CIPHER_SUITE_BIP_GMAC_128
:
1053 case WLAN_CIPHER_SUITE_BIP_GMAC_256
:
1054 if (WARN_ON(tid
!= 0))
1056 pn
= key
->u
.aes_gmac
.rx_pn
;
1057 memcpy(pn
, seq
->aes_gmac
.pn
, IEEE80211_GMAC_PN_LEN
);
1059 case WLAN_CIPHER_SUITE_GCMP
:
1060 case WLAN_CIPHER_SUITE_GCMP_256
:
1061 if (WARN_ON(tid
< -1 || tid
>= IEEE80211_NUM_TIDS
))
1064 pn
= key
->u
.gcmp
.rx_pn
[IEEE80211_NUM_TIDS
];
1066 pn
= key
->u
.gcmp
.rx_pn
[tid
];
1067 memcpy(pn
, seq
->gcmp
.pn
, IEEE80211_GCMP_PN_LEN
);
1074 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq
);
1076 void ieee80211_remove_key(struct ieee80211_key_conf
*keyconf
)
1078 struct ieee80211_key
*key
;
1080 key
= container_of(keyconf
, struct ieee80211_key
, conf
);
1082 assert_key_lock(key
->local
);
1085 * if key was uploaded, we assume the driver will/has remove(d)
1086 * it, so adjust bookkeeping accordingly
1088 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) {
1089 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
1091 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
1092 (key
->conf
.flags
& IEEE80211_KEY_FLAG_RESERVE_TAILROOM
)))
1093 increment_tailroom_need_count(key
->sdata
);
1096 ieee80211_key_free(key
, false);
1098 EXPORT_SYMBOL_GPL(ieee80211_remove_key
);
1100 struct ieee80211_key_conf
*
1101 ieee80211_gtk_rekey_add(struct ieee80211_vif
*vif
,
1102 struct ieee80211_key_conf
*keyconf
)
1104 struct ieee80211_sub_if_data
*sdata
= vif_to_sdata(vif
);
1105 struct ieee80211_local
*local
= sdata
->local
;
1106 struct ieee80211_key
*key
;
1109 if (WARN_ON(!local
->wowlan
))
1110 return ERR_PTR(-EINVAL
);
1112 if (WARN_ON(vif
->type
!= NL80211_IFTYPE_STATION
))
1113 return ERR_PTR(-EINVAL
);
1115 key
= ieee80211_key_alloc(keyconf
->cipher
, keyconf
->keyidx
,
1116 keyconf
->keylen
, keyconf
->key
,
1119 return ERR_CAST(key
);
1121 if (sdata
->u
.mgd
.mfp
!= IEEE80211_MFP_DISABLED
)
1122 key
->conf
.flags
|= IEEE80211_KEY_FLAG_RX_MGMT
;
1124 err
= ieee80211_key_link(key
, sdata
, NULL
);
1126 return ERR_PTR(err
);
1130 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add
);