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"
30 * DOC: Key handling basics
32 * Key handling in mac80211 is done based on per-interface (sub_if_data)
33 * keys and per-station keys. Since each station belongs to an interface,
34 * each station key also belongs to that interface.
36 * Hardware acceleration is done on a best-effort basis for algorithms
37 * that are implemented in software, for each key the hardware is asked
38 * to enable that key for offloading but if it cannot do that the key is
39 * simply kept for software encryption (unless it is for an algorithm
40 * that isn't implemented in software).
41 * There is currently no way of knowing whether a key is handled in SW
42 * or HW except by looking into debugfs.
44 * All key management is internally protected by a mutex. Within all
45 * other parts of mac80211, key references are, just as STA structure
46 * references, protected by RCU. Note, however, that some things are
47 * unprotected, namely the key->sta dereferences within the hardware
48 * acceleration functions. This means that sta_info_destroy() must
49 * remove the key which waits for an RCU grace period.
52 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
54 static void assert_key_lock(struct ieee80211_local *local)
56 lockdep_assert_held(&local->key_mtx);
59 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
62 * When this count is zero, SKB resizing for allocating tailroom
63 * for IV or MMIC is skipped. But, this check has created two race
64 * cases in xmit path while transiting from zero count to one:
66 * 1. SKB resize was skipped because no key was added but just before
67 * the xmit key is added and SW encryption kicks off.
69 * 2. SKB resize was skipped because all the keys were hw planted but
70 * just before xmit one of the key is deleted and SW encryption kicks
73 * In both the above case SW encryption will find not enough space for
74 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
76 * Solution has been explained at
77 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
80 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
82 * Flush all XMIT packets currently using HW encryption or no
83 * encryption at all if the count transition is from 0 -> 1.
89 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
91 struct ieee80211_sub_if_data *sdata;
97 if (key->flags & KEY_FLAG_TAINTED) {
98 /* If we get here, it's during resume and the key is
99 * tainted so shouldn't be used/programmed any more.
100 * However, its flags may still indicate that it was
101 * programmed into the device (since we're in resume)
102 * so clear that flag now to avoid trying to remove
105 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
109 if (!key->local->ops->set_key)
110 goto out_unsupported;
112 assert_key_lock(key->local);
117 * If this is a per-STA GTK, check if it
118 * is supported; if not, return.
120 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
121 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
122 goto out_unsupported;
124 if (sta && !sta->uploaded)
125 goto out_unsupported;
128 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
130 * The driver doesn't know anything about VLAN interfaces.
131 * Hence, don't send GTKs for VLAN interfaces to the driver.
133 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
134 goto out_unsupported;
137 ret = drv_set_key(key->local, SET_KEY, sdata,
138 sta ? &sta->sta : NULL, &key->conf);
141 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
143 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
144 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
145 sdata->crypto_tx_tailroom_needed_cnt--;
147 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
148 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
153 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
155 "failed to set key (%d, %pM) to hardware (%d)\n",
157 sta ? sta->sta.addr : bcast_addr, ret);
160 switch (key->conf.cipher) {
161 case WLAN_CIPHER_SUITE_WEP40:
162 case WLAN_CIPHER_SUITE_WEP104:
163 case WLAN_CIPHER_SUITE_TKIP:
164 case WLAN_CIPHER_SUITE_CCMP:
165 case WLAN_CIPHER_SUITE_AES_CMAC:
166 /* all of these we can do in software */
173 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
175 struct ieee80211_sub_if_data *sdata;
176 struct sta_info *sta;
181 if (!key || !key->local->ops->set_key)
184 assert_key_lock(key->local);
186 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
192 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
193 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
194 increment_tailroom_need_count(sdata);
196 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
197 sta ? &sta->sta : NULL, &key->conf);
201 "failed to remove key (%d, %pM) from hardware (%d)\n",
203 sta ? sta->sta.addr : bcast_addr, ret);
205 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
208 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
209 int idx, bool uni, bool multi)
211 struct ieee80211_key *key = NULL;
213 assert_key_lock(sdata->local);
215 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
216 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
219 rcu_assign_pointer(sdata->default_unicast_key, key);
220 drv_set_default_unicast_key(sdata->local, sdata, idx);
224 rcu_assign_pointer(sdata->default_multicast_key, key);
226 ieee80211_debugfs_key_update_default(sdata);
229 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
230 bool uni, bool multi)
232 mutex_lock(&sdata->local->key_mtx);
233 __ieee80211_set_default_key(sdata, idx, uni, multi);
234 mutex_unlock(&sdata->local->key_mtx);
238 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
240 struct ieee80211_key *key = NULL;
242 assert_key_lock(sdata->local);
244 if (idx >= NUM_DEFAULT_KEYS &&
245 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
246 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
248 rcu_assign_pointer(sdata->default_mgmt_key, key);
250 ieee80211_debugfs_key_update_default(sdata);
253 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
256 mutex_lock(&sdata->local->key_mtx);
257 __ieee80211_set_default_mgmt_key(sdata, idx);
258 mutex_unlock(&sdata->local->key_mtx);
262 static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
263 struct sta_info *sta,
265 struct ieee80211_key *old,
266 struct ieee80211_key *new)
269 bool defunikey, defmultikey, defmgmtkey;
271 /* caller must provide at least one old/new */
272 if (WARN_ON(!new && !old))
276 list_add_tail(&new->list, &sdata->key_list);
278 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
281 idx = old->conf.keyidx;
283 idx = new->conf.keyidx;
287 rcu_assign_pointer(sta->ptk[idx], new);
290 rcu_assign_pointer(sta->gtk[idx], new);
295 old == key_mtx_dereference(sdata->local,
296 sdata->default_unicast_key);
298 old == key_mtx_dereference(sdata->local,
299 sdata->default_multicast_key);
301 old == key_mtx_dereference(sdata->local,
302 sdata->default_mgmt_key);
304 if (defunikey && !new)
305 __ieee80211_set_default_key(sdata, -1, true, false);
306 if (defmultikey && !new)
307 __ieee80211_set_default_key(sdata, -1, false, true);
308 if (defmgmtkey && !new)
309 __ieee80211_set_default_mgmt_key(sdata, -1);
311 rcu_assign_pointer(sdata->keys[idx], new);
312 if (defunikey && new)
313 __ieee80211_set_default_key(sdata, new->conf.keyidx,
315 if (defmultikey && new)
316 __ieee80211_set_default_key(sdata, new->conf.keyidx,
318 if (defmgmtkey && new)
319 __ieee80211_set_default_mgmt_key(sdata,
324 list_del(&old->list);
327 struct ieee80211_key *
328 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
330 size_t seq_len, const u8 *seq,
331 const struct ieee80211_cipher_scheme *cs)
333 struct ieee80211_key *key;
336 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
337 return ERR_PTR(-EINVAL);
339 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
341 return ERR_PTR(-ENOMEM);
344 * Default to software encryption; we'll later upload the
345 * key to the hardware if possible.
350 key->conf.cipher = cipher;
351 key->conf.keyidx = idx;
352 key->conf.keylen = key_len;
354 case WLAN_CIPHER_SUITE_WEP40:
355 case WLAN_CIPHER_SUITE_WEP104:
356 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
357 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
359 case WLAN_CIPHER_SUITE_TKIP:
360 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
361 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
363 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
364 key->u.tkip.rx[i].iv32 =
365 get_unaligned_le32(&seq[2]);
366 key->u.tkip.rx[i].iv16 =
367 get_unaligned_le16(seq);
370 spin_lock_init(&key->u.tkip.txlock);
372 case WLAN_CIPHER_SUITE_CCMP:
373 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
374 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
376 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
377 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
378 key->u.ccmp.rx_pn[i][j] =
379 seq[IEEE80211_CCMP_PN_LEN - j - 1];
382 * Initialize AES key state here as an optimization so that
383 * it does not need to be initialized for every packet.
385 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
386 if (IS_ERR(key->u.ccmp.tfm)) {
387 err = PTR_ERR(key->u.ccmp.tfm);
392 case WLAN_CIPHER_SUITE_AES_CMAC:
393 key->conf.iv_len = 0;
394 key->conf.icv_len = sizeof(struct ieee80211_mmie);
396 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
397 key->u.aes_cmac.rx_pn[j] =
398 seq[IEEE80211_CMAC_PN_LEN - j - 1];
400 * Initialize AES key state here as an optimization so that
401 * it does not need to be initialized for every packet.
403 key->u.aes_cmac.tfm =
404 ieee80211_aes_cmac_key_setup(key_data);
405 if (IS_ERR(key->u.aes_cmac.tfm)) {
406 err = PTR_ERR(key->u.aes_cmac.tfm);
413 size_t len = (seq_len > MAX_PN_LEN) ?
414 MAX_PN_LEN : seq_len;
416 key->conf.iv_len = cs->hdr_len;
417 key->conf.icv_len = cs->mic_len;
418 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
419 for (j = 0; j < len; j++)
420 key->u.gen.rx_pn[i][j] =
424 memcpy(key->conf.key, key_data, key_len);
425 INIT_LIST_HEAD(&key->list);
430 static void ieee80211_key_free_common(struct ieee80211_key *key)
432 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
433 ieee80211_aes_key_free(key->u.ccmp.tfm);
434 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
435 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
439 static void __ieee80211_key_destroy(struct ieee80211_key *key,
443 ieee80211_key_disable_hw_accel(key);
446 struct ieee80211_sub_if_data *sdata = key->sdata;
448 ieee80211_debugfs_key_remove(key);
450 if (delay_tailroom) {
451 /* see ieee80211_delayed_tailroom_dec */
452 sdata->crypto_tx_tailroom_pending_dec++;
453 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
456 sdata->crypto_tx_tailroom_needed_cnt--;
460 ieee80211_key_free_common(key);
463 static void ieee80211_key_destroy(struct ieee80211_key *key,
470 * Synchronize so the TX path can no longer be using
471 * this key before we free/remove it.
475 __ieee80211_key_destroy(key, delay_tailroom);
478 void ieee80211_key_free_unused(struct ieee80211_key *key)
480 WARN_ON(key->sdata || key->local);
481 ieee80211_key_free_common(key);
484 int ieee80211_key_link(struct ieee80211_key *key,
485 struct ieee80211_sub_if_data *sdata,
486 struct sta_info *sta)
488 struct ieee80211_local *local = sdata->local;
489 struct ieee80211_key *old_key;
493 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
494 idx = key->conf.keyidx;
495 key->local = sdata->local;
499 mutex_lock(&sdata->local->key_mtx);
502 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
504 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
506 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
508 increment_tailroom_need_count(sdata);
510 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
511 ieee80211_key_destroy(old_key, true);
513 ieee80211_debugfs_key_add(key);
515 if (!local->wowlan) {
516 ret = ieee80211_key_enable_hw_accel(key);
518 ieee80211_key_free(key, true);
523 mutex_unlock(&sdata->local->key_mtx);
528 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
534 * Replace key with nothingness if it was ever used.
537 ieee80211_key_replace(key->sdata, key->sta,
538 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
540 ieee80211_key_destroy(key, delay_tailroom);
543 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
545 struct ieee80211_key *key;
549 if (WARN_ON(!ieee80211_sdata_running(sdata)))
552 mutex_lock(&sdata->local->key_mtx);
554 sdata->crypto_tx_tailroom_needed_cnt = 0;
556 list_for_each_entry(key, &sdata->key_list, list) {
557 increment_tailroom_need_count(sdata);
558 ieee80211_key_enable_hw_accel(key);
561 mutex_unlock(&sdata->local->key_mtx);
564 void ieee80211_iter_keys(struct ieee80211_hw *hw,
565 struct ieee80211_vif *vif,
566 void (*iter)(struct ieee80211_hw *hw,
567 struct ieee80211_vif *vif,
568 struct ieee80211_sta *sta,
569 struct ieee80211_key_conf *key,
573 struct ieee80211_local *local = hw_to_local(hw);
574 struct ieee80211_key *key, *tmp;
575 struct ieee80211_sub_if_data *sdata;
579 mutex_lock(&local->key_mtx);
581 sdata = vif_to_sdata(vif);
582 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
583 iter(hw, &sdata->vif,
584 key->sta ? &key->sta->sta : NULL,
585 &key->conf, iter_data);
587 list_for_each_entry(sdata, &local->interfaces, list)
588 list_for_each_entry_safe(key, tmp,
589 &sdata->key_list, list)
590 iter(hw, &sdata->vif,
591 key->sta ? &key->sta->sta : NULL,
592 &key->conf, iter_data);
594 mutex_unlock(&local->key_mtx);
596 EXPORT_SYMBOL(ieee80211_iter_keys);
598 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
599 struct list_head *keys)
601 struct ieee80211_key *key, *tmp;
603 sdata->crypto_tx_tailroom_needed_cnt -=
604 sdata->crypto_tx_tailroom_pending_dec;
605 sdata->crypto_tx_tailroom_pending_dec = 0;
607 ieee80211_debugfs_key_remove_mgmt_default(sdata);
609 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
610 ieee80211_key_replace(key->sdata, key->sta,
611 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
613 list_add_tail(&key->list, keys);
616 ieee80211_debugfs_key_update_default(sdata);
619 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
620 bool force_synchronize)
622 struct ieee80211_local *local = sdata->local;
623 struct ieee80211_sub_if_data *vlan;
624 struct ieee80211_key *key, *tmp;
627 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
629 mutex_lock(&local->key_mtx);
631 ieee80211_free_keys_iface(sdata, &keys);
633 if (sdata->vif.type == NL80211_IFTYPE_AP) {
634 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
635 ieee80211_free_keys_iface(vlan, &keys);
638 if (!list_empty(&keys) || force_synchronize)
640 list_for_each_entry_safe(key, tmp, &keys, list)
641 __ieee80211_key_destroy(key, false);
643 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
644 sdata->crypto_tx_tailroom_pending_dec);
645 if (sdata->vif.type == NL80211_IFTYPE_AP) {
646 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
647 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
648 vlan->crypto_tx_tailroom_pending_dec);
651 mutex_unlock(&local->key_mtx);
654 void ieee80211_free_sta_keys(struct ieee80211_local *local,
655 struct sta_info *sta)
657 struct ieee80211_key *key;
660 mutex_lock(&local->key_mtx);
661 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
662 key = key_mtx_dereference(local, sta->gtk[i]);
665 ieee80211_key_replace(key->sdata, key->sta,
666 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
668 __ieee80211_key_destroy(key, true);
671 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
672 key = key_mtx_dereference(local, sta->ptk[i]);
675 ieee80211_key_replace(key->sdata, key->sta,
676 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
678 __ieee80211_key_destroy(key, true);
681 mutex_unlock(&local->key_mtx);
684 void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
686 struct ieee80211_sub_if_data *sdata;
688 sdata = container_of(wk, struct ieee80211_sub_if_data,
689 dec_tailroom_needed_wk.work);
692 * The reason for the delayed tailroom needed decrementing is to
693 * make roaming faster: during roaming, all keys are first deleted
694 * and then new keys are installed. The first new key causes the
695 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
696 * the cost of synchronize_net() (which can be slow). Avoid this
697 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
698 * key removal for a while, so if we roam the value is larger than
699 * zero and no 0->1 transition happens.
701 * The cost is that if the AP switching was from an AP with keys
702 * to one without, we still allocate tailroom while it would no
703 * longer be needed. However, in the typical (fast) roaming case
704 * within an ESS this usually won't happen.
707 mutex_lock(&sdata->local->key_mtx);
708 sdata->crypto_tx_tailroom_needed_cnt -=
709 sdata->crypto_tx_tailroom_pending_dec;
710 sdata->crypto_tx_tailroom_pending_dec = 0;
711 mutex_unlock(&sdata->local->key_mtx);
714 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
715 const u8 *replay_ctr, gfp_t gfp)
717 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
719 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
721 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
723 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
725 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
726 struct ieee80211_key_seq *seq)
728 struct ieee80211_key *key;
731 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
734 key = container_of(keyconf, struct ieee80211_key, conf);
736 switch (key->conf.cipher) {
737 case WLAN_CIPHER_SUITE_TKIP:
738 seq->tkip.iv32 = key->u.tkip.tx.iv32;
739 seq->tkip.iv16 = key->u.tkip.tx.iv16;
741 case WLAN_CIPHER_SUITE_CCMP:
742 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
743 seq->ccmp.pn[5] = pn64;
744 seq->ccmp.pn[4] = pn64 >> 8;
745 seq->ccmp.pn[3] = pn64 >> 16;
746 seq->ccmp.pn[2] = pn64 >> 24;
747 seq->ccmp.pn[1] = pn64 >> 32;
748 seq->ccmp.pn[0] = pn64 >> 40;
750 case WLAN_CIPHER_SUITE_AES_CMAC:
751 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
752 seq->ccmp.pn[5] = pn64;
753 seq->ccmp.pn[4] = pn64 >> 8;
754 seq->ccmp.pn[3] = pn64 >> 16;
755 seq->ccmp.pn[2] = pn64 >> 24;
756 seq->ccmp.pn[1] = pn64 >> 32;
757 seq->ccmp.pn[0] = pn64 >> 40;
763 EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
765 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
766 int tid, struct ieee80211_key_seq *seq)
768 struct ieee80211_key *key;
771 key = container_of(keyconf, struct ieee80211_key, conf);
773 switch (key->conf.cipher) {
774 case WLAN_CIPHER_SUITE_TKIP:
775 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
777 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
778 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
780 case WLAN_CIPHER_SUITE_CCMP:
781 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
784 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
786 pn = key->u.ccmp.rx_pn[tid];
787 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
789 case WLAN_CIPHER_SUITE_AES_CMAC:
790 if (WARN_ON(tid != 0))
792 pn = key->u.aes_cmac.rx_pn;
793 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
797 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
799 void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
800 struct ieee80211_key_seq *seq)
802 struct ieee80211_key *key;
805 key = container_of(keyconf, struct ieee80211_key, conf);
807 switch (key->conf.cipher) {
808 case WLAN_CIPHER_SUITE_TKIP:
809 key->u.tkip.tx.iv32 = seq->tkip.iv32;
810 key->u.tkip.tx.iv16 = seq->tkip.iv16;
812 case WLAN_CIPHER_SUITE_CCMP:
813 pn64 = (u64)seq->ccmp.pn[5] |
814 ((u64)seq->ccmp.pn[4] << 8) |
815 ((u64)seq->ccmp.pn[3] << 16) |
816 ((u64)seq->ccmp.pn[2] << 24) |
817 ((u64)seq->ccmp.pn[1] << 32) |
818 ((u64)seq->ccmp.pn[0] << 40);
819 atomic64_set(&key->u.ccmp.tx_pn, pn64);
821 case WLAN_CIPHER_SUITE_AES_CMAC:
822 pn64 = (u64)seq->aes_cmac.pn[5] |
823 ((u64)seq->aes_cmac.pn[4] << 8) |
824 ((u64)seq->aes_cmac.pn[3] << 16) |
825 ((u64)seq->aes_cmac.pn[2] << 24) |
826 ((u64)seq->aes_cmac.pn[1] << 32) |
827 ((u64)seq->aes_cmac.pn[0] << 40);
828 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
835 EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
837 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
838 int tid, struct ieee80211_key_seq *seq)
840 struct ieee80211_key *key;
843 key = container_of(keyconf, struct ieee80211_key, conf);
845 switch (key->conf.cipher) {
846 case WLAN_CIPHER_SUITE_TKIP:
847 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
849 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
850 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
852 case WLAN_CIPHER_SUITE_CCMP:
853 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
856 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
858 pn = key->u.ccmp.rx_pn[tid];
859 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
861 case WLAN_CIPHER_SUITE_AES_CMAC:
862 if (WARN_ON(tid != 0))
864 pn = key->u.aes_cmac.rx_pn;
865 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
872 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
874 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
876 struct ieee80211_key *key;
878 key = container_of(keyconf, struct ieee80211_key, conf);
880 assert_key_lock(key->local);
883 * if key was uploaded, we assume the driver will/has remove(d)
884 * it, so adjust bookkeeping accordingly
886 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
887 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
889 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
890 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
891 increment_tailroom_need_count(key->sdata);
894 ieee80211_key_free(key, false);
896 EXPORT_SYMBOL_GPL(ieee80211_remove_key);
898 struct ieee80211_key_conf *
899 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
900 struct ieee80211_key_conf *keyconf)
902 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
903 struct ieee80211_local *local = sdata->local;
904 struct ieee80211_key *key;
907 if (WARN_ON(!local->wowlan))
908 return ERR_PTR(-EINVAL);
910 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
911 return ERR_PTR(-EINVAL);
913 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
914 keyconf->keylen, keyconf->key,
917 return ERR_CAST(key);
919 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
920 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
922 err = ieee80211_key_link(key, sdata, NULL);
928 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);