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
9 * Copyright 2018-2020, 2022-2023 Intel Corporation
12 #include <crypto/utils.h>
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 /* crypto_tx_tailroom_needed_cnt is protected by this */
70 assert_key_lock(sdata->local);
74 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
75 vlan->crypto_tx_tailroom_needed_cnt += delta;
80 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
83 * When this count is zero, SKB resizing for allocating tailroom
84 * for IV or MMIC is skipped. But, this check has created two race
85 * cases in xmit path while transiting from zero count to one:
87 * 1. SKB resize was skipped because no key was added but just before
88 * the xmit key is added and SW encryption kicks off.
90 * 2. SKB resize was skipped because all the keys were hw planted but
91 * just before xmit one of the key is deleted and SW encryption kicks
94 * In both the above case SW encryption will find not enough space for
95 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
97 * Solution has been explained at
98 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
101 assert_key_lock(sdata->local);
103 update_vlan_tailroom_need_count(sdata, 1);
105 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
107 * Flush all XMIT packets currently using HW encryption or no
108 * encryption at all if the count transition is from 0 -> 1.
114 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
117 assert_key_lock(sdata->local);
119 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
121 update_vlan_tailroom_need_count(sdata, -delta);
122 sdata->crypto_tx_tailroom_needed_cnt -= delta;
125 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
127 struct ieee80211_sub_if_data *sdata = key->sdata;
128 struct sta_info *sta;
129 int ret = -EOPNOTSUPP;
133 if (key->flags & KEY_FLAG_TAINTED) {
134 /* If we get here, it's during resume and the key is
135 * tainted so shouldn't be used/programmed any more.
136 * However, its flags may still indicate that it was
137 * programmed into the device (since we're in resume)
138 * so clear that flag now to avoid trying to remove
141 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
142 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
143 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
144 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
145 increment_tailroom_need_count(sdata);
147 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
151 if (!key->local->ops->set_key)
152 goto out_unsupported;
154 assert_key_lock(key->local);
159 * If this is a per-STA GTK, check if it
160 * is supported; if not, return.
162 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
163 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
164 goto out_unsupported;
166 if (sta && !sta->uploaded)
167 goto out_unsupported;
169 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
171 * The driver doesn't know anything about VLAN interfaces.
172 * Hence, don't send GTKs for VLAN interfaces to the driver.
174 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
176 goto out_unsupported;
180 if (key->conf.link_id >= 0 && sdata->vif.active_links &&
181 !(sdata->vif.active_links & BIT(key->conf.link_id)))
184 ret = drv_set_key(key->local, SET_KEY, sdata,
185 sta ? &sta->sta : NULL, &key->conf);
188 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
190 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
191 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
192 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
193 decrease_tailroom_need_count(sdata, 1);
195 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
196 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
198 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
199 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
204 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
206 "failed to set key (%d, %pM) to hardware (%d)\n",
208 sta ? sta->sta.addr : bcast_addr, ret);
211 switch (key->conf.cipher) {
212 case WLAN_CIPHER_SUITE_WEP40:
213 case WLAN_CIPHER_SUITE_WEP104:
214 case WLAN_CIPHER_SUITE_TKIP:
215 case WLAN_CIPHER_SUITE_CCMP:
216 case WLAN_CIPHER_SUITE_CCMP_256:
217 case WLAN_CIPHER_SUITE_GCMP:
218 case WLAN_CIPHER_SUITE_GCMP_256:
219 case WLAN_CIPHER_SUITE_AES_CMAC:
220 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
221 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
222 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
223 /* all of these we can do in software - if driver can */
226 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
234 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
236 struct ieee80211_sub_if_data *sdata;
237 struct sta_info *sta;
242 if (!key || !key->local->ops->set_key)
245 assert_key_lock(key->local);
247 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
253 if (key->conf.link_id >= 0 && sdata->vif.active_links &&
254 !(sdata->vif.active_links & BIT(key->conf.link_id)))
257 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
258 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
259 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
260 increment_tailroom_need_count(sdata);
262 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
263 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
264 sta ? &sta->sta : NULL, &key->conf);
268 "failed to remove key (%d, %pM) from hardware (%d)\n",
270 sta ? sta->sta.addr : bcast_addr, ret);
273 static int _ieee80211_set_tx_key(struct ieee80211_key *key, bool force)
275 struct sta_info *sta = key->sta;
276 struct ieee80211_local *local = key->local;
278 assert_key_lock(local);
280 set_sta_flag(sta, WLAN_STA_USES_ENCRYPTION);
282 sta->ptk_idx = key->conf.keyidx;
284 if (force || !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT))
285 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
286 ieee80211_check_fast_xmit(sta);
291 int ieee80211_set_tx_key(struct ieee80211_key *key)
293 return _ieee80211_set_tx_key(key, false);
296 static void ieee80211_pairwise_rekey(struct ieee80211_key *old,
297 struct ieee80211_key *new)
299 struct ieee80211_local *local = new->local;
300 struct sta_info *sta = new->sta;
303 assert_key_lock(local);
305 if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) {
306 /* Extended Key ID key install, initial one or rekey */
308 if (sta->ptk_idx != INVALID_PTK_KEYIDX &&
309 !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) {
310 /* Aggregation Sessions with Extended Key ID must not
311 * mix MPDUs with different keyIDs within one A-MPDU.
312 * Tear down running Tx aggregation sessions and block
313 * new Rx/Tx aggregation requests during rekey to
314 * ensure there are no A-MPDUs when the driver is not
315 * supporting A-MPDU key borders. (Blocking Tx only
316 * would be sufficient but WLAN_STA_BLOCK_BA gets the
317 * job done for the few ms we need it.)
319 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
320 mutex_lock(&sta->ampdu_mlme.mtx);
321 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
322 ___ieee80211_stop_tx_ba_session(sta, i,
323 AGG_STOP_LOCAL_REQUEST);
324 mutex_unlock(&sta->ampdu_mlme.mtx);
327 /* Rekey without Extended Key ID.
328 * Aggregation sessions are OK when running on SW crypto.
329 * A broken remote STA may cause issues not observed with HW
332 if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
335 /* Stop Tx till we are on the new key */
336 old->flags |= KEY_FLAG_TAINTED;
337 ieee80211_clear_fast_xmit(sta);
338 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
339 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
340 ieee80211_sta_tear_down_BA_sessions(sta,
341 AGG_STOP_LOCAL_REQUEST);
343 if (!wiphy_ext_feature_isset(local->hw.wiphy,
344 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
345 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
347 /* Flushing the driver queues *may* help prevent
348 * the clear text leaks and freezes.
350 ieee80211_flush_queues(local, old->sdata, false);
355 static void __ieee80211_set_default_key(struct ieee80211_link_data *link,
356 int idx, bool uni, bool multi)
358 struct ieee80211_sub_if_data *sdata = link->sdata;
359 struct ieee80211_key *key = NULL;
361 assert_key_lock(sdata->local);
363 if (idx >= 0 && idx < NUM_DEFAULT_KEYS) {
364 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
366 key = key_mtx_dereference(sdata->local, link->gtk[idx]);
370 rcu_assign_pointer(sdata->default_unicast_key, key);
371 ieee80211_check_fast_xmit_iface(sdata);
372 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
373 drv_set_default_unicast_key(sdata->local, sdata, idx);
377 rcu_assign_pointer(link->default_multicast_key, key);
379 ieee80211_debugfs_key_update_default(sdata);
382 void ieee80211_set_default_key(struct ieee80211_link_data *link, int idx,
383 bool uni, bool multi)
385 mutex_lock(&link->sdata->local->key_mtx);
386 __ieee80211_set_default_key(link, idx, uni, multi);
387 mutex_unlock(&link->sdata->local->key_mtx);
391 __ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link, int idx)
393 struct ieee80211_sub_if_data *sdata = link->sdata;
394 struct ieee80211_key *key = NULL;
396 assert_key_lock(sdata->local);
398 if (idx >= NUM_DEFAULT_KEYS &&
399 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
400 key = key_mtx_dereference(sdata->local, link->gtk[idx]);
402 rcu_assign_pointer(link->default_mgmt_key, key);
404 ieee80211_debugfs_key_update_default(sdata);
407 void ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link,
410 mutex_lock(&link->sdata->local->key_mtx);
411 __ieee80211_set_default_mgmt_key(link, idx);
412 mutex_unlock(&link->sdata->local->key_mtx);
416 __ieee80211_set_default_beacon_key(struct ieee80211_link_data *link, int idx)
418 struct ieee80211_sub_if_data *sdata = link->sdata;
419 struct ieee80211_key *key = NULL;
421 assert_key_lock(sdata->local);
423 if (idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS &&
424 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
425 NUM_DEFAULT_BEACON_KEYS)
426 key = key_mtx_dereference(sdata->local, link->gtk[idx]);
428 rcu_assign_pointer(link->default_beacon_key, key);
430 ieee80211_debugfs_key_update_default(sdata);
433 void ieee80211_set_default_beacon_key(struct ieee80211_link_data *link,
436 mutex_lock(&link->sdata->local->key_mtx);
437 __ieee80211_set_default_beacon_key(link, idx);
438 mutex_unlock(&link->sdata->local->key_mtx);
441 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
442 struct ieee80211_link_data *link,
443 struct sta_info *sta,
445 struct ieee80211_key *old,
446 struct ieee80211_key *new)
448 struct link_sta_info *link_sta = sta ? &sta->deflink : NULL;
452 bool defunikey, defmultikey, defmgmtkey, defbeaconkey;
455 /* caller must provide at least one old/new */
456 if (WARN_ON(!new && !old))
460 idx = new->conf.keyidx;
461 is_wep = new->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||
462 new->conf.cipher == WLAN_CIPHER_SUITE_WEP104;
463 link_id = new->conf.link_id;
465 idx = old->conf.keyidx;
466 is_wep = old->conf.cipher == WLAN_CIPHER_SUITE_WEP40 ||
467 old->conf.cipher == WLAN_CIPHER_SUITE_WEP104;
468 link_id = old->conf.link_id;
471 if (WARN(old && old->conf.link_id != link_id,
472 "old link ID %d doesn't match new link ID %d\n",
473 old->conf.link_id, link_id))
478 link = sdata_dereference(sdata->link[link_id], sdata);
484 link_sta = rcu_dereference_protected(sta->link[link_id],
485 lockdep_is_held(&sta->local->sta_mtx));
490 link = &sdata->deflink;
493 if ((is_wep || pairwise) && idx >= NUM_DEFAULT_KEYS)
496 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
498 if (new && sta && pairwise) {
499 /* Unicast rekey needs special handling. With Extended Key ID
500 * old is still NULL for the first rekey.
502 ieee80211_pairwise_rekey(old, new);
506 if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
507 ieee80211_key_disable_hw_accel(old);
510 ret = ieee80211_key_enable_hw_accel(new);
513 if (!new->local->wowlan) {
514 ret = ieee80211_key_enable_hw_accel(new);
516 assert_key_lock(new->local);
517 new->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
525 list_add_tail_rcu(&new->list, &sdata->key_list);
529 rcu_assign_pointer(sta->ptk[idx], new);
531 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX))
532 _ieee80211_set_tx_key(new, true);
534 rcu_assign_pointer(link_sta->gtk[idx], new);
536 /* Only needed for transition from no key -> key.
537 * Still triggers unnecessary when using Extended Key ID
538 * and installing the second key ID the first time.
541 ieee80211_check_fast_rx(sta);
544 old == key_mtx_dereference(sdata->local,
545 sdata->default_unicast_key);
547 old == key_mtx_dereference(sdata->local,
548 link->default_multicast_key);
550 old == key_mtx_dereference(sdata->local,
551 link->default_mgmt_key);
552 defbeaconkey = old &&
553 old == key_mtx_dereference(sdata->local,
554 link->default_beacon_key);
556 if (defunikey && !new)
557 __ieee80211_set_default_key(link, -1, true, false);
558 if (defmultikey && !new)
559 __ieee80211_set_default_key(link, -1, false, true);
560 if (defmgmtkey && !new)
561 __ieee80211_set_default_mgmt_key(link, -1);
562 if (defbeaconkey && !new)
563 __ieee80211_set_default_beacon_key(link, -1);
565 if (is_wep || pairwise)
566 rcu_assign_pointer(sdata->keys[idx], new);
568 rcu_assign_pointer(link->gtk[idx], new);
570 if (defunikey && new)
571 __ieee80211_set_default_key(link, new->conf.keyidx,
573 if (defmultikey && new)
574 __ieee80211_set_default_key(link, new->conf.keyidx,
576 if (defmgmtkey && new)
577 __ieee80211_set_default_mgmt_key(link,
579 if (defbeaconkey && new)
580 __ieee80211_set_default_beacon_key(link,
585 list_del_rcu(&old->list);
590 struct ieee80211_key *
591 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
593 size_t seq_len, const u8 *seq)
595 struct ieee80211_key *key;
598 if (WARN_ON(idx < 0 ||
599 idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
600 NUM_DEFAULT_BEACON_KEYS))
601 return ERR_PTR(-EINVAL);
603 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
605 return ERR_PTR(-ENOMEM);
608 * Default to software encryption; we'll later upload the
609 * key to the hardware if possible.
614 key->conf.link_id = -1;
615 key->conf.cipher = cipher;
616 key->conf.keyidx = idx;
617 key->conf.keylen = key_len;
619 case WLAN_CIPHER_SUITE_WEP40:
620 case WLAN_CIPHER_SUITE_WEP104:
621 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
622 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
624 case WLAN_CIPHER_SUITE_TKIP:
625 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
626 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
628 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
629 key->u.tkip.rx[i].iv32 =
630 get_unaligned_le32(&seq[2]);
631 key->u.tkip.rx[i].iv16 =
632 get_unaligned_le16(seq);
635 spin_lock_init(&key->u.tkip.txlock);
637 case WLAN_CIPHER_SUITE_CCMP:
638 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
639 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
641 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
642 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
643 key->u.ccmp.rx_pn[i][j] =
644 seq[IEEE80211_CCMP_PN_LEN - j - 1];
647 * Initialize AES key state here as an optimization so that
648 * it does not need to be initialized for every packet.
650 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
651 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
652 if (IS_ERR(key->u.ccmp.tfm)) {
653 err = PTR_ERR(key->u.ccmp.tfm);
658 case WLAN_CIPHER_SUITE_CCMP_256:
659 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
660 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
661 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
662 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
663 key->u.ccmp.rx_pn[i][j] =
664 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
665 /* Initialize AES key state here as an optimization so that
666 * it does not need to be initialized for every packet.
668 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
669 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
670 if (IS_ERR(key->u.ccmp.tfm)) {
671 err = PTR_ERR(key->u.ccmp.tfm);
676 case WLAN_CIPHER_SUITE_AES_CMAC:
677 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
678 key->conf.iv_len = 0;
679 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
680 key->conf.icv_len = sizeof(struct ieee80211_mmie);
682 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
684 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
685 key->u.aes_cmac.rx_pn[j] =
686 seq[IEEE80211_CMAC_PN_LEN - j - 1];
688 * Initialize AES key state here as an optimization so that
689 * it does not need to be initialized for every packet.
691 key->u.aes_cmac.tfm =
692 ieee80211_aes_cmac_key_setup(key_data, key_len);
693 if (IS_ERR(key->u.aes_cmac.tfm)) {
694 err = PTR_ERR(key->u.aes_cmac.tfm);
699 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
700 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
701 key->conf.iv_len = 0;
702 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
704 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
705 key->u.aes_gmac.rx_pn[j] =
706 seq[IEEE80211_GMAC_PN_LEN - j - 1];
707 /* Initialize AES key state here as an optimization so that
708 * it does not need to be initialized for every packet.
710 key->u.aes_gmac.tfm =
711 ieee80211_aes_gmac_key_setup(key_data, key_len);
712 if (IS_ERR(key->u.aes_gmac.tfm)) {
713 err = PTR_ERR(key->u.aes_gmac.tfm);
718 case WLAN_CIPHER_SUITE_GCMP:
719 case WLAN_CIPHER_SUITE_GCMP_256:
720 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
721 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
722 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
723 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
724 key->u.gcmp.rx_pn[i][j] =
725 seq[IEEE80211_GCMP_PN_LEN - j - 1];
726 /* Initialize AES key state here as an optimization so that
727 * it does not need to be initialized for every packet.
729 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
731 if (IS_ERR(key->u.gcmp.tfm)) {
732 err = PTR_ERR(key->u.gcmp.tfm);
738 memcpy(key->conf.key, key_data, key_len);
739 INIT_LIST_HEAD(&key->list);
744 static void ieee80211_key_free_common(struct ieee80211_key *key)
746 switch (key->conf.cipher) {
747 case WLAN_CIPHER_SUITE_CCMP:
748 case WLAN_CIPHER_SUITE_CCMP_256:
749 ieee80211_aes_key_free(key->u.ccmp.tfm);
751 case WLAN_CIPHER_SUITE_AES_CMAC:
752 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
753 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
755 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
756 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
757 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
759 case WLAN_CIPHER_SUITE_GCMP:
760 case WLAN_CIPHER_SUITE_GCMP_256:
761 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
764 kfree_sensitive(key);
767 static void __ieee80211_key_destroy(struct ieee80211_key *key,
771 struct ieee80211_sub_if_data *sdata = key->sdata;
773 ieee80211_debugfs_key_remove(key);
775 if (delay_tailroom) {
776 /* see ieee80211_delayed_tailroom_dec */
777 sdata->crypto_tx_tailroom_pending_dec++;
778 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
781 decrease_tailroom_need_count(sdata, 1);
785 ieee80211_key_free_common(key);
788 static void ieee80211_key_destroy(struct ieee80211_key *key,
795 * Synchronize so the TX path and rcu key iterators
796 * can no longer be using this key before we free/remove it.
800 __ieee80211_key_destroy(key, delay_tailroom);
803 void ieee80211_key_free_unused(struct ieee80211_key *key)
805 WARN_ON(key->sdata || key->local);
806 ieee80211_key_free_common(key);
809 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
810 struct ieee80211_key *old,
811 struct ieee80211_key *new)
813 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
816 if (!old || new->conf.keylen != old->conf.keylen)
819 tk_old = old->conf.key;
820 tk_new = new->conf.key;
823 * In station mode, don't compare the TX MIC key, as it's never used
824 * and offloaded rekeying may not care to send it to the host. This
825 * is the case in iwlwifi, for example.
827 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
828 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
829 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
830 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
831 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
832 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
833 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
834 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
839 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
842 int ieee80211_key_link(struct ieee80211_key *key,
843 struct ieee80211_link_data *link,
844 struct sta_info *sta)
846 struct ieee80211_sub_if_data *sdata = link->sdata;
847 static atomic_t key_color = ATOMIC_INIT(0);
848 struct ieee80211_key *old_key = NULL;
849 int idx = key->conf.keyidx;
850 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
852 * We want to delay tailroom updates only for station - in that
853 * case it helps roaming speed, but in other cases it hurts and
854 * can cause warnings to appear.
856 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
857 int ret = -EOPNOTSUPP;
859 mutex_lock(&sdata->local->key_mtx);
861 if (sta && pairwise) {
862 struct ieee80211_key *alt_key;
864 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
865 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
867 /* The rekey code assumes that the old and new key are using
868 * the same cipher. Enforce the assumption for pairwise keys.
870 if ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
871 (old_key && old_key->conf.cipher != key->conf.cipher))
874 struct link_sta_info *link_sta = &sta->deflink;
875 int link_id = key->conf.link_id;
878 link_sta = rcu_dereference_protected(sta->link[link_id],
879 lockdep_is_held(&sta->local->sta_mtx));
886 old_key = key_mtx_dereference(sdata->local, link_sta->gtk[idx]);
888 if (idx < NUM_DEFAULT_KEYS)
889 old_key = key_mtx_dereference(sdata->local,
892 old_key = key_mtx_dereference(sdata->local,
896 /* Non-pairwise keys must also not switch the cipher on rekey */
898 if (old_key && old_key->conf.cipher != key->conf.cipher)
903 * Silently accept key re-installation without really installing the
904 * new version of the key to avoid nonce reuse or replay issues.
906 if (ieee80211_key_identical(sdata, old_key, key)) {
907 ieee80211_key_free_unused(key);
912 key->local = sdata->local;
917 * Assign a unique ID to every key so we can easily prevent mixed
918 * key and fragment cache attacks.
920 key->color = atomic_inc_return(&key_color);
922 increment_tailroom_need_count(sdata);
924 ret = ieee80211_key_replace(sdata, link, sta, pairwise, old_key, key);
927 ieee80211_debugfs_key_add(key);
928 ieee80211_key_destroy(old_key, delay_tailroom);
930 ieee80211_key_free(key, delay_tailroom);
934 mutex_unlock(&sdata->local->key_mtx);
939 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
945 * Replace key with nothingness if it was ever used.
948 ieee80211_key_replace(key->sdata, NULL, key->sta,
949 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
951 ieee80211_key_destroy(key, delay_tailroom);
954 void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata)
956 struct ieee80211_key *key;
957 struct ieee80211_sub_if_data *vlan;
959 lockdep_assert_wiphy(sdata->local->hw.wiphy);
961 mutex_lock(&sdata->local->key_mtx);
963 sdata->crypto_tx_tailroom_needed_cnt = 0;
964 sdata->crypto_tx_tailroom_pending_dec = 0;
966 if (sdata->vif.type == NL80211_IFTYPE_AP) {
967 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
968 vlan->crypto_tx_tailroom_needed_cnt = 0;
969 vlan->crypto_tx_tailroom_pending_dec = 0;
973 if (ieee80211_sdata_running(sdata)) {
974 list_for_each_entry(key, &sdata->key_list, list) {
975 increment_tailroom_need_count(sdata);
976 ieee80211_key_enable_hw_accel(key);
980 mutex_unlock(&sdata->local->key_mtx);
983 void ieee80211_iter_keys(struct ieee80211_hw *hw,
984 struct ieee80211_vif *vif,
985 void (*iter)(struct ieee80211_hw *hw,
986 struct ieee80211_vif *vif,
987 struct ieee80211_sta *sta,
988 struct ieee80211_key_conf *key,
992 struct ieee80211_local *local = hw_to_local(hw);
993 struct ieee80211_key *key, *tmp;
994 struct ieee80211_sub_if_data *sdata;
996 lockdep_assert_wiphy(hw->wiphy);
998 mutex_lock(&local->key_mtx);
1000 sdata = vif_to_sdata(vif);
1001 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
1002 iter(hw, &sdata->vif,
1003 key->sta ? &key->sta->sta : NULL,
1004 &key->conf, iter_data);
1006 list_for_each_entry(sdata, &local->interfaces, list)
1007 list_for_each_entry_safe(key, tmp,
1008 &sdata->key_list, list)
1009 iter(hw, &sdata->vif,
1010 key->sta ? &key->sta->sta : NULL,
1011 &key->conf, iter_data);
1013 mutex_unlock(&local->key_mtx);
1015 EXPORT_SYMBOL(ieee80211_iter_keys);
1018 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
1019 struct ieee80211_sub_if_data *sdata,
1020 void (*iter)(struct ieee80211_hw *hw,
1021 struct ieee80211_vif *vif,
1022 struct ieee80211_sta *sta,
1023 struct ieee80211_key_conf *key,
1027 struct ieee80211_key *key;
1029 list_for_each_entry_rcu(key, &sdata->key_list, list) {
1030 /* skip keys of station in removal process */
1031 if (key->sta && key->sta->removed)
1033 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
1036 iter(hw, &sdata->vif,
1037 key->sta ? &key->sta->sta : NULL,
1038 &key->conf, iter_data);
1042 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
1043 struct ieee80211_vif *vif,
1044 void (*iter)(struct ieee80211_hw *hw,
1045 struct ieee80211_vif *vif,
1046 struct ieee80211_sta *sta,
1047 struct ieee80211_key_conf *key,
1051 struct ieee80211_local *local = hw_to_local(hw);
1052 struct ieee80211_sub_if_data *sdata;
1055 sdata = vif_to_sdata(vif);
1056 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
1058 list_for_each_entry_rcu(sdata, &local->interfaces, list)
1059 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
1062 EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
1064 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
1065 struct list_head *keys)
1067 struct ieee80211_key *key, *tmp;
1069 decrease_tailroom_need_count(sdata,
1070 sdata->crypto_tx_tailroom_pending_dec);
1071 sdata->crypto_tx_tailroom_pending_dec = 0;
1073 ieee80211_debugfs_key_remove_mgmt_default(sdata);
1074 ieee80211_debugfs_key_remove_beacon_default(sdata);
1076 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
1077 ieee80211_key_replace(key->sdata, NULL, key->sta,
1078 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1080 list_add_tail(&key->list, keys);
1083 ieee80211_debugfs_key_update_default(sdata);
1086 void ieee80211_remove_link_keys(struct ieee80211_link_data *link,
1087 struct list_head *keys)
1089 struct ieee80211_sub_if_data *sdata = link->sdata;
1090 struct ieee80211_local *local = sdata->local;
1091 struct ieee80211_key *key, *tmp;
1093 mutex_lock(&local->key_mtx);
1094 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
1095 if (key->conf.link_id != link->link_id)
1097 ieee80211_key_replace(key->sdata, link, key->sta,
1098 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1100 list_add_tail(&key->list, keys);
1102 mutex_unlock(&local->key_mtx);
1105 void ieee80211_free_key_list(struct ieee80211_local *local,
1106 struct list_head *keys)
1108 struct ieee80211_key *key, *tmp;
1110 mutex_lock(&local->key_mtx);
1111 list_for_each_entry_safe(key, tmp, keys, list)
1112 __ieee80211_key_destroy(key, false);
1113 mutex_unlock(&local->key_mtx);
1116 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
1117 bool force_synchronize)
1119 struct ieee80211_local *local = sdata->local;
1120 struct ieee80211_sub_if_data *vlan;
1121 struct ieee80211_sub_if_data *master;
1122 struct ieee80211_key *key, *tmp;
1125 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
1127 mutex_lock(&local->key_mtx);
1129 ieee80211_free_keys_iface(sdata, &keys);
1131 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1132 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1133 ieee80211_free_keys_iface(vlan, &keys);
1136 if (!list_empty(&keys) || force_synchronize)
1138 list_for_each_entry_safe(key, tmp, &keys, list)
1139 __ieee80211_key_destroy(key, false);
1141 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1143 master = container_of(sdata->bss,
1144 struct ieee80211_sub_if_data,
1147 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1148 master->crypto_tx_tailroom_needed_cnt);
1151 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1152 sdata->crypto_tx_tailroom_pending_dec);
1155 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1156 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1157 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1158 vlan->crypto_tx_tailroom_pending_dec);
1161 mutex_unlock(&local->key_mtx);
1164 void ieee80211_free_sta_keys(struct ieee80211_local *local,
1165 struct sta_info *sta)
1167 struct ieee80211_key *key;
1170 mutex_lock(&local->key_mtx);
1171 for (i = 0; i < ARRAY_SIZE(sta->deflink.gtk); i++) {
1172 key = key_mtx_dereference(local, sta->deflink.gtk[i]);
1175 ieee80211_key_replace(key->sdata, NULL, key->sta,
1176 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1178 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1179 NL80211_IFTYPE_STATION);
1182 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1183 key = key_mtx_dereference(local, sta->ptk[i]);
1186 ieee80211_key_replace(key->sdata, NULL, key->sta,
1187 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1189 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1190 NL80211_IFTYPE_STATION);
1193 mutex_unlock(&local->key_mtx);
1196 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1198 struct ieee80211_sub_if_data *sdata;
1200 sdata = container_of(wk, struct ieee80211_sub_if_data,
1201 dec_tailroom_needed_wk.work);
1204 * The reason for the delayed tailroom needed decrementing is to
1205 * make roaming faster: during roaming, all keys are first deleted
1206 * and then new keys are installed. The first new key causes the
1207 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1208 * the cost of synchronize_net() (which can be slow). Avoid this
1209 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1210 * key removal for a while, so if we roam the value is larger than
1211 * zero and no 0->1 transition happens.
1213 * The cost is that if the AP switching was from an AP with keys
1214 * to one without, we still allocate tailroom while it would no
1215 * longer be needed. However, in the typical (fast) roaming case
1216 * within an ESS this usually won't happen.
1219 mutex_lock(&sdata->local->key_mtx);
1220 decrease_tailroom_need_count(sdata,
1221 sdata->crypto_tx_tailroom_pending_dec);
1222 sdata->crypto_tx_tailroom_pending_dec = 0;
1223 mutex_unlock(&sdata->local->key_mtx);
1226 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1227 const u8 *replay_ctr, gfp_t gfp)
1229 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1231 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1233 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1235 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
1237 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1238 int tid, struct ieee80211_key_seq *seq)
1240 struct ieee80211_key *key;
1243 key = container_of(keyconf, struct ieee80211_key, conf);
1245 switch (key->conf.cipher) {
1246 case WLAN_CIPHER_SUITE_TKIP:
1247 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1249 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1250 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1252 case WLAN_CIPHER_SUITE_CCMP:
1253 case WLAN_CIPHER_SUITE_CCMP_256:
1254 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1257 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1259 pn = key->u.ccmp.rx_pn[tid];
1260 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
1262 case WLAN_CIPHER_SUITE_AES_CMAC:
1263 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1264 if (WARN_ON(tid != 0))
1266 pn = key->u.aes_cmac.rx_pn;
1267 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
1269 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1270 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1271 if (WARN_ON(tid != 0))
1273 pn = key->u.aes_gmac.rx_pn;
1274 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1276 case WLAN_CIPHER_SUITE_GCMP:
1277 case WLAN_CIPHER_SUITE_GCMP_256:
1278 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1281 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1283 pn = key->u.gcmp.rx_pn[tid];
1284 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1288 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
1290 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1291 int tid, struct ieee80211_key_seq *seq)
1293 struct ieee80211_key *key;
1296 key = container_of(keyconf, struct ieee80211_key, conf);
1298 switch (key->conf.cipher) {
1299 case WLAN_CIPHER_SUITE_TKIP:
1300 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1302 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1303 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1305 case WLAN_CIPHER_SUITE_CCMP:
1306 case WLAN_CIPHER_SUITE_CCMP_256:
1307 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1310 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1312 pn = key->u.ccmp.rx_pn[tid];
1313 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1315 case WLAN_CIPHER_SUITE_AES_CMAC:
1316 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1317 if (WARN_ON(tid != 0))
1319 pn = key->u.aes_cmac.rx_pn;
1320 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1322 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1323 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1324 if (WARN_ON(tid != 0))
1326 pn = key->u.aes_gmac.rx_pn;
1327 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1329 case WLAN_CIPHER_SUITE_GCMP:
1330 case WLAN_CIPHER_SUITE_GCMP_256:
1331 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1334 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1336 pn = key->u.gcmp.rx_pn[tid];
1337 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1344 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1346 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1348 struct ieee80211_key *key;
1350 key = container_of(keyconf, struct ieee80211_key, conf);
1352 assert_key_lock(key->local);
1355 * if key was uploaded, we assume the driver will/has remove(d)
1356 * it, so adjust bookkeeping accordingly
1358 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1359 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1361 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1362 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1363 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1364 increment_tailroom_need_count(key->sdata);
1367 ieee80211_key_free(key, false);
1369 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1371 struct ieee80211_key_conf *
1372 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1373 struct ieee80211_key_conf *keyconf)
1375 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1376 struct ieee80211_local *local = sdata->local;
1377 struct ieee80211_key *key;
1380 if (WARN_ON(!local->wowlan))
1381 return ERR_PTR(-EINVAL);
1383 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1384 return ERR_PTR(-EINVAL);
1386 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1387 keyconf->keylen, keyconf->key,
1390 return ERR_CAST(key);
1392 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1393 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1395 /* FIXME: this function needs to get a link ID */
1396 err = ieee80211_key_link(key, &sdata->deflink, NULL);
1398 return ERR_PTR(err);
1402 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);
1404 void ieee80211_key_mic_failure(struct ieee80211_key_conf *keyconf)
1406 struct ieee80211_key *key;
1408 key = container_of(keyconf, struct ieee80211_key, conf);
1410 switch (key->conf.cipher) {
1411 case WLAN_CIPHER_SUITE_AES_CMAC:
1412 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1413 key->u.aes_cmac.icverrors++;
1415 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1416 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1417 key->u.aes_gmac.icverrors++;
1420 /* ignore the others for now, we don't keep counters now */
1424 EXPORT_SYMBOL_GPL(ieee80211_key_mic_failure);
1426 void ieee80211_key_replay(struct ieee80211_key_conf *keyconf)
1428 struct ieee80211_key *key;
1430 key = container_of(keyconf, struct ieee80211_key, conf);
1432 switch (key->conf.cipher) {
1433 case WLAN_CIPHER_SUITE_CCMP:
1434 case WLAN_CIPHER_SUITE_CCMP_256:
1435 key->u.ccmp.replays++;
1437 case WLAN_CIPHER_SUITE_AES_CMAC:
1438 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1439 key->u.aes_cmac.replays++;
1441 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1442 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1443 key->u.aes_gmac.replays++;
1445 case WLAN_CIPHER_SUITE_GCMP:
1446 case WLAN_CIPHER_SUITE_GCMP_256:
1447 key->u.gcmp.replays++;
1451 EXPORT_SYMBOL_GPL(ieee80211_key_replay);
1453 int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata,
1454 unsigned long del_links_mask,
1455 unsigned long add_links_mask)
1457 struct ieee80211_key *key;
1460 list_for_each_entry(key, &sdata->key_list, list) {
1461 if (key->conf.link_id < 0 ||
1462 !(del_links_mask & BIT(key->conf.link_id)))
1465 /* shouldn't happen for per-link keys */
1468 ieee80211_key_disable_hw_accel(key);
1471 list_for_each_entry(key, &sdata->key_list, list) {
1472 if (key->conf.link_id < 0 ||
1473 !(add_links_mask & BIT(key->conf.link_id)))
1476 /* shouldn't happen for per-link keys */
1479 ret = ieee80211_key_enable_hw_accel(key);