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;
269 assert_key_lock(local);
271 sta->ptk_idx = key->conf.keyidx;
273 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
274 ieee80211_check_fast_xmit(sta);
279 static void ieee80211_pairwise_rekey(struct ieee80211_key *old,
280 struct ieee80211_key *new)
282 struct ieee80211_local *local = new->local;
283 struct sta_info *sta = new->sta;
286 assert_key_lock(local);
288 if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) {
289 /* Extended Key ID key install, initial one or rekey */
291 if (sta->ptk_idx != INVALID_PTK_KEYIDX) {
292 /* Aggregation Sessions with Extended Key ID must not
293 * mix MPDUs with different keyIDs within one A-MPDU.
294 * Tear down running Tx aggregation sessions and block
295 * new Rx/Tx aggregation requests during rekey to
296 * ensure there are no A-MPDUs for the driver to
297 * aggregate. (Blocking Tx only would be sufficient but
298 * WLAN_STA_BLOCK_BA gets the job done for the few ms
301 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
302 mutex_lock(&sta->ampdu_mlme.mtx);
303 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
304 ___ieee80211_stop_tx_ba_session(sta, i,
305 AGG_STOP_LOCAL_REQUEST);
306 mutex_unlock(&sta->ampdu_mlme.mtx);
309 /* Rekey without Extended Key ID.
310 * Aggregation sessions are OK when running on SW crypto.
311 * A broken remote STA may cause issues not observed with HW
314 if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
317 /* Stop Tx till we are on the new key */
318 old->flags |= KEY_FLAG_TAINTED;
319 ieee80211_clear_fast_xmit(sta);
320 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
321 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
322 ieee80211_sta_tear_down_BA_sessions(sta,
323 AGG_STOP_LOCAL_REQUEST);
325 if (!wiphy_ext_feature_isset(local->hw.wiphy,
326 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
327 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
329 /* Flushing the driver queues *may* help prevent
330 * the clear text leaks and freezes.
332 ieee80211_flush_queues(local, old->sdata, false);
337 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
338 int idx, bool uni, bool multi)
340 struct ieee80211_key *key = NULL;
342 assert_key_lock(sdata->local);
344 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
345 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
348 rcu_assign_pointer(sdata->default_unicast_key, key);
349 ieee80211_check_fast_xmit_iface(sdata);
350 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
351 drv_set_default_unicast_key(sdata->local, sdata, idx);
355 rcu_assign_pointer(sdata->default_multicast_key, key);
357 ieee80211_debugfs_key_update_default(sdata);
360 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
361 bool uni, bool multi)
363 mutex_lock(&sdata->local->key_mtx);
364 __ieee80211_set_default_key(sdata, idx, uni, multi);
365 mutex_unlock(&sdata->local->key_mtx);
369 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
371 struct ieee80211_key *key = NULL;
373 assert_key_lock(sdata->local);
375 if (idx >= NUM_DEFAULT_KEYS &&
376 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
377 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
379 rcu_assign_pointer(sdata->default_mgmt_key, key);
381 ieee80211_debugfs_key_update_default(sdata);
384 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
387 mutex_lock(&sdata->local->key_mtx);
388 __ieee80211_set_default_mgmt_key(sdata, idx);
389 mutex_unlock(&sdata->local->key_mtx);
392 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
393 struct sta_info *sta,
395 struct ieee80211_key *old,
396 struct ieee80211_key *new)
400 bool defunikey, defmultikey, defmgmtkey;
402 /* caller must provide at least one old/new */
403 if (WARN_ON(!new && !old))
407 list_add_tail_rcu(&new->list, &sdata->key_list);
409 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
411 if (new && sta && pairwise) {
412 /* Unicast rekey needs special handling. With Extended Key ID
413 * old is still NULL for the first rekey.
415 ieee80211_pairwise_rekey(old, new);
419 idx = old->conf.keyidx;
421 if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
422 ieee80211_key_disable_hw_accel(old);
425 ret = ieee80211_key_enable_hw_accel(new);
428 /* new must be provided in case old is not */
429 idx = new->conf.keyidx;
430 if (!new->local->wowlan)
431 ret = ieee80211_key_enable_hw_accel(new);
439 rcu_assign_pointer(sta->ptk[idx], new);
441 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
443 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
444 ieee80211_check_fast_xmit(sta);
447 rcu_assign_pointer(sta->gtk[idx], new);
449 /* Only needed for transition from no key -> key.
450 * Still triggers unnecessary when using Extended Key ID
451 * and installing the second key ID the first time.
454 ieee80211_check_fast_rx(sta);
457 old == key_mtx_dereference(sdata->local,
458 sdata->default_unicast_key);
460 old == key_mtx_dereference(sdata->local,
461 sdata->default_multicast_key);
463 old == key_mtx_dereference(sdata->local,
464 sdata->default_mgmt_key);
466 if (defunikey && !new)
467 __ieee80211_set_default_key(sdata, -1, true, false);
468 if (defmultikey && !new)
469 __ieee80211_set_default_key(sdata, -1, false, true);
470 if (defmgmtkey && !new)
471 __ieee80211_set_default_mgmt_key(sdata, -1);
473 rcu_assign_pointer(sdata->keys[idx], new);
474 if (defunikey && new)
475 __ieee80211_set_default_key(sdata, new->conf.keyidx,
477 if (defmultikey && new)
478 __ieee80211_set_default_key(sdata, new->conf.keyidx,
480 if (defmgmtkey && new)
481 __ieee80211_set_default_mgmt_key(sdata,
486 list_del_rcu(&old->list);
491 struct ieee80211_key *
492 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
494 size_t seq_len, const u8 *seq,
495 const struct ieee80211_cipher_scheme *cs)
497 struct ieee80211_key *key;
500 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
501 return ERR_PTR(-EINVAL);
503 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
505 return ERR_PTR(-ENOMEM);
508 * Default to software encryption; we'll later upload the
509 * key to the hardware if possible.
514 key->conf.cipher = cipher;
515 key->conf.keyidx = idx;
516 key->conf.keylen = key_len;
518 case WLAN_CIPHER_SUITE_WEP40:
519 case WLAN_CIPHER_SUITE_WEP104:
520 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
521 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
523 case WLAN_CIPHER_SUITE_TKIP:
524 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
525 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
527 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
528 key->u.tkip.rx[i].iv32 =
529 get_unaligned_le32(&seq[2]);
530 key->u.tkip.rx[i].iv16 =
531 get_unaligned_le16(seq);
534 spin_lock_init(&key->u.tkip.txlock);
536 case WLAN_CIPHER_SUITE_CCMP:
537 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
538 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
540 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
541 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
542 key->u.ccmp.rx_pn[i][j] =
543 seq[IEEE80211_CCMP_PN_LEN - j - 1];
546 * Initialize AES key state here as an optimization so that
547 * it does not need to be initialized for every packet.
549 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
550 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
551 if (IS_ERR(key->u.ccmp.tfm)) {
552 err = PTR_ERR(key->u.ccmp.tfm);
557 case WLAN_CIPHER_SUITE_CCMP_256:
558 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
559 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
560 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
561 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
562 key->u.ccmp.rx_pn[i][j] =
563 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
564 /* Initialize AES key state here as an optimization so that
565 * it does not need to be initialized for every packet.
567 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
568 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
569 if (IS_ERR(key->u.ccmp.tfm)) {
570 err = PTR_ERR(key->u.ccmp.tfm);
575 case WLAN_CIPHER_SUITE_AES_CMAC:
576 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
577 key->conf.iv_len = 0;
578 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
579 key->conf.icv_len = sizeof(struct ieee80211_mmie);
581 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
583 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
584 key->u.aes_cmac.rx_pn[j] =
585 seq[IEEE80211_CMAC_PN_LEN - j - 1];
587 * Initialize AES key state here as an optimization so that
588 * it does not need to be initialized for every packet.
590 key->u.aes_cmac.tfm =
591 ieee80211_aes_cmac_key_setup(key_data, key_len);
592 if (IS_ERR(key->u.aes_cmac.tfm)) {
593 err = PTR_ERR(key->u.aes_cmac.tfm);
598 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
599 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
600 key->conf.iv_len = 0;
601 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
603 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
604 key->u.aes_gmac.rx_pn[j] =
605 seq[IEEE80211_GMAC_PN_LEN - j - 1];
606 /* Initialize AES key state here as an optimization so that
607 * it does not need to be initialized for every packet.
609 key->u.aes_gmac.tfm =
610 ieee80211_aes_gmac_key_setup(key_data, key_len);
611 if (IS_ERR(key->u.aes_gmac.tfm)) {
612 err = PTR_ERR(key->u.aes_gmac.tfm);
617 case WLAN_CIPHER_SUITE_GCMP:
618 case WLAN_CIPHER_SUITE_GCMP_256:
619 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
620 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
621 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
622 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
623 key->u.gcmp.rx_pn[i][j] =
624 seq[IEEE80211_GCMP_PN_LEN - j - 1];
625 /* Initialize AES key state here as an optimization so that
626 * it does not need to be initialized for every packet.
628 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
630 if (IS_ERR(key->u.gcmp.tfm)) {
631 err = PTR_ERR(key->u.gcmp.tfm);
638 if (seq_len && seq_len != cs->pn_len) {
640 return ERR_PTR(-EINVAL);
643 key->conf.iv_len = cs->hdr_len;
644 key->conf.icv_len = cs->mic_len;
645 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
646 for (j = 0; j < seq_len; j++)
647 key->u.gen.rx_pn[i][j] =
648 seq[seq_len - j - 1];
649 key->flags |= KEY_FLAG_CIPHER_SCHEME;
652 memcpy(key->conf.key, key_data, key_len);
653 INIT_LIST_HEAD(&key->list);
658 static void ieee80211_key_free_common(struct ieee80211_key *key)
660 switch (key->conf.cipher) {
661 case WLAN_CIPHER_SUITE_CCMP:
662 case WLAN_CIPHER_SUITE_CCMP_256:
663 ieee80211_aes_key_free(key->u.ccmp.tfm);
665 case WLAN_CIPHER_SUITE_AES_CMAC:
666 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
667 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
669 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
670 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
671 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
673 case WLAN_CIPHER_SUITE_GCMP:
674 case WLAN_CIPHER_SUITE_GCMP_256:
675 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
681 static void __ieee80211_key_destroy(struct ieee80211_key *key,
685 struct ieee80211_sub_if_data *sdata = key->sdata;
687 ieee80211_debugfs_key_remove(key);
689 if (delay_tailroom) {
690 /* see ieee80211_delayed_tailroom_dec */
691 sdata->crypto_tx_tailroom_pending_dec++;
692 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
695 decrease_tailroom_need_count(sdata, 1);
699 ieee80211_key_free_common(key);
702 static void ieee80211_key_destroy(struct ieee80211_key *key,
709 * Synchronize so the TX path and rcu key iterators
710 * can no longer be using this key before we free/remove it.
714 __ieee80211_key_destroy(key, delay_tailroom);
717 void ieee80211_key_free_unused(struct ieee80211_key *key)
719 WARN_ON(key->sdata || key->local);
720 ieee80211_key_free_common(key);
723 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
724 struct ieee80211_key *old,
725 struct ieee80211_key *new)
727 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
730 if (!old || new->conf.keylen != old->conf.keylen)
733 tk_old = old->conf.key;
734 tk_new = new->conf.key;
737 * In station mode, don't compare the TX MIC key, as it's never used
738 * and offloaded rekeying may not care to send it to the host. This
739 * is the case in iwlwifi, for example.
741 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
742 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
743 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
744 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
745 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
746 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
747 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
748 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
753 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
756 int ieee80211_key_link(struct ieee80211_key *key,
757 struct ieee80211_sub_if_data *sdata,
758 struct sta_info *sta)
760 struct ieee80211_key *old_key;
761 int idx = key->conf.keyidx;
762 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
764 * We want to delay tailroom updates only for station - in that
765 * case it helps roaming speed, but in other cases it hurts and
766 * can cause warnings to appear.
768 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
769 int ret = -EOPNOTSUPP;
771 mutex_lock(&sdata->local->key_mtx);
773 if (sta && pairwise) {
774 struct ieee80211_key *alt_key;
776 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
777 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
779 /* The rekey code assumes that the old and new key are using
780 * the same cipher. Enforce the assumption for pairwise keys.
783 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
784 (old_key && old_key->conf.cipher != key->conf.cipher)))
787 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
789 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
792 /* Non-pairwise keys must also not switch the cipher on rekey */
794 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
799 * Silently accept key re-installation without really installing the
800 * new version of the key to avoid nonce reuse or replay issues.
802 if (ieee80211_key_identical(sdata, old_key, key)) {
803 ieee80211_key_free_unused(key);
808 key->local = sdata->local;
812 increment_tailroom_need_count(sdata);
814 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
817 ieee80211_debugfs_key_add(key);
818 ieee80211_key_destroy(old_key, delay_tailroom);
820 ieee80211_key_free(key, delay_tailroom);
824 mutex_unlock(&sdata->local->key_mtx);
829 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
835 * Replace key with nothingness if it was ever used.
838 ieee80211_key_replace(key->sdata, key->sta,
839 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
841 ieee80211_key_destroy(key, delay_tailroom);
844 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
846 struct ieee80211_key *key;
847 struct ieee80211_sub_if_data *vlan;
851 if (WARN_ON(!ieee80211_sdata_running(sdata)))
854 mutex_lock(&sdata->local->key_mtx);
856 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
857 sdata->crypto_tx_tailroom_pending_dec);
859 if (sdata->vif.type == NL80211_IFTYPE_AP) {
860 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
861 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
862 vlan->crypto_tx_tailroom_pending_dec);
865 list_for_each_entry(key, &sdata->key_list, list) {
866 increment_tailroom_need_count(sdata);
867 ieee80211_key_enable_hw_accel(key);
870 mutex_unlock(&sdata->local->key_mtx);
873 void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
875 struct ieee80211_sub_if_data *vlan;
877 mutex_lock(&sdata->local->key_mtx);
879 sdata->crypto_tx_tailroom_needed_cnt = 0;
881 if (sdata->vif.type == NL80211_IFTYPE_AP) {
882 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
883 vlan->crypto_tx_tailroom_needed_cnt = 0;
886 mutex_unlock(&sdata->local->key_mtx);
889 void ieee80211_iter_keys(struct ieee80211_hw *hw,
890 struct ieee80211_vif *vif,
891 void (*iter)(struct ieee80211_hw *hw,
892 struct ieee80211_vif *vif,
893 struct ieee80211_sta *sta,
894 struct ieee80211_key_conf *key,
898 struct ieee80211_local *local = hw_to_local(hw);
899 struct ieee80211_key *key, *tmp;
900 struct ieee80211_sub_if_data *sdata;
904 mutex_lock(&local->key_mtx);
906 sdata = vif_to_sdata(vif);
907 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
908 iter(hw, &sdata->vif,
909 key->sta ? &key->sta->sta : NULL,
910 &key->conf, iter_data);
912 list_for_each_entry(sdata, &local->interfaces, list)
913 list_for_each_entry_safe(key, tmp,
914 &sdata->key_list, list)
915 iter(hw, &sdata->vif,
916 key->sta ? &key->sta->sta : NULL,
917 &key->conf, iter_data);
919 mutex_unlock(&local->key_mtx);
921 EXPORT_SYMBOL(ieee80211_iter_keys);
924 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
925 struct ieee80211_sub_if_data *sdata,
926 void (*iter)(struct ieee80211_hw *hw,
927 struct ieee80211_vif *vif,
928 struct ieee80211_sta *sta,
929 struct ieee80211_key_conf *key,
933 struct ieee80211_key *key;
935 list_for_each_entry_rcu(key, &sdata->key_list, list) {
936 /* skip keys of station in removal process */
937 if (key->sta && key->sta->removed)
939 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
942 iter(hw, &sdata->vif,
943 key->sta ? &key->sta->sta : NULL,
944 &key->conf, iter_data);
948 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
949 struct ieee80211_vif *vif,
950 void (*iter)(struct ieee80211_hw *hw,
951 struct ieee80211_vif *vif,
952 struct ieee80211_sta *sta,
953 struct ieee80211_key_conf *key,
957 struct ieee80211_local *local = hw_to_local(hw);
958 struct ieee80211_sub_if_data *sdata;
961 sdata = vif_to_sdata(vif);
962 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
964 list_for_each_entry_rcu(sdata, &local->interfaces, list)
965 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
968 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
970 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
971 struct list_head *keys)
973 struct ieee80211_key *key, *tmp;
975 decrease_tailroom_need_count(sdata,
976 sdata->crypto_tx_tailroom_pending_dec);
977 sdata->crypto_tx_tailroom_pending_dec = 0;
979 ieee80211_debugfs_key_remove_mgmt_default(sdata);
981 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
982 ieee80211_key_replace(key->sdata, key->sta,
983 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
985 list_add_tail(&key->list, keys);
988 ieee80211_debugfs_key_update_default(sdata);
991 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
992 bool force_synchronize)
994 struct ieee80211_local *local = sdata->local;
995 struct ieee80211_sub_if_data *vlan;
996 struct ieee80211_sub_if_data *master;
997 struct ieee80211_key *key, *tmp;
1000 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
1002 mutex_lock(&local->key_mtx);
1004 ieee80211_free_keys_iface(sdata, &keys);
1006 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1007 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1008 ieee80211_free_keys_iface(vlan, &keys);
1011 if (!list_empty(&keys) || force_synchronize)
1013 list_for_each_entry_safe(key, tmp, &keys, list)
1014 __ieee80211_key_destroy(key, false);
1016 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1018 master = container_of(sdata->bss,
1019 struct ieee80211_sub_if_data,
1022 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1023 master->crypto_tx_tailroom_needed_cnt);
1026 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1027 sdata->crypto_tx_tailroom_pending_dec);
1030 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1031 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1032 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1033 vlan->crypto_tx_tailroom_pending_dec);
1036 mutex_unlock(&local->key_mtx);
1039 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1040 struct sta_info *sta)
1042 struct ieee80211_key *key;
1045 mutex_lock(&local->key_mtx);
1046 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
1047 key = key_mtx_dereference(local, sta->gtk[i]);
1050 ieee80211_key_replace(key->sdata, key->sta,
1051 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1053 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1054 NL80211_IFTYPE_STATION);
1057 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1058 key = key_mtx_dereference(local, sta->ptk[i]);
1061 ieee80211_key_replace(key->sdata, key->sta,
1062 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1064 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1065 NL80211_IFTYPE_STATION);
1068 mutex_unlock(&local->key_mtx);
1071 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1073 struct ieee80211_sub_if_data *sdata;
1075 sdata = container_of(wk, struct ieee80211_sub_if_data,
1076 dec_tailroom_needed_wk.work);
1079 * The reason for the delayed tailroom needed decrementing is to
1080 * make roaming faster: during roaming, all keys are first deleted
1081 * and then new keys are installed. The first new key causes the
1082 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1083 * the cost of synchronize_net() (which can be slow). Avoid this
1084 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1085 * key removal for a while, so if we roam the value is larger than
1086 * zero and no 0->1 transition happens.
1088 * The cost is that if the AP switching was from an AP with keys
1089 * to one without, we still allocate tailroom while it would no
1090 * longer be needed. However, in the typical (fast) roaming case
1091 * within an ESS this usually won't happen.
1094 mutex_lock(&sdata->local->key_mtx);
1095 decrease_tailroom_need_count(sdata,
1096 sdata->crypto_tx_tailroom_pending_dec);
1097 sdata->crypto_tx_tailroom_pending_dec = 0;
1098 mutex_unlock(&sdata->local->key_mtx);
1101 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1102 const u8 *replay_ctr, gfp_t gfp)
1104 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1106 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1108 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1110 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1112 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1113 int tid, struct ieee80211_key_seq *seq)
1115 struct ieee80211_key *key;
1118 key = container_of(keyconf, struct ieee80211_key, conf);
1120 switch (key->conf.cipher) {
1121 case WLAN_CIPHER_SUITE_TKIP:
1122 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1124 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1125 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1127 case WLAN_CIPHER_SUITE_CCMP:
1128 case WLAN_CIPHER_SUITE_CCMP_256:
1129 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1132 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1134 pn = key->u.ccmp.rx_pn[tid];
1135 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1137 case WLAN_CIPHER_SUITE_AES_CMAC:
1138 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1139 if (WARN_ON(tid != 0))
1141 pn = key->u.aes_cmac.rx_pn;
1142 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1144 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1145 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1146 if (WARN_ON(tid != 0))
1148 pn = key->u.aes_gmac.rx_pn;
1149 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1151 case WLAN_CIPHER_SUITE_GCMP:
1152 case WLAN_CIPHER_SUITE_GCMP_256:
1153 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1156 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1158 pn = key->u.gcmp.rx_pn[tid];
1159 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1163 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1165 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1166 int tid, struct ieee80211_key_seq *seq)
1168 struct ieee80211_key *key;
1171 key = container_of(keyconf, struct ieee80211_key, conf);
1173 switch (key->conf.cipher) {
1174 case WLAN_CIPHER_SUITE_TKIP:
1175 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1177 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1178 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1180 case WLAN_CIPHER_SUITE_CCMP:
1181 case WLAN_CIPHER_SUITE_CCMP_256:
1182 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1185 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1187 pn = key->u.ccmp.rx_pn[tid];
1188 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1190 case WLAN_CIPHER_SUITE_AES_CMAC:
1191 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1192 if (WARN_ON(tid != 0))
1194 pn = key->u.aes_cmac.rx_pn;
1195 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1197 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1198 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1199 if (WARN_ON(tid != 0))
1201 pn = key->u.aes_gmac.rx_pn;
1202 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1204 case WLAN_CIPHER_SUITE_GCMP:
1205 case WLAN_CIPHER_SUITE_GCMP_256:
1206 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1209 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1211 pn = key->u.gcmp.rx_pn[tid];
1212 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1219 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1221 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1223 struct ieee80211_key *key;
1225 key = container_of(keyconf, struct ieee80211_key, conf);
1227 assert_key_lock(key->local);
1230 * if key was uploaded, we assume the driver will/has remove(d)
1231 * it, so adjust bookkeeping accordingly
1233 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1234 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1236 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1237 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1238 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1239 increment_tailroom_need_count(key->sdata);
1242 ieee80211_key_free(key, false);
1244 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1246 struct ieee80211_key_conf *
1247 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1248 struct ieee80211_key_conf *keyconf)
1250 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1251 struct ieee80211_local *local = sdata->local;
1252 struct ieee80211_key *key;
1255 if (WARN_ON(!local->wowlan))
1256 return ERR_PTR(-EINVAL);
1258 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1259 return ERR_PTR(-EINVAL);
1261 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1262 keyconf->keylen, keyconf->key,
1265 return ERR_CAST(key);
1267 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1268 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1270 err = ieee80211_key_link(key, sdata, NULL);
1272 return ERR_PTR(err);
1276 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);