2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/if_ether.h>
14 #include <linux/etherdevice.h>
15 #include <linux/list.h>
16 #include <linux/rcupdate.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include <linux/export.h>
20 #include <net/mac80211.h>
21 #include <asm/unaligned.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "debugfs_key.h"
32 * DOC: Key handling basics
34 * Key handling in mac80211 is done based on per-interface (sub_if_data)
35 * keys and per-station keys. Since each station belongs to an interface,
36 * each station key also belongs to that interface.
38 * Hardware acceleration is done on a best-effort basis for algorithms
39 * that are implemented in software, for each key the hardware is asked
40 * to enable that key for offloading but if it cannot do that the key is
41 * simply kept for software encryption (unless it is for an algorithm
42 * that isn't implemented in software).
43 * There is currently no way of knowing whether a key is handled in SW
44 * or HW except by looking into debugfs.
46 * All key management is internally protected by a mutex. Within all
47 * other parts of mac80211, key references are, just as STA structure
48 * references, protected by RCU. Note, however, that some things are
49 * unprotected, namely the key->sta dereferences within the hardware
50 * acceleration functions. This means that sta_info_destroy() must
51 * remove the key which waits for an RCU grace period.
54 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
56 static void assert_key_lock(struct ieee80211_local *local)
58 lockdep_assert_held(&local->key_mtx);
61 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
64 * When this count is zero, SKB resizing for allocating tailroom
65 * for IV or MMIC is skipped. But, this check has created two race
66 * cases in xmit path while transiting from zero count to one:
68 * 1. SKB resize was skipped because no key was added but just before
69 * the xmit key is added and SW encryption kicks off.
71 * 2. SKB resize was skipped because all the keys were hw planted but
72 * just before xmit one of the key is deleted and SW encryption kicks
75 * In both the above case SW encryption will find not enough space for
76 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
78 * Solution has been explained at
79 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
82 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
84 * Flush all XMIT packets currently using HW encryption or no
85 * encryption at all if the count transition is from 0 -> 1.
91 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
93 struct ieee80211_sub_if_data *sdata;
95 int ret = -EOPNOTSUPP;
99 if (key->flags & KEY_FLAG_TAINTED) {
100 /* If we get here, it's during resume and the key is
101 * tainted so shouldn't be used/programmed any more.
102 * However, its flags may still indicate that it was
103 * programmed into the device (since we're in resume)
104 * so clear that flag now to avoid trying to remove
107 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
111 if (!key->local->ops->set_key)
112 goto out_unsupported;
114 assert_key_lock(key->local);
119 * If this is a per-STA GTK, check if it
120 * is supported; if not, return.
122 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
123 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
124 goto out_unsupported;
126 if (sta && !sta->uploaded)
127 goto out_unsupported;
130 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
132 * The driver doesn't know anything about VLAN interfaces.
133 * Hence, don't send GTKs for VLAN interfaces to the driver.
135 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
136 goto out_unsupported;
139 ret = drv_set_key(key->local, SET_KEY, sdata,
140 sta ? &sta->sta : NULL, &key->conf);
143 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
145 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
146 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
147 sdata->crypto_tx_tailroom_needed_cnt--;
149 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
150 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
155 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
157 "failed to set key (%d, %pM) to hardware (%d)\n",
159 sta ? sta->sta.addr : bcast_addr, ret);
162 switch (key->conf.cipher) {
163 case WLAN_CIPHER_SUITE_WEP40:
164 case WLAN_CIPHER_SUITE_WEP104:
165 case WLAN_CIPHER_SUITE_TKIP:
166 case WLAN_CIPHER_SUITE_CCMP:
167 case WLAN_CIPHER_SUITE_CCMP_256:
168 case WLAN_CIPHER_SUITE_AES_CMAC:
169 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
170 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
171 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
172 case WLAN_CIPHER_SUITE_GCMP:
173 case WLAN_CIPHER_SUITE_GCMP_256:
174 /* all of these we can do in software - if driver can */
177 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
185 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
187 struct ieee80211_sub_if_data *sdata;
188 struct sta_info *sta;
193 if (!key || !key->local->ops->set_key)
196 assert_key_lock(key->local);
198 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
204 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
205 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
206 increment_tailroom_need_count(sdata);
208 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
209 sta ? &sta->sta : NULL, &key->conf);
213 "failed to remove key (%d, %pM) from hardware (%d)\n",
215 sta ? sta->sta.addr : bcast_addr, ret);
217 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
220 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
221 int idx, bool uni, bool multi)
223 struct ieee80211_key *key = NULL;
225 assert_key_lock(sdata->local);
227 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
228 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
231 rcu_assign_pointer(sdata->default_unicast_key, key);
232 drv_set_default_unicast_key(sdata->local, sdata, idx);
236 rcu_assign_pointer(sdata->default_multicast_key, key);
238 ieee80211_debugfs_key_update_default(sdata);
241 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
242 bool uni, bool multi)
244 mutex_lock(&sdata->local->key_mtx);
245 __ieee80211_set_default_key(sdata, idx, uni, multi);
246 mutex_unlock(&sdata->local->key_mtx);
250 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
252 struct ieee80211_key *key = NULL;
254 assert_key_lock(sdata->local);
256 if (idx >= NUM_DEFAULT_KEYS &&
257 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
258 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
260 rcu_assign_pointer(sdata->default_mgmt_key, key);
262 ieee80211_debugfs_key_update_default(sdata);
265 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
268 mutex_lock(&sdata->local->key_mtx);
269 __ieee80211_set_default_mgmt_key(sdata, idx);
270 mutex_unlock(&sdata->local->key_mtx);
274 static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
275 struct sta_info *sta,
277 struct ieee80211_key *old,
278 struct ieee80211_key *new)
281 bool defunikey, defmultikey, defmgmtkey;
283 /* caller must provide at least one old/new */
284 if (WARN_ON(!new && !old))
288 list_add_tail(&new->list, &sdata->key_list);
290 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
293 idx = old->conf.keyidx;
295 idx = new->conf.keyidx;
299 rcu_assign_pointer(sta->ptk[idx], new);
302 rcu_assign_pointer(sta->gtk[idx], new);
307 old == key_mtx_dereference(sdata->local,
308 sdata->default_unicast_key);
310 old == key_mtx_dereference(sdata->local,
311 sdata->default_multicast_key);
313 old == key_mtx_dereference(sdata->local,
314 sdata->default_mgmt_key);
316 if (defunikey && !new)
317 __ieee80211_set_default_key(sdata, -1, true, false);
318 if (defmultikey && !new)
319 __ieee80211_set_default_key(sdata, -1, false, true);
320 if (defmgmtkey && !new)
321 __ieee80211_set_default_mgmt_key(sdata, -1);
323 rcu_assign_pointer(sdata->keys[idx], new);
324 if (defunikey && new)
325 __ieee80211_set_default_key(sdata, new->conf.keyidx,
327 if (defmultikey && new)
328 __ieee80211_set_default_key(sdata, new->conf.keyidx,
330 if (defmgmtkey && new)
331 __ieee80211_set_default_mgmt_key(sdata,
336 list_del(&old->list);
339 struct ieee80211_key *
340 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
342 size_t seq_len, const u8 *seq,
343 const struct ieee80211_cipher_scheme *cs)
345 struct ieee80211_key *key;
348 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
349 return ERR_PTR(-EINVAL);
351 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
353 return ERR_PTR(-ENOMEM);
356 * Default to software encryption; we'll later upload the
357 * key to the hardware if possible.
362 key->conf.cipher = cipher;
363 key->conf.keyidx = idx;
364 key->conf.keylen = key_len;
366 case WLAN_CIPHER_SUITE_WEP40:
367 case WLAN_CIPHER_SUITE_WEP104:
368 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
369 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
371 case WLAN_CIPHER_SUITE_TKIP:
372 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
373 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
375 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
376 key->u.tkip.rx[i].iv32 =
377 get_unaligned_le32(&seq[2]);
378 key->u.tkip.rx[i].iv16 =
379 get_unaligned_le16(seq);
382 spin_lock_init(&key->u.tkip.txlock);
384 case WLAN_CIPHER_SUITE_CCMP:
385 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
386 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
388 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
389 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
390 key->u.ccmp.rx_pn[i][j] =
391 seq[IEEE80211_CCMP_PN_LEN - j - 1];
394 * Initialize AES key state here as an optimization so that
395 * it does not need to be initialized for every packet.
397 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
398 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
399 if (IS_ERR(key->u.ccmp.tfm)) {
400 err = PTR_ERR(key->u.ccmp.tfm);
405 case WLAN_CIPHER_SUITE_CCMP_256:
406 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
407 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
408 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
409 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
410 key->u.ccmp.rx_pn[i][j] =
411 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
412 /* Initialize AES key state here as an optimization so that
413 * it does not need to be initialized for every packet.
415 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
416 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
417 if (IS_ERR(key->u.ccmp.tfm)) {
418 err = PTR_ERR(key->u.ccmp.tfm);
423 case WLAN_CIPHER_SUITE_AES_CMAC:
424 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
425 key->conf.iv_len = 0;
426 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
427 key->conf.icv_len = sizeof(struct ieee80211_mmie);
429 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
431 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
432 key->u.aes_cmac.rx_pn[j] =
433 seq[IEEE80211_CMAC_PN_LEN - j - 1];
435 * Initialize AES key state here as an optimization so that
436 * it does not need to be initialized for every packet.
438 key->u.aes_cmac.tfm =
439 ieee80211_aes_cmac_key_setup(key_data, key_len);
440 if (IS_ERR(key->u.aes_cmac.tfm)) {
441 err = PTR_ERR(key->u.aes_cmac.tfm);
446 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
447 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
448 key->conf.iv_len = 0;
449 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
451 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
452 key->u.aes_gmac.rx_pn[j] =
453 seq[IEEE80211_GMAC_PN_LEN - j - 1];
454 /* Initialize AES key state here as an optimization so that
455 * it does not need to be initialized for every packet.
457 key->u.aes_gmac.tfm =
458 ieee80211_aes_gmac_key_setup(key_data, key_len);
459 if (IS_ERR(key->u.aes_gmac.tfm)) {
460 err = PTR_ERR(key->u.aes_gmac.tfm);
465 case WLAN_CIPHER_SUITE_GCMP:
466 case WLAN_CIPHER_SUITE_GCMP_256:
467 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
468 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
469 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
470 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
471 key->u.gcmp.rx_pn[i][j] =
472 seq[IEEE80211_GCMP_PN_LEN - j - 1];
473 /* Initialize AES key state here as an optimization so that
474 * it does not need to be initialized for every packet.
476 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
478 if (IS_ERR(key->u.gcmp.tfm)) {
479 err = PTR_ERR(key->u.gcmp.tfm);
486 size_t len = (seq_len > MAX_PN_LEN) ?
487 MAX_PN_LEN : seq_len;
489 key->conf.iv_len = cs->hdr_len;
490 key->conf.icv_len = cs->mic_len;
491 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
492 for (j = 0; j < len; j++)
493 key->u.gen.rx_pn[i][j] =
497 memcpy(key->conf.key, key_data, key_len);
498 INIT_LIST_HEAD(&key->list);
503 static void ieee80211_key_free_common(struct ieee80211_key *key)
505 switch (key->conf.cipher) {
506 case WLAN_CIPHER_SUITE_CCMP:
507 case WLAN_CIPHER_SUITE_CCMP_256:
508 ieee80211_aes_key_free(key->u.ccmp.tfm);
510 case WLAN_CIPHER_SUITE_AES_CMAC:
511 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
512 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
514 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
515 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
516 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
518 case WLAN_CIPHER_SUITE_GCMP:
519 case WLAN_CIPHER_SUITE_GCMP_256:
520 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
526 static void __ieee80211_key_destroy(struct ieee80211_key *key,
530 ieee80211_key_disable_hw_accel(key);
533 struct ieee80211_sub_if_data *sdata = key->sdata;
535 ieee80211_debugfs_key_remove(key);
537 if (delay_tailroom) {
538 /* see ieee80211_delayed_tailroom_dec */
539 sdata->crypto_tx_tailroom_pending_dec++;
540 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
543 sdata->crypto_tx_tailroom_needed_cnt--;
547 ieee80211_key_free_common(key);
550 static void ieee80211_key_destroy(struct ieee80211_key *key,
557 * Synchronize so the TX path can no longer be using
558 * this key before we free/remove it.
562 __ieee80211_key_destroy(key, delay_tailroom);
565 void ieee80211_key_free_unused(struct ieee80211_key *key)
567 WARN_ON(key->sdata || key->local);
568 ieee80211_key_free_common(key);
571 int ieee80211_key_link(struct ieee80211_key *key,
572 struct ieee80211_sub_if_data *sdata,
573 struct sta_info *sta)
575 struct ieee80211_local *local = sdata->local;
576 struct ieee80211_key *old_key;
580 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
581 idx = key->conf.keyidx;
582 key->local = sdata->local;
586 mutex_lock(&sdata->local->key_mtx);
589 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
591 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
593 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
595 increment_tailroom_need_count(sdata);
597 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
598 ieee80211_key_destroy(old_key, true);
600 ieee80211_debugfs_key_add(key);
602 if (!local->wowlan) {
603 ret = ieee80211_key_enable_hw_accel(key);
605 ieee80211_key_free(key, true);
610 mutex_unlock(&sdata->local->key_mtx);
615 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
621 * Replace key with nothingness if it was ever used.
624 ieee80211_key_replace(key->sdata, key->sta,
625 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
627 ieee80211_key_destroy(key, delay_tailroom);
630 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
632 struct ieee80211_key *key;
636 if (WARN_ON(!ieee80211_sdata_running(sdata)))
639 mutex_lock(&sdata->local->key_mtx);
641 sdata->crypto_tx_tailroom_needed_cnt = 0;
643 list_for_each_entry(key, &sdata->key_list, list) {
644 increment_tailroom_need_count(sdata);
645 ieee80211_key_enable_hw_accel(key);
648 mutex_unlock(&sdata->local->key_mtx);
651 void ieee80211_iter_keys(struct ieee80211_hw *hw,
652 struct ieee80211_vif *vif,
653 void (*iter)(struct ieee80211_hw *hw,
654 struct ieee80211_vif *vif,
655 struct ieee80211_sta *sta,
656 struct ieee80211_key_conf *key,
660 struct ieee80211_local *local = hw_to_local(hw);
661 struct ieee80211_key *key, *tmp;
662 struct ieee80211_sub_if_data *sdata;
666 mutex_lock(&local->key_mtx);
668 sdata = vif_to_sdata(vif);
669 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
670 iter(hw, &sdata->vif,
671 key->sta ? &key->sta->sta : NULL,
672 &key->conf, iter_data);
674 list_for_each_entry(sdata, &local->interfaces, list)
675 list_for_each_entry_safe(key, tmp,
676 &sdata->key_list, list)
677 iter(hw, &sdata->vif,
678 key->sta ? &key->sta->sta : NULL,
679 &key->conf, iter_data);
681 mutex_unlock(&local->key_mtx);
683 EXPORT_SYMBOL(ieee80211_iter_keys);
685 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
686 struct list_head *keys)
688 struct ieee80211_key *key, *tmp;
690 sdata->crypto_tx_tailroom_needed_cnt -=
691 sdata->crypto_tx_tailroom_pending_dec;
692 sdata->crypto_tx_tailroom_pending_dec = 0;
694 ieee80211_debugfs_key_remove_mgmt_default(sdata);
696 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
697 ieee80211_key_replace(key->sdata, key->sta,
698 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
700 list_add_tail(&key->list, keys);
703 ieee80211_debugfs_key_update_default(sdata);
706 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
707 bool force_synchronize)
709 struct ieee80211_local *local = sdata->local;
710 struct ieee80211_sub_if_data *vlan;
711 struct ieee80211_key *key, *tmp;
714 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
716 mutex_lock(&local->key_mtx);
718 ieee80211_free_keys_iface(sdata, &keys);
720 if (sdata->vif.type == NL80211_IFTYPE_AP) {
721 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
722 ieee80211_free_keys_iface(vlan, &keys);
725 if (!list_empty(&keys) || force_synchronize)
727 list_for_each_entry_safe(key, tmp, &keys, list)
728 __ieee80211_key_destroy(key, false);
730 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
731 sdata->crypto_tx_tailroom_pending_dec);
732 if (sdata->vif.type == NL80211_IFTYPE_AP) {
733 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
734 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
735 vlan->crypto_tx_tailroom_pending_dec);
738 mutex_unlock(&local->key_mtx);
741 void ieee80211_free_sta_keys(struct ieee80211_local *local,
742 struct sta_info *sta)
744 struct ieee80211_key *key;
747 mutex_lock(&local->key_mtx);
748 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
749 key = key_mtx_dereference(local, sta->gtk[i]);
752 ieee80211_key_replace(key->sdata, key->sta,
753 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
755 __ieee80211_key_destroy(key, true);
758 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
759 key = key_mtx_dereference(local, sta->ptk[i]);
762 ieee80211_key_replace(key->sdata, key->sta,
763 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
765 __ieee80211_key_destroy(key, true);
768 mutex_unlock(&local->key_mtx);
771 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
773 struct ieee80211_sub_if_data *sdata;
775 sdata = container_of(wk, struct ieee80211_sub_if_data,
776 dec_tailroom_needed_wk.work);
779 * The reason for the delayed tailroom needed decrementing is to
780 * make roaming faster: during roaming, all keys are first deleted
781 * and then new keys are installed. The first new key causes the
782 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
783 * the cost of synchronize_net() (which can be slow). Avoid this
784 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
785 * key removal for a while, so if we roam the value is larger than
786 * zero and no 0->1 transition happens.
788 * The cost is that if the AP switching was from an AP with keys
789 * to one without, we still allocate tailroom while it would no
790 * longer be needed. However, in the typical (fast) roaming case
791 * within an ESS this usually won't happen.
794 mutex_lock(&sdata->local->key_mtx);
795 sdata->crypto_tx_tailroom_needed_cnt -=
796 sdata->crypto_tx_tailroom_pending_dec;
797 sdata->crypto_tx_tailroom_pending_dec = 0;
798 mutex_unlock(&sdata->local->key_mtx);
801 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
802 const u8 *replay_ctr, gfp_t gfp)
804 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
806 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
808 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
810 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
812 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
813 struct ieee80211_key_seq *seq)
815 struct ieee80211_key *key;
818 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
821 key = container_of(keyconf, struct ieee80211_key, conf);
823 switch (key->conf.cipher) {
824 case WLAN_CIPHER_SUITE_TKIP:
825 seq->tkip.iv32 = key->u.tkip.tx.iv32;
826 seq->tkip.iv16 = key->u.tkip.tx.iv16;
828 case WLAN_CIPHER_SUITE_CCMP:
829 case WLAN_CIPHER_SUITE_CCMP_256:
830 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
831 seq->ccmp.pn[5] = pn64;
832 seq->ccmp.pn[4] = pn64 >> 8;
833 seq->ccmp.pn[3] = pn64 >> 16;
834 seq->ccmp.pn[2] = pn64 >> 24;
835 seq->ccmp.pn[1] = pn64 >> 32;
836 seq->ccmp.pn[0] = pn64 >> 40;
838 case WLAN_CIPHER_SUITE_AES_CMAC:
839 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
840 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
841 seq->ccmp.pn[5] = pn64;
842 seq->ccmp.pn[4] = pn64 >> 8;
843 seq->ccmp.pn[3] = pn64 >> 16;
844 seq->ccmp.pn[2] = pn64 >> 24;
845 seq->ccmp.pn[1] = pn64 >> 32;
846 seq->ccmp.pn[0] = pn64 >> 40;
848 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
849 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
850 pn64 = atomic64_read(&key->u.aes_gmac.tx_pn);
851 seq->ccmp.pn[5] = pn64;
852 seq->ccmp.pn[4] = pn64 >> 8;
853 seq->ccmp.pn[3] = pn64 >> 16;
854 seq->ccmp.pn[2] = pn64 >> 24;
855 seq->ccmp.pn[1] = pn64 >> 32;
856 seq->ccmp.pn[0] = pn64 >> 40;
858 case WLAN_CIPHER_SUITE_GCMP:
859 case WLAN_CIPHER_SUITE_GCMP_256:
860 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
861 seq->gcmp.pn[5] = pn64;
862 seq->gcmp.pn[4] = pn64 >> 8;
863 seq->gcmp.pn[3] = pn64 >> 16;
864 seq->gcmp.pn[2] = pn64 >> 24;
865 seq->gcmp.pn[1] = pn64 >> 32;
866 seq->gcmp.pn[0] = pn64 >> 40;
872 EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
874 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
875 int tid, struct ieee80211_key_seq *seq)
877 struct ieee80211_key *key;
880 key = container_of(keyconf, struct ieee80211_key, conf);
882 switch (key->conf.cipher) {
883 case WLAN_CIPHER_SUITE_TKIP:
884 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
886 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
887 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
889 case WLAN_CIPHER_SUITE_CCMP:
890 case WLAN_CIPHER_SUITE_CCMP_256:
891 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
894 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
896 pn = key->u.ccmp.rx_pn[tid];
897 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
899 case WLAN_CIPHER_SUITE_AES_CMAC:
900 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
901 if (WARN_ON(tid != 0))
903 pn = key->u.aes_cmac.rx_pn;
904 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
906 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
907 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
908 if (WARN_ON(tid != 0))
910 pn = key->u.aes_gmac.rx_pn;
911 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
913 case WLAN_CIPHER_SUITE_GCMP:
914 case WLAN_CIPHER_SUITE_GCMP_256:
915 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
918 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
920 pn = key->u.gcmp.rx_pn[tid];
921 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
925 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
927 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
928 struct ieee80211_key_seq *seq)
930 struct ieee80211_key *key;
933 key = container_of(keyconf, struct ieee80211_key, conf);
935 switch (key->conf.cipher) {
936 case WLAN_CIPHER_SUITE_TKIP:
937 key->u.tkip.tx.iv32 = seq->tkip.iv32;
938 key->u.tkip.tx.iv16 = seq->tkip.iv16;
940 case WLAN_CIPHER_SUITE_CCMP:
941 case WLAN_CIPHER_SUITE_CCMP_256:
942 pn64 = (u64)seq->ccmp.pn[5] |
943 ((u64)seq->ccmp.pn[4] << 8) |
944 ((u64)seq->ccmp.pn[3] << 16) |
945 ((u64)seq->ccmp.pn[2] << 24) |
946 ((u64)seq->ccmp.pn[1] << 32) |
947 ((u64)seq->ccmp.pn[0] << 40);
948 atomic64_set(&key->u.ccmp.tx_pn, pn64);
950 case WLAN_CIPHER_SUITE_AES_CMAC:
951 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
952 pn64 = (u64)seq->aes_cmac.pn[5] |
953 ((u64)seq->aes_cmac.pn[4] << 8) |
954 ((u64)seq->aes_cmac.pn[3] << 16) |
955 ((u64)seq->aes_cmac.pn[2] << 24) |
956 ((u64)seq->aes_cmac.pn[1] << 32) |
957 ((u64)seq->aes_cmac.pn[0] << 40);
958 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
960 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
961 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
962 pn64 = (u64)seq->aes_gmac.pn[5] |
963 ((u64)seq->aes_gmac.pn[4] << 8) |
964 ((u64)seq->aes_gmac.pn[3] << 16) |
965 ((u64)seq->aes_gmac.pn[2] << 24) |
966 ((u64)seq->aes_gmac.pn[1] << 32) |
967 ((u64)seq->aes_gmac.pn[0] << 40);
968 atomic64_set(&key->u.aes_gmac.tx_pn, pn64);
970 case WLAN_CIPHER_SUITE_GCMP:
971 case WLAN_CIPHER_SUITE_GCMP_256:
972 pn64 = (u64)seq->gcmp.pn[5] |
973 ((u64)seq->gcmp.pn[4] << 8) |
974 ((u64)seq->gcmp.pn[3] << 16) |
975 ((u64)seq->gcmp.pn[2] << 24) |
976 ((u64)seq->gcmp.pn[1] << 32) |
977 ((u64)seq->gcmp.pn[0] << 40);
978 atomic64_set(&key->u.gcmp.tx_pn, pn64);
985 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
987 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
988 int tid, struct ieee80211_key_seq *seq)
990 struct ieee80211_key *key;
993 key = container_of(keyconf, struct ieee80211_key, conf);
995 switch (key->conf.cipher) {
996 case WLAN_CIPHER_SUITE_TKIP:
997 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
999 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1000 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1002 case WLAN_CIPHER_SUITE_CCMP:
1003 case WLAN_CIPHER_SUITE_CCMP_256:
1004 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1007 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1009 pn = key->u.ccmp.rx_pn[tid];
1010 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1012 case WLAN_CIPHER_SUITE_AES_CMAC:
1013 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1014 if (WARN_ON(tid != 0))
1016 pn = key->u.aes_cmac.rx_pn;
1017 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1019 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1020 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1021 if (WARN_ON(tid != 0))
1023 pn = key->u.aes_gmac.rx_pn;
1024 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1026 case WLAN_CIPHER_SUITE_GCMP:
1027 case WLAN_CIPHER_SUITE_GCMP_256:
1028 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1031 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1033 pn = key->u.gcmp.rx_pn[tid];
1034 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1041 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1043 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1045 struct ieee80211_key *key;
1047 key = container_of(keyconf, struct ieee80211_key, conf);
1049 assert_key_lock(key->local);
1052 * if key was uploaded, we assume the driver will/has remove(d)
1053 * it, so adjust bookkeeping accordingly
1055 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1056 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1058 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
1059 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
1060 increment_tailroom_need_count(key->sdata);
1063 ieee80211_key_free(key, false);
1065 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1067 struct ieee80211_key_conf *
1068 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1069 struct ieee80211_key_conf *keyconf)
1071 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1072 struct ieee80211_local *local = sdata->local;
1073 struct ieee80211_key *key;
1076 if (WARN_ON(!local->wowlan))
1077 return ERR_PTR(-EINVAL);
1079 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1080 return ERR_PTR(-EINVAL);
1082 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1083 keyconf->keylen, keyconf->key,
1086 return ERR_CAST(key);
1088 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1089 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1091 err = ieee80211_key_link(key, sdata, NULL);
1093 return ERR_PTR(err);
1097 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);