blob: 5f58895ee0d257939ddb594e422378a9e3b71738 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berg1f5a7e42007-07-27 15:43:23 +02002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Berg3b967662008-04-08 17:56:52 +02006 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03007 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Bergfdf7cb42017-09-05 14:54:54 +02008 * Copyright 2015-2017 Intel Deutschland GmbH
Johannes Berg1f5a7e42007-07-27 15:43:23 +02009 */
10
Johannes Berg11a843b2007-08-28 17:01:55 -040011#include <linux/if_ether.h>
12#include <linux/etherdevice.h>
13#include <linux/list.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040014#include <linux/rcupdate.h>
Johannes Bergdb4d1162008-02-25 16:27:45 +010015#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040017#include <linux/export.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020018#include <net/mac80211.h>
Jason A. Donenfeld2bdd7132017-10-17 20:32:07 +020019#include <crypto/algapi.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010020#include <asm/unaligned.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020021#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020022#include "driver-ops.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020023#include "debugfs_key.h"
24#include "aes_ccm.h"
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +020025#include "aes_cmac.h"
Jouni Malinen8ade5382015-01-24 19:52:09 +020026#include "aes_gmac.h"
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020027#include "aes_gcm.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020028
Johannes Berg11a843b2007-08-28 17:01:55 -040029
Johannes Bergdbbea672008-02-26 14:34:06 +010030/**
31 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040032 *
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.
36 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010037 * 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.
Johannes Berg11a843b2007-08-28 17:01:55 -040044 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010045 * 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.
Johannes Berg11a843b2007-08-28 17:01:55 -040051 */
52
53static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040054
Johannes Bergad0e2b52010-06-01 10:19:19 +020055static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020056{
Johannes Berg46a5eba2010-09-15 13:28:15 +020057 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020058}
59
Michal Kaziorf9dca802015-05-13 09:16:48 +000060static void
61update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
62{
63 struct ieee80211_sub_if_data *vlan;
64
65 if (sdata->vif.type != NL80211_IFTYPE_AP)
66 return;
67
Johannes Berg51f458d2015-06-17 13:54:54 +020068 /* crypto_tx_tailroom_needed_cnt is protected by this */
69 assert_key_lock(sdata->local);
Michal Kaziorf9dca802015-05-13 09:16:48 +000070
Johannes Berg51f458d2015-06-17 13:54:54 +020071 rcu_read_lock();
72
73 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
Michal Kaziorf9dca802015-05-13 09:16:48 +000074 vlan->crypto_tx_tailroom_needed_cnt += delta;
75
Johannes Berg51f458d2015-06-17 13:54:54 +020076 rcu_read_unlock();
Michal Kaziorf9dca802015-05-13 09:16:48 +000077}
78
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053079static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
80{
81 /*
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:
85 *
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.
88 *
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
91 * off.
92 *
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)
95 *
96 * Solution has been explained at
97 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
98 */
99
Johannes Berg51f458d2015-06-17 13:54:54 +0200100 assert_key_lock(sdata->local);
101
Michal Kaziorf9dca802015-05-13 09:16:48 +0000102 update_vlan_tailroom_need_count(sdata, 1);
103
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530104 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
105 /*
106 * Flush all XMIT packets currently using HW encryption or no
107 * encryption at all if the count transition is from 0 -> 1.
108 */
109 synchronize_net();
110 }
111}
112
Michal Kaziorf9dca802015-05-13 09:16:48 +0000113static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
114 int delta)
115{
Johannes Berg51f458d2015-06-17 13:54:54 +0200116 assert_key_lock(sdata->local);
117
Michal Kaziorf9dca802015-05-13 09:16:48 +0000118 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
119
120 update_vlan_tailroom_need_count(sdata, -delta);
121 sdata->crypto_tx_tailroom_needed_cnt -= delta;
122}
123
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300124static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400125{
Manikanta Pubbisettydb3bdcb2018-03-28 18:34:19 +0530126 struct ieee80211_sub_if_data *sdata = key->sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100127 struct sta_info *sta;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100128 int ret = -EOPNOTSUPP;
Johannes Berg11a843b2007-08-28 17:01:55 -0400129
Johannes Berg3b967662008-04-08 17:56:52 +0200130 might_sleep();
131
Johannes Berg46191942014-10-13 13:43:29 +0200132 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
138 * it again later.
139 */
Alexander Wetzel092c4092019-03-16 21:44:43 +0100140 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);
145
Johannes Berg46191942014-10-13 13:43:29 +0200146 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200147 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200148 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200149
Johannes Berge31b8212010-10-05 19:39:30 +0200150 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300151 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400152
Johannes Bergad0e2b52010-06-01 10:19:19 +0200153 assert_key_lock(key->local);
154
Johannes Berg89c91ca2012-01-20 13:55:19 +0100155 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100156
Johannes Berge31b8212010-10-05 19:39:30 +0200157 /*
158 * If this is a per-STA GTK, check if it
159 * is supported; if not, return.
160 */
161 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200162 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
Johannes Berge31b8212010-10-05 19:39:30 +0200163 goto out_unsupported;
164
Johannes Berg89c91ca2012-01-20 13:55:19 +0100165 if (sta && !sta->uploaded)
166 goto out_unsupported;
167
Helmut Schaa18890d42010-11-19 08:11:01 +0100168 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
169 /*
170 * The driver doesn't know anything about VLAN interfaces.
171 * Hence, don't send GTKs for VLAN interfaces to the driver.
172 */
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100173 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
174 ret = 1;
Helmut Schaa18890d42010-11-19 08:11:01 +0100175 goto out_unsupported;
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100176 }
Helmut Schaa18890d42010-11-19 08:11:01 +0100177 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400178
Johannes Berg89c91ca2012-01-20 13:55:19 +0100179 ret = drv_set_key(key->local, SET_KEY, sdata,
180 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400181
Johannes Berge31b8212010-10-05 19:39:30 +0200182 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400183 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530184
Alexander Wetzel092c4092019-03-16 21:44:43 +0100185 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
186 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
187 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Michal Kaziorf9dca802015-05-13 09:16:48 +0000188 decrease_tailroom_need_count(sdata, 1);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530189
Arik Nemtsov077a9152011-10-23 08:21:41 +0200190 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
191 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
192
David Spinadel9de18d82017-12-01 13:50:52 +0200193 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
194 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
195
Johannes Berge31b8212010-10-05 19:39:30 +0200196 return 0;
197 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400198
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100199 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200200 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700201 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100202 key->conf.keyidx,
203 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300204
Johannes Berge31b8212010-10-05 19:39:30 +0200205 out_unsupported:
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:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200211 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200212 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200213 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200214 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
215 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200216 case WLAN_CIPHER_SUITE_GCMP:
217 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100218 /* all of these we can do in software - if driver can */
219 if (ret == 1)
220 return 0;
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100221 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100222 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200223 return 0;
224 default:
225 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300226 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400227}
228
229static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
230{
Johannes Bergdc822b52008-12-29 12:55:09 +0100231 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100232 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400233 int ret;
234
Johannes Berg3b967662008-04-08 17:56:52 +0200235 might_sleep();
236
Johannes Bergdb4d1162008-02-25 16:27:45 +0100237 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400238 return;
239
Johannes Bergad0e2b52010-06-01 10:19:19 +0200240 assert_key_lock(key->local);
241
242 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400243 return;
244
Johannes Berg89c91ca2012-01-20 13:55:19 +0100245 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100246 sdata = key->sdata;
247
Alexander Wetzel092c4092019-03-16 21:44:43 +0100248 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
249 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
250 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530251 increment_tailroom_need_count(sdata);
252
Alexander Wetzel62872a92018-08-31 15:00:38 +0200253 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg12375ef2009-11-25 20:30:31 +0100254 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100255 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400256
257 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200258 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700259 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100260 key->conf.keyidx,
261 sta ? sta->sta.addr : bcast_addr, ret);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200262}
Johannes Berg11a843b2007-08-28 17:01:55 -0400263
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100264int ieee80211_set_tx_key(struct ieee80211_key *key)
265{
266 struct sta_info *sta = key->sta;
267 struct ieee80211_local *local = key->local;
268 struct ieee80211_key *old;
269
270 assert_key_lock(local);
271
272 old = key_mtx_dereference(local, sta->ptk[sta->ptk_idx]);
273 sta->ptk_idx = key->conf.keyidx;
274 ieee80211_check_fast_xmit(sta);
275
276 return 0;
277}
278
Alexander Wetzel62872a92018-08-31 15:00:38 +0200279static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
280 struct ieee80211_key *new_key,
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100281 bool pairwise)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200282{
283 struct ieee80211_sub_if_data *sdata;
284 struct ieee80211_local *local;
285 struct sta_info *sta;
286 int ret;
287
288 /* Aggregation sessions are OK when running on SW crypto.
289 * A broken remote STA may cause issues not observed with HW
290 * crypto, though.
291 */
292 if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
293 return 0;
294
295 assert_key_lock(old_key->local);
296 sta = old_key->sta;
297
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100298 /* Unicast rekey without Extended Key ID needs special handling */
299 if (new_key && sta && pairwise &&
300 rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200301 local = old_key->local;
302 sdata = old_key->sdata;
303
304 /* Stop TX till we are on the new key */
305 old_key->flags |= KEY_FLAG_TAINTED;
306 ieee80211_clear_fast_xmit(sta);
307
308 /* Aggregation sessions during rekey are complicated due to the
309 * reorder buffer and retransmits. Side step that by blocking
310 * aggregation during rekey and tear down running sessions.
311 */
312 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
313 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
314 ieee80211_sta_tear_down_BA_sessions(sta,
315 AGG_STOP_LOCAL_REQUEST);
316 }
317
318 if (!wiphy_ext_feature_isset(local->hw.wiphy,
319 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
320 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
321 sta->sta.addr);
322 /* Flushing the driver queues *may* help prevent
323 * the clear text leaks and freezes.
324 */
325 ieee80211_flush_queues(local, sdata, false);
326 }
327 }
328
329 ieee80211_key_disable_hw_accel(old_key);
330
331 if (new_key)
332 ret = ieee80211_key_enable_hw_accel(new_key);
333 else
334 ret = 0;
335
336 return ret;
Johannes Berg3b967662008-04-08 17:56:52 +0200337}
338
339static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100340 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200341{
342 struct ieee80211_key *key = NULL;
343
Johannes Bergad0e2b52010-06-01 10:19:19 +0200344 assert_key_lock(sdata->local);
345
Johannes Berg3b967662008-04-08 17:56:52 +0200346 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200347 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200348
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300349 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100350 rcu_assign_pointer(sdata->default_unicast_key, key);
Johannes Berg17c18bf2015-03-21 15:25:43 +0100351 ieee80211_check_fast_xmit_iface(sdata);
Johannes Bergec4efc42016-12-13 09:39:18 +0100352 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
353 drv_set_default_unicast_key(sdata->local, sdata, idx);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300354 }
355
Johannes Bergf7e01042010-12-09 19:49:02 +0100356 if (multi)
357 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200358
Johannes Bergf7e01042010-12-09 19:49:02 +0100359 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200360}
361
Johannes Bergf7e01042010-12-09 19:49:02 +0100362void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
363 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200364{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200365 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100366 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200367 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200368}
369
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200370static void
371__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
372{
373 struct ieee80211_key *key = NULL;
374
Johannes Bergad0e2b52010-06-01 10:19:19 +0200375 assert_key_lock(sdata->local);
376
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200377 if (idx >= NUM_DEFAULT_KEYS &&
378 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200379 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200380
381 rcu_assign_pointer(sdata->default_mgmt_key, key);
382
Johannes Bergf7e01042010-12-09 19:49:02 +0100383 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200384}
385
386void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
387 int idx)
388{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200389 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200390 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200391 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200392}
393
Johannes Berg3b967662008-04-08 17:56:52 +0200394
Alexander Wetzel62872a92018-08-31 15:00:38 +0200395static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100396 struct sta_info *sta,
397 bool pairwise,
398 struct ieee80211_key *old,
399 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200400{
Johannes Bergf7e01042010-12-09 19:49:02 +0100401 int idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200402 int ret;
Johannes Bergf7e01042010-12-09 19:49:02 +0100403 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200404
Johannes Berg5282c3b2013-10-29 10:00:08 +0100405 /* caller must provide at least one old/new */
406 if (WARN_ON(!new && !old))
Alexander Wetzel62872a92018-08-31 15:00:38 +0200407 return 0;
Johannes Berg5282c3b2013-10-29 10:00:08 +0100408
Johannes Berg3b967662008-04-08 17:56:52 +0200409 if (new)
Eliad Pelleref044762015-11-17 10:24:37 +0200410 list_add_tail_rcu(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200411
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200412 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
413
Alexander Wetzel62872a92018-08-31 15:00:38 +0200414 if (old) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200415 idx = old->conf.keyidx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200416 ret = ieee80211_hw_key_replace(old, new, pairwise);
417 } else {
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500418 /* new must be provided in case old is not */
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200419 idx = new->conf.keyidx;
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500420 if (!new->local->wowlan)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200421 ret = ieee80211_key_enable_hw_accel(new);
422 else
423 ret = 0;
424 }
425
426 if (ret)
427 return ret;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200428
429 if (sta) {
430 if (pairwise) {
431 rcu_assign_pointer(sta->ptk[idx], new);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100432 if (new &&
433 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
434 sta->ptk_idx = idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200435 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
436 ieee80211_check_fast_xmit(sta);
437 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200438 } else {
439 rcu_assign_pointer(sta->gtk[idx], new);
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200440 }
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100441 /* Only needed for transition from no key -> key.
442 * Still triggers unnecessary when using Extended Key ID
443 * and installing the second key ID the first time.
444 */
445 if (new && !old)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200446 ieee80211_check_fast_rx(sta);
Johannes Berg3b967662008-04-08 17:56:52 +0200447 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200448 defunikey = old &&
449 old == key_mtx_dereference(sdata->local,
450 sdata->default_unicast_key);
451 defmultikey = old &&
452 old == key_mtx_dereference(sdata->local,
453 sdata->default_multicast_key);
454 defmgmtkey = old &&
455 old == key_mtx_dereference(sdata->local,
456 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200457
Johannes Bergf7e01042010-12-09 19:49:02 +0100458 if (defunikey && !new)
459 __ieee80211_set_default_key(sdata, -1, true, false);
460 if (defmultikey && !new)
461 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200462 if (defmgmtkey && !new)
463 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200464
465 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100466 if (defunikey && new)
467 __ieee80211_set_default_key(sdata, new->conf.keyidx,
468 true, false);
469 if (defmultikey && new)
470 __ieee80211_set_default_key(sdata, new->conf.keyidx,
471 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200472 if (defmgmtkey && new)
473 __ieee80211_set_default_mgmt_key(sdata,
474 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200475 }
476
Johannes Bergb5c34f62011-01-03 19:51:09 +0100477 if (old)
Eliad Pelleref044762015-11-17 10:24:37 +0200478 list_del_rcu(&old->list);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200479
480 return 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400481}
482
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200483struct ieee80211_key *
484ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
485 const u8 *key_data,
486 size_t seq_len, const u8 *seq,
487 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200488{
489 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100490 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200491
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200492 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
493 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400494
495 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200496 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100497 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400498
499 /*
500 * Default to software encryption; we'll later upload the
501 * key to the hardware if possible.
502 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400503 key->conf.flags = 0;
504 key->flags = 0;
505
Johannes Berg97359d12010-08-10 09:46:38 +0200506 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400507 key->conf.keyidx = idx;
508 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200509 switch (cipher) {
510 case WLAN_CIPHER_SUITE_WEP40:
511 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200512 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
513 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200514 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200515 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200516 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
517 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300518 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100519 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300520 key->u.tkip.rx[i].iv32 =
521 get_unaligned_le32(&seq[2]);
522 key->u.tkip.rx[i].iv16 =
523 get_unaligned_le16(seq);
524 }
525 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200526 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200527 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200528 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200529 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
530 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300531 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100532 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200533 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300534 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200535 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300536 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400537 /*
538 * Initialize AES key state here as an optimization so that
539 * it does not need to be initialized for every packet.
540 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200541 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
542 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
543 if (IS_ERR(key->u.ccmp.tfm)) {
544 err = PTR_ERR(key->u.ccmp.tfm);
545 kfree(key);
546 return ERR_PTR(err);
547 }
548 break;
549 case WLAN_CIPHER_SUITE_CCMP_256:
550 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
551 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
552 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
553 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
554 key->u.ccmp.rx_pn[i][j] =
555 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
556 /* Initialize AES key state here as an optimization so that
557 * it does not need to be initialized for every packet.
558 */
559 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
560 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100561 if (IS_ERR(key->u.ccmp.tfm)) {
562 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200563 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200564 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400565 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200566 break;
567 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200568 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200569 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200570 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
571 key->conf.icv_len = sizeof(struct ieee80211_mmie);
572 else
573 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200574 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200575 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100576 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200577 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200578 /*
579 * Initialize AES key state here as an optimization so that
580 * it does not need to be initialized for every packet.
581 */
582 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200583 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100584 if (IS_ERR(key->u.aes_cmac.tfm)) {
585 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200586 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200587 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200588 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200589 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200590 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
591 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
592 key->conf.iv_len = 0;
593 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
594 if (seq)
595 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
596 key->u.aes_gmac.rx_pn[j] =
597 seq[IEEE80211_GMAC_PN_LEN - j - 1];
598 /* Initialize AES key state here as an optimization so that
599 * it does not need to be initialized for every packet.
600 */
601 key->u.aes_gmac.tfm =
602 ieee80211_aes_gmac_key_setup(key_data, key_len);
603 if (IS_ERR(key->u.aes_gmac.tfm)) {
604 err = PTR_ERR(key->u.aes_gmac.tfm);
605 kfree(key);
606 return ERR_PTR(err);
607 }
608 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200609 case WLAN_CIPHER_SUITE_GCMP:
610 case WLAN_CIPHER_SUITE_GCMP_256:
611 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
612 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
613 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
614 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
615 key->u.gcmp.rx_pn[i][j] =
616 seq[IEEE80211_GCMP_PN_LEN - j - 1];
617 /* Initialize AES key state here as an optimization so that
618 * it does not need to be initialized for every packet.
619 */
620 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
621 key_len);
622 if (IS_ERR(key->u.gcmp.tfm)) {
623 err = PTR_ERR(key->u.gcmp.tfm);
624 kfree(key);
625 return ERR_PTR(err);
626 }
627 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200628 default:
629 if (cs) {
Johannes Berge3a55b52015-05-05 16:32:29 +0200630 if (seq_len && seq_len != cs->pn_len) {
631 kfree(key);
632 return ERR_PTR(-EINVAL);
633 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200634
635 key->conf.iv_len = cs->hdr_len;
636 key->conf.icv_len = cs->mic_len;
637 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berge3a55b52015-05-05 16:32:29 +0200638 for (j = 0; j < seq_len; j++)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200639 key->u.gen.rx_pn[i][j] =
Johannes Berge3a55b52015-05-05 16:32:29 +0200640 seq[seq_len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000641 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200642 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200643 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200644 memcpy(key->conf.key, key_data, key_len);
645 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200646
Johannes Bergdb4d1162008-02-25 16:27:45 +0100647 return key;
648}
Johannes Berg11a843b2007-08-28 17:01:55 -0400649
Johannes Berg79cf2df2013-03-06 22:53:52 +0100650static void ieee80211_key_free_common(struct ieee80211_key *key)
651{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200652 switch (key->conf.cipher) {
653 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200654 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100655 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200656 break;
657 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200658 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100659 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200660 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200661 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
662 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
663 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
664 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200665 case WLAN_CIPHER_SUITE_GCMP:
666 case WLAN_CIPHER_SUITE_GCMP_256:
667 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
668 break;
669 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300670 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100671}
672
Johannes Berg6d10e462013-03-06 23:09:11 +0100673static void __ieee80211_key_destroy(struct ieee80211_key *key,
674 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200675{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530676 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100677 struct ieee80211_sub_if_data *sdata = key->sdata;
678
Jouni Malinen32162a42010-07-26 15:52:03 -0700679 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100680
681 if (delay_tailroom) {
682 /* see ieee80211_delayed_tailroom_dec */
683 sdata->crypto_tx_tailroom_pending_dec++;
684 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
685 HZ/2);
686 } else {
Michal Kaziorf9dca802015-05-13 09:16:48 +0000687 decrease_tailroom_need_count(sdata, 1);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100688 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530689 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200690
Johannes Berg79cf2df2013-03-06 22:53:52 +0100691 ieee80211_key_free_common(key);
692}
693
Johannes Berg6d10e462013-03-06 23:09:11 +0100694static void ieee80211_key_destroy(struct ieee80211_key *key,
695 bool delay_tailroom)
696{
697 if (!key)
698 return;
699
700 /*
Eliad Pelleref044762015-11-17 10:24:37 +0200701 * Synchronize so the TX path and rcu key iterators
702 * can no longer be using this key before we free/remove it.
Johannes Berg6d10e462013-03-06 23:09:11 +0100703 */
704 synchronize_net();
705
706 __ieee80211_key_destroy(key, delay_tailroom);
707}
708
Johannes Berg79cf2df2013-03-06 22:53:52 +0100709void ieee80211_key_free_unused(struct ieee80211_key *key)
710{
711 WARN_ON(key->sdata || key->local);
712 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200713}
714
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200715static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_key *old,
717 struct ieee80211_key *new)
718{
719 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
720 u8 *tk_old, *tk_new;
721
722 if (!old || new->conf.keylen != old->conf.keylen)
723 return false;
724
725 tk_old = old->conf.key;
726 tk_new = new->conf.key;
727
728 /*
729 * In station mode, don't compare the TX MIC key, as it's never used
730 * and offloaded rekeying may not care to send it to the host. This
731 * is the case in iwlwifi, for example.
732 */
733 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
734 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
735 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
736 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
737 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
738 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
739 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
740 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
741 tk_old = tkip_old;
742 tk_new = tkip_new;
743 }
744
745 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
746}
747
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300748int ieee80211_key_link(struct ieee80211_key *key,
749 struct ieee80211_sub_if_data *sdata,
750 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100751{
752 struct ieee80211_key *old_key;
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +0530753 int idx = key->conf.keyidx;
754 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
755 /*
756 * We want to delay tailroom updates only for station - in that
757 * case it helps roaming speed, but in other cases it hurts and
758 * can cause warnings to appear.
759 */
760 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100761 int ret = -EOPNOTSUPP;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100762
Johannes Bergad0e2b52010-06-01 10:19:19 +0200763 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200764
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100765 if (sta && pairwise) {
766 struct ieee80211_key *alt_key;
767
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200768 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100769 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
770
771 /* The rekey code assumes that the old and new key are using
772 * the same cipher. Enforce the assumption for pairwise keys.
773 */
774 if (key &&
775 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
776 (old_key && old_key->conf.cipher != key->conf.cipher)))
777 goto out;
778 } else if (sta) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200779 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100780 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200781 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100782 }
783
784 /* Non-pairwise keys must also not switch the cipher on rekey */
785 if (!pairwise) {
786 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
787 goto out;
788 }
Johannes Bergdb4d1162008-02-25 16:27:45 +0100789
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200790 /*
791 * Silently accept key re-installation without really installing the
792 * new version of the key to avoid nonce reuse or replay issues.
793 */
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200794 if (ieee80211_key_identical(sdata, old_key, key)) {
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200795 ieee80211_key_free_unused(key);
796 ret = 0;
797 goto out;
798 }
799
800 key->local = sdata->local;
801 key->sdata = sdata;
802 key->sta = sta;
803
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530804 increment_tailroom_need_count(sdata);
805
Alexander Wetzel62872a92018-08-31 15:00:38 +0200806 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400807
Alexander Wetzel62872a92018-08-31 15:00:38 +0200808 if (!ret) {
809 ieee80211_debugfs_key_add(key);
810 ieee80211_key_destroy(old_key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200811 } else {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200812 ieee80211_key_free(key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200813 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100814
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200815 out:
Johannes Bergad0e2b52010-06-01 10:19:19 +0200816 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300817
818 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200819}
820
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100821void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200822{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200823 if (!key)
824 return;
825
Johannes Berg3a245762008-04-09 16:45:37 +0200826 /*
827 * Replace key with nothingness if it was ever used.
828 */
829 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100830 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200831 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
832 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100833 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200834}
835
Johannes Berg3b967662008-04-08 17:56:52 +0200836void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
837{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200838 struct ieee80211_key *key;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000839 struct ieee80211_sub_if_data *vlan;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200840
Johannes Berg3a245762008-04-09 16:45:37 +0200841 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200842
Johannes Berg9607e6b662009-12-23 13:15:31 +0100843 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200844 return;
845
Johannes Bergad0e2b52010-06-01 10:19:19 +0200846 mutex_lock(&sdata->local->key_mtx);
847
Michal Kaziorf9dca802015-05-13 09:16:48 +0000848 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
849 sdata->crypto_tx_tailroom_pending_dec);
850
851 if (sdata->vif.type == NL80211_IFTYPE_AP) {
852 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
853 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
854 vlan->crypto_tx_tailroom_pending_dec);
855 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530856
857 list_for_each_entry(key, &sdata->key_list, list) {
858 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200859 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530860 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200861
862 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200863}
864
Michal Kaziorf9dca802015-05-13 09:16:48 +0000865void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
866{
867 struct ieee80211_sub_if_data *vlan;
868
869 mutex_lock(&sdata->local->key_mtx);
870
871 sdata->crypto_tx_tailroom_needed_cnt = 0;
872
873 if (sdata->vif.type == NL80211_IFTYPE_AP) {
874 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
875 vlan->crypto_tx_tailroom_needed_cnt = 0;
876 }
877
878 mutex_unlock(&sdata->local->key_mtx);
879}
880
Johannes Berg830af022011-07-05 16:35:39 +0200881void ieee80211_iter_keys(struct ieee80211_hw *hw,
882 struct ieee80211_vif *vif,
883 void (*iter)(struct ieee80211_hw *hw,
884 struct ieee80211_vif *vif,
885 struct ieee80211_sta *sta,
886 struct ieee80211_key_conf *key,
887 void *data),
888 void *iter_data)
889{
890 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200891 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200892 struct ieee80211_sub_if_data *sdata;
893
894 ASSERT_RTNL();
895
896 mutex_lock(&local->key_mtx);
897 if (vif) {
898 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200899 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200900 iter(hw, &sdata->vif,
901 key->sta ? &key->sta->sta : NULL,
902 &key->conf, iter_data);
903 } else {
904 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200905 list_for_each_entry_safe(key, tmp,
906 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200907 iter(hw, &sdata->vif,
908 key->sta ? &key->sta->sta : NULL,
909 &key->conf, iter_data);
910 }
911 mutex_unlock(&local->key_mtx);
912}
913EXPORT_SYMBOL(ieee80211_iter_keys);
914
Eliad Pelleref044762015-11-17 10:24:37 +0200915static void
916_ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
917 struct ieee80211_sub_if_data *sdata,
918 void (*iter)(struct ieee80211_hw *hw,
919 struct ieee80211_vif *vif,
920 struct ieee80211_sta *sta,
921 struct ieee80211_key_conf *key,
922 void *data),
923 void *iter_data)
924{
925 struct ieee80211_key *key;
926
927 list_for_each_entry_rcu(key, &sdata->key_list, list) {
928 /* skip keys of station in removal process */
929 if (key->sta && key->sta->removed)
930 continue;
931 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
932 continue;
933
934 iter(hw, &sdata->vif,
935 key->sta ? &key->sta->sta : NULL,
936 &key->conf, iter_data);
937 }
938}
939
940void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
941 struct ieee80211_vif *vif,
942 void (*iter)(struct ieee80211_hw *hw,
943 struct ieee80211_vif *vif,
944 struct ieee80211_sta *sta,
945 struct ieee80211_key_conf *key,
946 void *data),
947 void *iter_data)
948{
949 struct ieee80211_local *local = hw_to_local(hw);
950 struct ieee80211_sub_if_data *sdata;
951
952 if (vif) {
953 sdata = vif_to_sdata(vif);
954 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
955 } else {
956 list_for_each_entry_rcu(sdata, &local->interfaces, list)
957 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
958 }
959}
960EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
961
Johannes Berg7907c7d2013-12-04 23:47:09 +0100962static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
963 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400964{
965 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200966
Michal Kaziorf9dca802015-05-13 09:16:48 +0000967 decrease_tailroom_need_count(sdata,
968 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100969 sdata->crypto_tx_tailroom_pending_dec = 0;
970
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200971 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400972
Johannes Berg6d10e462013-03-06 23:09:11 +0100973 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
974 ieee80211_key_replace(key->sdata, key->sta,
975 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
976 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100977 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100978 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400979
Johannes Bergf7e01042010-12-09 19:49:02 +0100980 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100981}
Johannes Bergf7e01042010-12-09 19:49:02 +0100982
Johannes Berg7907c7d2013-12-04 23:47:09 +0100983void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
984 bool force_synchronize)
985{
986 struct ieee80211_local *local = sdata->local;
987 struct ieee80211_sub_if_data *vlan;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000988 struct ieee80211_sub_if_data *master;
Johannes Berg7907c7d2013-12-04 23:47:09 +0100989 struct ieee80211_key *key, *tmp;
990 LIST_HEAD(keys);
991
992 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
993
994 mutex_lock(&local->key_mtx);
995
996 ieee80211_free_keys_iface(sdata, &keys);
997
998 if (sdata->vif.type == NL80211_IFTYPE_AP) {
999 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1000 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +01001001 }
1002
Johannes Berg7907c7d2013-12-04 23:47:09 +01001003 if (!list_empty(&keys) || force_synchronize)
1004 synchronize_net();
1005 list_for_each_entry_safe(key, tmp, &keys, list)
1006 __ieee80211_key_destroy(key, false);
1007
Michal Kaziorf9dca802015-05-13 09:16:48 +00001008 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1009 if (sdata->bss) {
1010 master = container_of(sdata->bss,
1011 struct ieee80211_sub_if_data,
1012 u.ap);
1013
1014 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1015 master->crypto_tx_tailroom_needed_cnt);
1016 }
1017 } else {
1018 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1019 sdata->crypto_tx_tailroom_pending_dec);
1020 }
1021
Johannes Berg7907c7d2013-12-04 23:47:09 +01001022 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1023 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1024 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1025 vlan->crypto_tx_tailroom_pending_dec);
1026 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001027
Johannes Berg7907c7d2013-12-04 23:47:09 +01001028 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -04001029}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001030
Johannes Berg6d10e462013-03-06 23:09:11 +01001031void ieee80211_free_sta_keys(struct ieee80211_local *local,
1032 struct sta_info *sta)
1033{
Johannes Bergc8782072013-12-04 23:05:45 +01001034 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +01001035 int i;
1036
1037 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +01001038 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +01001039 key = key_mtx_dereference(local, sta->gtk[i]);
1040 if (!key)
1041 continue;
1042 ieee80211_key_replace(key->sdata, key->sta,
1043 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1044 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301045 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1046 NL80211_IFTYPE_STATION);
Johannes Berg6d10e462013-03-06 23:09:11 +01001047 }
1048
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001049 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1050 key = key_mtx_dereference(local, sta->ptk[i]);
1051 if (!key)
1052 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +01001053 ieee80211_key_replace(key->sdata, key->sta,
1054 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1055 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301056 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1057 NL80211_IFTYPE_STATION);
Johannes Bergc8782072013-12-04 23:05:45 +01001058 }
Johannes Berg6d10e462013-03-06 23:09:11 +01001059
1060 mutex_unlock(&local->key_mtx);
1061}
1062
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001063void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1064{
1065 struct ieee80211_sub_if_data *sdata;
1066
1067 sdata = container_of(wk, struct ieee80211_sub_if_data,
1068 dec_tailroom_needed_wk.work);
1069
1070 /*
1071 * The reason for the delayed tailroom needed decrementing is to
1072 * make roaming faster: during roaming, all keys are first deleted
1073 * and then new keys are installed. The first new key causes the
1074 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1075 * the cost of synchronize_net() (which can be slow). Avoid this
1076 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1077 * key removal for a while, so if we roam the value is larger than
1078 * zero and no 0->1 transition happens.
1079 *
1080 * The cost is that if the AP switching was from an AP with keys
1081 * to one without, we still allocate tailroom while it would no
1082 * longer be needed. However, in the typical (fast) roaming case
1083 * within an ESS this usually won't happen.
1084 */
1085
1086 mutex_lock(&sdata->local->key_mtx);
Michal Kaziorf9dca802015-05-13 09:16:48 +00001087 decrease_tailroom_need_count(sdata,
1088 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001089 sdata->crypto_tx_tailroom_pending_dec = 0;
1090 mutex_unlock(&sdata->local->key_mtx);
1091}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001092
1093void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1094 const u8 *replay_ctr, gfp_t gfp)
1095{
1096 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1097
1098 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1099
1100 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1101}
1102EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001103
Johannes Berg3ea542d2011-07-07 18:58:00 +02001104void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1105 int tid, struct ieee80211_key_seq *seq)
1106{
1107 struct ieee80211_key *key;
1108 const u8 *pn;
1109
1110 key = container_of(keyconf, struct ieee80211_key, conf);
1111
1112 switch (key->conf.cipher) {
1113 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +01001114 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001115 return;
1116 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1117 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1118 break;
1119 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001120 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +01001121 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001122 return;
1123 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +01001124 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +02001125 else
1126 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +02001127 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001128 break;
1129 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001130 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +02001131 if (WARN_ON(tid != 0))
1132 return;
1133 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001134 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001135 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001136 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1137 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1138 if (WARN_ON(tid != 0))
1139 return;
1140 pn = key->u.aes_gmac.rx_pn;
1141 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1142 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001143 case WLAN_CIPHER_SUITE_GCMP:
1144 case WLAN_CIPHER_SUITE_GCMP_256:
1145 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1146 return;
1147 if (tid < 0)
1148 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1149 else
1150 pn = key->u.gcmp.rx_pn[tid];
1151 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1152 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +02001153 }
1154}
1155EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001156
Johannes Berg27b3eb92013-08-07 20:11:55 +02001157void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1158 int tid, struct ieee80211_key_seq *seq)
1159{
1160 struct ieee80211_key *key;
1161 u8 *pn;
1162
1163 key = container_of(keyconf, struct ieee80211_key, conf);
1164
1165 switch (key->conf.cipher) {
1166 case WLAN_CIPHER_SUITE_TKIP:
1167 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1168 return;
1169 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1170 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1171 break;
1172 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001173 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001174 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1175 return;
1176 if (tid < 0)
1177 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1178 else
1179 pn = key->u.ccmp.rx_pn[tid];
1180 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1181 break;
1182 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001183 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001184 if (WARN_ON(tid != 0))
1185 return;
1186 pn = key->u.aes_cmac.rx_pn;
1187 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1188 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001189 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1190 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1191 if (WARN_ON(tid != 0))
1192 return;
1193 pn = key->u.aes_gmac.rx_pn;
1194 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1195 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001196 case WLAN_CIPHER_SUITE_GCMP:
1197 case WLAN_CIPHER_SUITE_GCMP_256:
1198 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1199 return;
1200 if (tid < 0)
1201 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1202 else
1203 pn = key->u.gcmp.rx_pn[tid];
1204 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1205 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001206 default:
1207 WARN_ON(1);
1208 break;
1209 }
1210}
1211EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1212
1213void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1214{
1215 struct ieee80211_key *key;
1216
1217 key = container_of(keyconf, struct ieee80211_key, conf);
1218
1219 assert_key_lock(key->local);
1220
1221 /*
1222 * if key was uploaded, we assume the driver will/has remove(d)
1223 * it, so adjust bookkeeping accordingly
1224 */
1225 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1226 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1227
Alexander Wetzel092c4092019-03-16 21:44:43 +01001228 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1229 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1230 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001231 increment_tailroom_need_count(key->sdata);
1232 }
1233
1234 ieee80211_key_free(key, false);
1235}
1236EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1237
1238struct ieee80211_key_conf *
1239ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1240 struct ieee80211_key_conf *keyconf)
1241{
1242 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1243 struct ieee80211_local *local = sdata->local;
1244 struct ieee80211_key *key;
1245 int err;
1246
1247 if (WARN_ON(!local->wowlan))
1248 return ERR_PTR(-EINVAL);
1249
1250 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1251 return ERR_PTR(-EINVAL);
1252
1253 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1254 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001255 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001256 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001257 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001258
1259 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1260 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1261
1262 err = ieee80211_key_link(key, sdata, NULL);
1263 if (err)
1264 return ERR_PTR(err);
1265
1266 return &key->conf;
1267}
1268EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);