1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright 2015-2017 Intel Deutschland GmbH
11 #include <linux/if_ether.h>
12 #include <linux/etherdevice.h>
13 #include <linux/list.h>
14 #include <linux/rcupdate.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/slab.h>
17 #include <linux/export.h>
18 #include <net/mac80211.h>
19 #include <crypto/algapi.h>
20 #include <asm/unaligned.h>
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #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);
61 update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
63 struct ieee80211_sub_if_data *vlan;
65 if (sdata->vif.type != NL80211_IFTYPE_AP)
68 /* crypto_tx_tailroom_needed_cnt is protected by this */
69 assert_key_lock(sdata->local);
73 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
74 vlan->crypto_tx_tailroom_needed_cnt += delta;
79 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
82 * When this count is zero, SKB resizing for allocating tailroom
83 * for IV or MMIC is skipped. But, this check has created two race
84 * cases in xmit path while transiting from zero count to one:
86 * 1. SKB resize was skipped because no key was added but just before
87 * the xmit key is added and SW encryption kicks off.
89 * 2. SKB resize was skipped because all the keys were hw planted but
90 * just before xmit one of the key is deleted and SW encryption kicks
93 * In both the above case SW encryption will find not enough space for
94 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
96 * Solution has been explained at
97 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
100 assert_key_lock(sdata->local);
102 update_vlan_tailroom_need_count(sdata, 1);
104 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
106 * Flush all XMIT packets currently using HW encryption or no
107 * encryption at all if the count transition is from 0 -> 1.
113 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
116 assert_key_lock(sdata->local);
118 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
120 update_vlan_tailroom_need_count(sdata, -delta);
121 sdata->crypto_tx_tailroom_needed_cnt -= delta;
124 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
126 struct ieee80211_sub_if_data *sdata = key->sdata;
127 struct sta_info *sta;
128 int ret = -EOPNOTSUPP;
132 if (key->flags & KEY_FLAG_TAINTED) {
133 /* If we get here, it's during resume and the key is
134 * tainted so shouldn't be used/programmed any more.
135 * However, its flags may still indicate that it was
136 * programmed into the device (since we're in resume)
137 * so clear that flag now to avoid trying to remove
140 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
141 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
142 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
143 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
144 increment_tailroom_need_count(sdata);
146 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
150 if (!key->local->ops->set_key)
151 goto out_unsupported;
153 assert_key_lock(key->local);
158 * If this is a per-STA GTK, check if it
159 * is supported; if not, return.
161 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
162 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
163 goto out_unsupported;
165 if (sta && !sta->uploaded)
166 goto out_unsupported;
168 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
170 * The driver doesn't know anything about VLAN interfaces.
171 * Hence, don't send GTKs for VLAN interfaces to the driver.
173 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
175 goto out_unsupported;
179 ret = drv_set_key(key->local, SET_KEY, sdata,
180 sta ? &sta->sta : NULL, &key->conf);
183 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
185 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
186 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
187 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
188 decrease_tailroom_need_count(sdata, 1);
190 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
191 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
193 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
194 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
199 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
201 "failed to set key (%d, %pM) to hardware (%d)\n",
203 sta ? sta->sta.addr : bcast_addr, ret);
206 switch (key->conf.cipher) {
207 case WLAN_CIPHER_SUITE_WEP40:
208 case WLAN_CIPHER_SUITE_WEP104:
209 case WLAN_CIPHER_SUITE_TKIP:
210 case WLAN_CIPHER_SUITE_CCMP:
211 case WLAN_CIPHER_SUITE_CCMP_256:
212 case WLAN_CIPHER_SUITE_AES_CMAC:
213 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
214 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
215 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
216 case WLAN_CIPHER_SUITE_GCMP:
217 case WLAN_CIPHER_SUITE_GCMP_256:
218 /* all of these we can do in software - if driver can */
221 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
229 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
231 struct ieee80211_sub_if_data *sdata;
232 struct sta_info *sta;
237 if (!key || !key->local->ops->set_key)
240 assert_key_lock(key->local);
242 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
248 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
249 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
250 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
251 increment_tailroom_need_count(sdata);
253 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
254 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
255 sta ? &sta->sta : NULL, &key->conf);
259 "failed to remove key (%d, %pM) from hardware (%d)\n",
261 sta ? sta->sta.addr : bcast_addr, ret);
264 int ieee80211_set_tx_key(struct ieee80211_key *key)
266 struct sta_info *sta = key->sta;
267 struct ieee80211_local *local = key->local;
268 struct ieee80211_key *old;
270 assert_key_lock(local);
272 old = key_mtx_dereference(local, sta->ptk[sta->ptk_idx]);
273 sta->ptk_idx = key->conf.keyidx;
274 ieee80211_check_fast_xmit(sta);
279 static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
280 struct ieee80211_key *new_key,
283 struct ieee80211_sub_if_data *sdata;
284 struct ieee80211_local *local;
285 struct sta_info *sta;
288 /* Aggregation sessions are OK when running on SW crypto.
289 * A broken remote STA may cause issues not observed with HW
292 if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
295 assert_key_lock(old_key->local);
298 /* Unicast rekey without Extended Key ID needs special handling */
299 if (new_key && sta && pairwise &&
300 rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
301 local = old_key->local;
302 sdata = old_key->sdata;
304 /* Stop TX till we are on the new key */
305 old_key->flags |= KEY_FLAG_TAINTED;
306 ieee80211_clear_fast_xmit(sta);
308 /* Aggregation sessions during rekey are complicated due to the
309 * reorder buffer and retransmits. Side step that by blocking
310 * aggregation during rekey and tear down running sessions.
312 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
313 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
314 ieee80211_sta_tear_down_BA_sessions(sta,
315 AGG_STOP_LOCAL_REQUEST);
318 if (!wiphy_ext_feature_isset(local->hw.wiphy,
319 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
320 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
322 /* Flushing the driver queues *may* help prevent
323 * the clear text leaks and freezes.
325 ieee80211_flush_queues(local, sdata, false);
329 ieee80211_key_disable_hw_accel(old_key);
332 ret = ieee80211_key_enable_hw_accel(new_key);
339 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
340 int idx, bool uni, bool multi)
342 struct ieee80211_key *key = NULL;
344 assert_key_lock(sdata->local);
346 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
347 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
350 rcu_assign_pointer(sdata->default_unicast_key, key);
351 ieee80211_check_fast_xmit_iface(sdata);
352 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
353 drv_set_default_unicast_key(sdata->local, sdata, idx);
357 rcu_assign_pointer(sdata->default_multicast_key, key);
359 ieee80211_debugfs_key_update_default(sdata);
362 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
363 bool uni, bool multi)
365 mutex_lock(&sdata->local->key_mtx);
366 __ieee80211_set_default_key(sdata, idx, uni, multi);
367 mutex_unlock(&sdata->local->key_mtx);
371 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
373 struct ieee80211_key *key = NULL;
375 assert_key_lock(sdata->local);
377 if (idx >= NUM_DEFAULT_KEYS &&
378 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
379 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
381 rcu_assign_pointer(sdata->default_mgmt_key, key);
383 ieee80211_debugfs_key_update_default(sdata);
386 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
389 mutex_lock(&sdata->local->key_mtx);
390 __ieee80211_set_default_mgmt_key(sdata, idx);
391 mutex_unlock(&sdata->local->key_mtx);
395 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
396 struct sta_info *sta,
398 struct ieee80211_key *old,
399 struct ieee80211_key *new)
403 bool defunikey, defmultikey, defmgmtkey;
405 /* caller must provide at least one old/new */
406 if (WARN_ON(!new && !old))
410 list_add_tail_rcu(&new->list, &sdata->key_list);
412 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
415 idx = old->conf.keyidx;
416 ret = ieee80211_hw_key_replace(old, new, pairwise);
418 /* new must be provided in case old is not */
419 idx = new->conf.keyidx;
420 if (!new->local->wowlan)
421 ret = ieee80211_key_enable_hw_accel(new);
431 rcu_assign_pointer(sta->ptk[idx], new);
433 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
435 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
436 ieee80211_check_fast_xmit(sta);
439 rcu_assign_pointer(sta->gtk[idx], new);
441 /* Only needed for transition from no key -> key.
442 * Still triggers unnecessary when using Extended Key ID
443 * and installing the second key ID the first time.
446 ieee80211_check_fast_rx(sta);
449 old == key_mtx_dereference(sdata->local,
450 sdata->default_unicast_key);
452 old == key_mtx_dereference(sdata->local,
453 sdata->default_multicast_key);
455 old == key_mtx_dereference(sdata->local,
456 sdata->default_mgmt_key);
458 if (defunikey && !new)
459 __ieee80211_set_default_key(sdata, -1, true, false);
460 if (defmultikey && !new)
461 __ieee80211_set_default_key(sdata, -1, false, true);
462 if (defmgmtkey && !new)
463 __ieee80211_set_default_mgmt_key(sdata, -1);
465 rcu_assign_pointer(sdata->keys[idx], new);
466 if (defunikey && new)
467 __ieee80211_set_default_key(sdata, new->conf.keyidx,
469 if (defmultikey && new)
470 __ieee80211_set_default_key(sdata, new->conf.keyidx,
472 if (defmgmtkey && new)
473 __ieee80211_set_default_mgmt_key(sdata,
478 list_del_rcu(&old->list);
483 struct ieee80211_key *
484 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
486 size_t seq_len, const u8 *seq,
487 const struct ieee80211_cipher_scheme *cs)
489 struct ieee80211_key *key;
492 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
493 return ERR_PTR(-EINVAL);
495 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
497 return ERR_PTR(-ENOMEM);
500 * Default to software encryption; we'll later upload the
501 * key to the hardware if possible.
506 key->conf.cipher = cipher;
507 key->conf.keyidx = idx;
508 key->conf.keylen = key_len;
510 case WLAN_CIPHER_SUITE_WEP40:
511 case WLAN_CIPHER_SUITE_WEP104:
512 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
513 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
515 case WLAN_CIPHER_SUITE_TKIP:
516 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
517 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
519 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
520 key->u.tkip.rx[i].iv32 =
521 get_unaligned_le32(&seq[2]);
522 key->u.tkip.rx[i].iv16 =
523 get_unaligned_le16(seq);
526 spin_lock_init(&key->u.tkip.txlock);
528 case WLAN_CIPHER_SUITE_CCMP:
529 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
530 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
532 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
533 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
534 key->u.ccmp.rx_pn[i][j] =
535 seq[IEEE80211_CCMP_PN_LEN - j - 1];
538 * Initialize AES key state here as an optimization so that
539 * it does not need to be initialized for every packet.
541 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
542 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
543 if (IS_ERR(key->u.ccmp.tfm)) {
544 err = PTR_ERR(key->u.ccmp.tfm);
549 case WLAN_CIPHER_SUITE_CCMP_256:
550 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
551 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
552 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
553 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
554 key->u.ccmp.rx_pn[i][j] =
555 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
556 /* Initialize AES key state here as an optimization so that
557 * it does not need to be initialized for every packet.
559 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
560 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
561 if (IS_ERR(key->u.ccmp.tfm)) {
562 err = PTR_ERR(key->u.ccmp.tfm);
567 case WLAN_CIPHER_SUITE_AES_CMAC:
568 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
569 key->conf.iv_len = 0;
570 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
571 key->conf.icv_len = sizeof(struct ieee80211_mmie);
573 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
575 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
576 key->u.aes_cmac.rx_pn[j] =
577 seq[IEEE80211_CMAC_PN_LEN - j - 1];
579 * Initialize AES key state here as an optimization so that
580 * it does not need to be initialized for every packet.
582 key->u.aes_cmac.tfm =
583 ieee80211_aes_cmac_key_setup(key_data, key_len);
584 if (IS_ERR(key->u.aes_cmac.tfm)) {
585 err = PTR_ERR(key->u.aes_cmac.tfm);
590 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
591 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
592 key->conf.iv_len = 0;
593 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
595 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
596 key->u.aes_gmac.rx_pn[j] =
597 seq[IEEE80211_GMAC_PN_LEN - j - 1];
598 /* Initialize AES key state here as an optimization so that
599 * it does not need to be initialized for every packet.
601 key->u.aes_gmac.tfm =
602 ieee80211_aes_gmac_key_setup(key_data, key_len);
603 if (IS_ERR(key->u.aes_gmac.tfm)) {
604 err = PTR_ERR(key->u.aes_gmac.tfm);
609 case WLAN_CIPHER_SUITE_GCMP:
610 case WLAN_CIPHER_SUITE_GCMP_256:
611 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
612 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
613 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
614 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
615 key->u.gcmp.rx_pn[i][j] =
616 seq[IEEE80211_GCMP_PN_LEN - j - 1];
617 /* Initialize AES key state here as an optimization so that
618 * it does not need to be initialized for every packet.
620 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
622 if (IS_ERR(key->u.gcmp.tfm)) {
623 err = PTR_ERR(key->u.gcmp.tfm);
630 if (seq_len && seq_len != cs->pn_len) {
632 return ERR_PTR(-EINVAL);
635 key->conf.iv_len = cs->hdr_len;
636 key->conf.icv_len = cs->mic_len;
637 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
638 for (j = 0; j < seq_len; j++)
639 key->u.gen.rx_pn[i][j] =
640 seq[seq_len - j - 1];
641 key->flags |= KEY_FLAG_CIPHER_SCHEME;
644 memcpy(key->conf.key, key_data, key_len);
645 INIT_LIST_HEAD(&key->list);
650 static void ieee80211_key_free_common(struct ieee80211_key *key)
652 switch (key->conf.cipher) {
653 case WLAN_CIPHER_SUITE_CCMP:
654 case WLAN_CIPHER_SUITE_CCMP_256:
655 ieee80211_aes_key_free(key->u.ccmp.tfm);
657 case WLAN_CIPHER_SUITE_AES_CMAC:
658 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
659 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
661 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
662 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
663 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
665 case WLAN_CIPHER_SUITE_GCMP:
666 case WLAN_CIPHER_SUITE_GCMP_256:
667 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
673 static void __ieee80211_key_destroy(struct ieee80211_key *key,
677 struct ieee80211_sub_if_data *sdata = key->sdata;
679 ieee80211_debugfs_key_remove(key);
681 if (delay_tailroom) {
682 /* see ieee80211_delayed_tailroom_dec */
683 sdata->crypto_tx_tailroom_pending_dec++;
684 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
687 decrease_tailroom_need_count(sdata, 1);
691 ieee80211_key_free_common(key);
694 static void ieee80211_key_destroy(struct ieee80211_key *key,
701 * Synchronize so the TX path and rcu key iterators
702 * can no longer be using this key before we free/remove it.
706 __ieee80211_key_destroy(key, delay_tailroom);
709 void ieee80211_key_free_unused(struct ieee80211_key *key)
711 WARN_ON(key->sdata || key->local);
712 ieee80211_key_free_common(key);
715 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_key *old,
717 struct ieee80211_key *new)
719 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
722 if (!old || new->conf.keylen != old->conf.keylen)
725 tk_old = old->conf.key;
726 tk_new = new->conf.key;
729 * In station mode, don't compare the TX MIC key, as it's never used
730 * and offloaded rekeying may not care to send it to the host. This
731 * is the case in iwlwifi, for example.
733 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
734 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
735 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
736 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
737 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
738 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
739 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
740 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
745 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
748 int ieee80211_key_link(struct ieee80211_key *key,
749 struct ieee80211_sub_if_data *sdata,
750 struct sta_info *sta)
752 struct ieee80211_key *old_key;
753 int idx = key->conf.keyidx;
754 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
756 * We want to delay tailroom updates only for station - in that
757 * case it helps roaming speed, but in other cases it hurts and
758 * can cause warnings to appear.
760 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
761 int ret = -EOPNOTSUPP;
763 mutex_lock(&sdata->local->key_mtx);
765 if (sta && pairwise) {
766 struct ieee80211_key *alt_key;
768 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
769 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
771 /* The rekey code assumes that the old and new key are using
772 * the same cipher. Enforce the assumption for pairwise keys.
775 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
776 (old_key && old_key->conf.cipher != key->conf.cipher)))
779 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
781 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
784 /* Non-pairwise keys must also not switch the cipher on rekey */
786 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
791 * Silently accept key re-installation without really installing the
792 * new version of the key to avoid nonce reuse or replay issues.
794 if (ieee80211_key_identical(sdata, old_key, key)) {
795 ieee80211_key_free_unused(key);
800 key->local = sdata->local;
804 increment_tailroom_need_count(sdata);
806 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
809 ieee80211_debugfs_key_add(key);
810 ieee80211_key_destroy(old_key, delay_tailroom);
812 ieee80211_key_free(key, delay_tailroom);
816 mutex_unlock(&sdata->local->key_mtx);
821 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
827 * Replace key with nothingness if it was ever used.
830 ieee80211_key_replace(key->sdata, key->sta,
831 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
833 ieee80211_key_destroy(key, delay_tailroom);
836 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
838 struct ieee80211_key *key;
839 struct ieee80211_sub_if_data *vlan;
843 if (WARN_ON(!ieee80211_sdata_running(sdata)))
846 mutex_lock(&sdata->local->key_mtx);
848 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
849 sdata->crypto_tx_tailroom_pending_dec);
851 if (sdata->vif.type == NL80211_IFTYPE_AP) {
852 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
853 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
854 vlan->crypto_tx_tailroom_pending_dec);
857 list_for_each_entry(key, &sdata->key_list, list) {
858 increment_tailroom_need_count(sdata);
859 ieee80211_key_enable_hw_accel(key);
862 mutex_unlock(&sdata->local->key_mtx);
865 void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
867 struct ieee80211_sub_if_data *vlan;
869 mutex_lock(&sdata->local->key_mtx);
871 sdata->crypto_tx_tailroom_needed_cnt = 0;
873 if (sdata->vif.type == NL80211_IFTYPE_AP) {
874 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
875 vlan->crypto_tx_tailroom_needed_cnt = 0;
878 mutex_unlock(&sdata->local->key_mtx);
881 void ieee80211_iter_keys(struct ieee80211_hw *hw,
882 struct ieee80211_vif *vif,
883 void (*iter)(struct ieee80211_hw *hw,
884 struct ieee80211_vif *vif,
885 struct ieee80211_sta *sta,
886 struct ieee80211_key_conf *key,
890 struct ieee80211_local *local = hw_to_local(hw);
891 struct ieee80211_key *key, *tmp;
892 struct ieee80211_sub_if_data *sdata;
896 mutex_lock(&local->key_mtx);
898 sdata = vif_to_sdata(vif);
899 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
900 iter(hw, &sdata->vif,
901 key->sta ? &key->sta->sta : NULL,
902 &key->conf, iter_data);
904 list_for_each_entry(sdata, &local->interfaces, list)
905 list_for_each_entry_safe(key, tmp,
906 &sdata->key_list, list)
907 iter(hw, &sdata->vif,
908 key->sta ? &key->sta->sta : NULL,
909 &key->conf, iter_data);
911 mutex_unlock(&local->key_mtx);
913 EXPORT_SYMBOL(ieee80211_iter_keys);
916 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
917 struct ieee80211_sub_if_data *sdata,
918 void (*iter)(struct ieee80211_hw *hw,
919 struct ieee80211_vif *vif,
920 struct ieee80211_sta *sta,
921 struct ieee80211_key_conf *key,
925 struct ieee80211_key *key;
927 list_for_each_entry_rcu(key, &sdata->key_list, list) {
928 /* skip keys of station in removal process */
929 if (key->sta && key->sta->removed)
931 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
934 iter(hw, &sdata->vif,
935 key->sta ? &key->sta->sta : NULL,
936 &key->conf, iter_data);
940 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
941 struct ieee80211_vif *vif,
942 void (*iter)(struct ieee80211_hw *hw,
943 struct ieee80211_vif *vif,
944 struct ieee80211_sta *sta,
945 struct ieee80211_key_conf *key,
949 struct ieee80211_local *local = hw_to_local(hw);
950 struct ieee80211_sub_if_data *sdata;
953 sdata = vif_to_sdata(vif);
954 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
956 list_for_each_entry_rcu(sdata, &local->interfaces, list)
957 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
960 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
962 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
963 struct list_head *keys)
965 struct ieee80211_key *key, *tmp;
967 decrease_tailroom_need_count(sdata,
968 sdata->crypto_tx_tailroom_pending_dec);
969 sdata->crypto_tx_tailroom_pending_dec = 0;
971 ieee80211_debugfs_key_remove_mgmt_default(sdata);
973 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
974 ieee80211_key_replace(key->sdata, key->sta,
975 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
977 list_add_tail(&key->list, keys);
980 ieee80211_debugfs_key_update_default(sdata);
983 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
984 bool force_synchronize)
986 struct ieee80211_local *local = sdata->local;
987 struct ieee80211_sub_if_data *vlan;
988 struct ieee80211_sub_if_data *master;
989 struct ieee80211_key *key, *tmp;
992 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
994 mutex_lock(&local->key_mtx);
996 ieee80211_free_keys_iface(sdata, &keys);
998 if (sdata->vif.type == NL80211_IFTYPE_AP) {
999 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1000 ieee80211_free_keys_iface(vlan, &keys);
1003 if (!list_empty(&keys) || force_synchronize)
1005 list_for_each_entry_safe(key, tmp, &keys, list)
1006 __ieee80211_key_destroy(key, false);
1008 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1010 master = container_of(sdata->bss,
1011 struct ieee80211_sub_if_data,
1014 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1015 master->crypto_tx_tailroom_needed_cnt);
1018 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1019 sdata->crypto_tx_tailroom_pending_dec);
1022 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1023 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1024 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1025 vlan->crypto_tx_tailroom_pending_dec);
1028 mutex_unlock(&local->key_mtx);
1031 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1032 struct sta_info *sta)
1034 struct ieee80211_key *key;
1037 mutex_lock(&local->key_mtx);
1038 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
1039 key = key_mtx_dereference(local, sta->gtk[i]);
1042 ieee80211_key_replace(key->sdata, key->sta,
1043 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1045 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1046 NL80211_IFTYPE_STATION);
1049 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1050 key = key_mtx_dereference(local, sta->ptk[i]);
1053 ieee80211_key_replace(key->sdata, key->sta,
1054 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1056 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1057 NL80211_IFTYPE_STATION);
1060 mutex_unlock(&local->key_mtx);
1063 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1065 struct ieee80211_sub_if_data *sdata;
1067 sdata = container_of(wk, struct ieee80211_sub_if_data,
1068 dec_tailroom_needed_wk.work);
1071 * The reason for the delayed tailroom needed decrementing is to
1072 * make roaming faster: during roaming, all keys are first deleted
1073 * and then new keys are installed. The first new key causes the
1074 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1075 * the cost of synchronize_net() (which can be slow). Avoid this
1076 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1077 * key removal for a while, so if we roam the value is larger than
1078 * zero and no 0->1 transition happens.
1080 * The cost is that if the AP switching was from an AP with keys
1081 * to one without, we still allocate tailroom while it would no
1082 * longer be needed. However, in the typical (fast) roaming case
1083 * within an ESS this usually won't happen.
1086 mutex_lock(&sdata->local->key_mtx);
1087 decrease_tailroom_need_count(sdata,
1088 sdata->crypto_tx_tailroom_pending_dec);
1089 sdata->crypto_tx_tailroom_pending_dec = 0;
1090 mutex_unlock(&sdata->local->key_mtx);
1093 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1094 const u8 *replay_ctr, gfp_t gfp)
1096 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1098 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1100 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1102 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1104 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1105 int tid, struct ieee80211_key_seq *seq)
1107 struct ieee80211_key *key;
1110 key = container_of(keyconf, struct ieee80211_key, conf);
1112 switch (key->conf.cipher) {
1113 case WLAN_CIPHER_SUITE_TKIP:
1114 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1116 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1117 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1119 case WLAN_CIPHER_SUITE_CCMP:
1120 case WLAN_CIPHER_SUITE_CCMP_256:
1121 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1124 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1126 pn = key->u.ccmp.rx_pn[tid];
1127 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1129 case WLAN_CIPHER_SUITE_AES_CMAC:
1130 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1131 if (WARN_ON(tid != 0))
1133 pn = key->u.aes_cmac.rx_pn;
1134 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1136 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1137 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1138 if (WARN_ON(tid != 0))
1140 pn = key->u.aes_gmac.rx_pn;
1141 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1143 case WLAN_CIPHER_SUITE_GCMP:
1144 case WLAN_CIPHER_SUITE_GCMP_256:
1145 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1148 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1150 pn = key->u.gcmp.rx_pn[tid];
1151 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1155 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1157 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1158 int tid, struct ieee80211_key_seq *seq)
1160 struct ieee80211_key *key;
1163 key = container_of(keyconf, struct ieee80211_key, conf);
1165 switch (key->conf.cipher) {
1166 case WLAN_CIPHER_SUITE_TKIP:
1167 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1169 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1170 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1172 case WLAN_CIPHER_SUITE_CCMP:
1173 case WLAN_CIPHER_SUITE_CCMP_256:
1174 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1177 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1179 pn = key->u.ccmp.rx_pn[tid];
1180 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1182 case WLAN_CIPHER_SUITE_AES_CMAC:
1183 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1184 if (WARN_ON(tid != 0))
1186 pn = key->u.aes_cmac.rx_pn;
1187 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1189 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1190 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1191 if (WARN_ON(tid != 0))
1193 pn = key->u.aes_gmac.rx_pn;
1194 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1196 case WLAN_CIPHER_SUITE_GCMP:
1197 case WLAN_CIPHER_SUITE_GCMP_256:
1198 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1201 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1203 pn = key->u.gcmp.rx_pn[tid];
1204 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1211 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1213 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1215 struct ieee80211_key *key;
1217 key = container_of(keyconf, struct ieee80211_key, conf);
1219 assert_key_lock(key->local);
1222 * if key was uploaded, we assume the driver will/has remove(d)
1223 * it, so adjust bookkeeping accordingly
1225 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1226 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1228 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1229 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1230 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1231 increment_tailroom_need_count(key->sdata);
1234 ieee80211_key_free(key, false);
1236 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1238 struct ieee80211_key_conf *
1239 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1240 struct ieee80211_key_conf *keyconf)
1242 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1243 struct ieee80211_local *local = sdata->local;
1244 struct ieee80211_key *key;
1247 if (WARN_ON(!local->wowlan))
1248 return ERR_PTR(-EINVAL);
1250 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1251 return ERR_PTR(-EINVAL);
1253 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1254 keyconf->keylen, keyconf->key,
1257 return ERR_CAST(key);
1259 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1260 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1262 err = ieee80211_key_link(key, sdata, NULL);
1264 return ERR_PTR(err);
1268 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);