blob: 20bf9db7a3886de7601fabd0e0ff2c721e54e30e [file] [log] [blame]
Johannes Berg1f5a7e42007-07-27 15:43:23 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Berg3b967662008-04-08 17:56:52 +02005 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Bergfdf7cb42017-09-05 14:54:54 +02007 * Copyright 2015-2017 Intel Deutschland GmbH
Johannes Berg1f5a7e42007-07-27 15:43:23 +02008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Johannes Berg11a843b2007-08-28 17:01:55 -040014#include <linux/if_ether.h>
15#include <linux/etherdevice.h>
16#include <linux/list.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040017#include <linux/rcupdate.h>
Johannes Bergdb4d1162008-02-25 16:27:45 +010018#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040020#include <linux/export.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020021#include <net/mac80211.h>
Jason A. Donenfeld2bdd7132017-10-17 20:32:07 +020022#include <crypto/algapi.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010023#include <asm/unaligned.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020024#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020026#include "debugfs_key.h"
27#include "aes_ccm.h"
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +020028#include "aes_cmac.h"
Jouni Malinen8ade5382015-01-24 19:52:09 +020029#include "aes_gmac.h"
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020030#include "aes_gcm.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020031
Johannes Berg11a843b2007-08-28 17:01:55 -040032
Johannes Bergdbbea672008-02-26 14:34:06 +010033/**
34 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040035 *
36 * Key handling in mac80211 is done based on per-interface (sub_if_data)
37 * keys and per-station keys. Since each station belongs to an interface,
38 * each station key also belongs to that interface.
39 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010040 * Hardware acceleration is done on a best-effort basis for algorithms
41 * that are implemented in software, for each key the hardware is asked
42 * to enable that key for offloading but if it cannot do that the key is
43 * simply kept for software encryption (unless it is for an algorithm
44 * that isn't implemented in software).
45 * There is currently no way of knowing whether a key is handled in SW
46 * or HW except by looking into debugfs.
Johannes Berg11a843b2007-08-28 17:01:55 -040047 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010048 * All key management is internally protected by a mutex. Within all
49 * other parts of mac80211, key references are, just as STA structure
50 * references, protected by RCU. Note, however, that some things are
51 * unprotected, namely the key->sta dereferences within the hardware
52 * acceleration functions. This means that sta_info_destroy() must
53 * remove the key which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040054 */
55
56static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040057
Johannes Bergad0e2b52010-06-01 10:19:19 +020058static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020059{
Johannes Berg46a5eba2010-09-15 13:28:15 +020060 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020061}
62
Michal Kaziorf9dca802015-05-13 09:16:48 +000063static void
64update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
65{
66 struct ieee80211_sub_if_data *vlan;
67
68 if (sdata->vif.type != NL80211_IFTYPE_AP)
69 return;
70
Johannes Berg51f458d2015-06-17 13:54:54 +020071 /* crypto_tx_tailroom_needed_cnt is protected by this */
72 assert_key_lock(sdata->local);
Michal Kaziorf9dca802015-05-13 09:16:48 +000073
Johannes Berg51f458d2015-06-17 13:54:54 +020074 rcu_read_lock();
75
76 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list)
Michal Kaziorf9dca802015-05-13 09:16:48 +000077 vlan->crypto_tx_tailroom_needed_cnt += delta;
78
Johannes Berg51f458d2015-06-17 13:54:54 +020079 rcu_read_unlock();
Michal Kaziorf9dca802015-05-13 09:16:48 +000080}
81
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053082static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
83{
84 /*
85 * When this count is zero, SKB resizing for allocating tailroom
86 * for IV or MMIC is skipped. But, this check has created two race
87 * cases in xmit path while transiting from zero count to one:
88 *
89 * 1. SKB resize was skipped because no key was added but just before
90 * the xmit key is added and SW encryption kicks off.
91 *
92 * 2. SKB resize was skipped because all the keys were hw planted but
93 * just before xmit one of the key is deleted and SW encryption kicks
94 * off.
95 *
96 * In both the above case SW encryption will find not enough space for
97 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
98 *
99 * Solution has been explained at
100 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
101 */
102
Johannes Berg51f458d2015-06-17 13:54:54 +0200103 assert_key_lock(sdata->local);
104
Michal Kaziorf9dca802015-05-13 09:16:48 +0000105 update_vlan_tailroom_need_count(sdata, 1);
106
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530107 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
108 /*
109 * Flush all XMIT packets currently using HW encryption or no
110 * encryption at all if the count transition is from 0 -> 1.
111 */
112 synchronize_net();
113 }
114}
115
Michal Kaziorf9dca802015-05-13 09:16:48 +0000116static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
117 int delta)
118{
Johannes Berg51f458d2015-06-17 13:54:54 +0200119 assert_key_lock(sdata->local);
120
Michal Kaziorf9dca802015-05-13 09:16:48 +0000121 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
122
123 update_vlan_tailroom_need_count(sdata, -delta);
124 sdata->crypto_tx_tailroom_needed_cnt -= delta;
125}
126
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300127static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400128{
Manikanta Pubbisettydb3bdcb2018-03-28 18:34:19 +0530129 struct ieee80211_sub_if_data *sdata = key->sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100130 struct sta_info *sta;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100131 int ret = -EOPNOTSUPP;
Johannes Berg11a843b2007-08-28 17:01:55 -0400132
Johannes Berg3b967662008-04-08 17:56:52 +0200133 might_sleep();
134
Johannes Berg46191942014-10-13 13:43:29 +0200135 if (key->flags & KEY_FLAG_TAINTED) {
136 /* If we get here, it's during resume and the key is
137 * tainted so shouldn't be used/programmed any more.
138 * However, its flags may still indicate that it was
139 * programmed into the device (since we're in resume)
140 * so clear that flag now to avoid trying to remove
141 * it again later.
142 */
Alexander Wetzel092c4092019-03-16 21:44:43 +0100143 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
144 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
145 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
146 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
147 increment_tailroom_need_count(sdata);
148
Johannes Berg46191942014-10-13 13:43:29 +0200149 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200150 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200151 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200152
Johannes Berge31b8212010-10-05 19:39:30 +0200153 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300154 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400155
Johannes Bergad0e2b52010-06-01 10:19:19 +0200156 assert_key_lock(key->local);
157
Johannes Berg89c91ca2012-01-20 13:55:19 +0100158 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100159
Johannes Berge31b8212010-10-05 19:39:30 +0200160 /*
161 * If this is a per-STA GTK, check if it
162 * is supported; if not, return.
163 */
164 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200165 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK))
Johannes Berge31b8212010-10-05 19:39:30 +0200166 goto out_unsupported;
167
Johannes Berg89c91ca2012-01-20 13:55:19 +0100168 if (sta && !sta->uploaded)
169 goto out_unsupported;
170
Helmut Schaa18890d42010-11-19 08:11:01 +0100171 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
172 /*
173 * The driver doesn't know anything about VLAN interfaces.
174 * Hence, don't send GTKs for VLAN interfaces to the driver.
175 */
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100176 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
177 ret = 1;
Helmut Schaa18890d42010-11-19 08:11:01 +0100178 goto out_unsupported;
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100179 }
Helmut Schaa18890d42010-11-19 08:11:01 +0100180 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400181
Johannes Berg89c91ca2012-01-20 13:55:19 +0100182 ret = drv_set_key(key->local, SET_KEY, sdata,
183 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400184
Johannes Berge31b8212010-10-05 19:39:30 +0200185 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400186 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530187
Alexander Wetzel092c4092019-03-16 21:44:43 +0100188 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
189 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
190 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Michal Kaziorf9dca802015-05-13 09:16:48 +0000191 decrease_tailroom_need_count(sdata, 1);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530192
Arik Nemtsov077a9152011-10-23 08:21:41 +0200193 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
194 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
195
David Spinadel9de18d82017-12-01 13:50:52 +0200196 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
197 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
198
Johannes Berge31b8212010-10-05 19:39:30 +0200199 return 0;
200 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400201
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100202 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200203 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700204 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100205 key->conf.keyidx,
206 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300207
Johannes Berge31b8212010-10-05 19:39:30 +0200208 out_unsupported:
209 switch (key->conf.cipher) {
210 case WLAN_CIPHER_SUITE_WEP40:
211 case WLAN_CIPHER_SUITE_WEP104:
212 case WLAN_CIPHER_SUITE_TKIP:
213 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200214 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200215 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200216 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200217 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
218 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200219 case WLAN_CIPHER_SUITE_GCMP:
220 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100221 /* all of these we can do in software - if driver can */
222 if (ret == 1)
223 return 0;
Alexander Wetzel78ad2342019-02-09 15:01:38 +0100224 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL))
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100225 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200226 return 0;
227 default:
228 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300229 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400230}
231
232static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
233{
Johannes Bergdc822b52008-12-29 12:55:09 +0100234 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100235 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400236 int ret;
237
Johannes Berg3b967662008-04-08 17:56:52 +0200238 might_sleep();
239
Johannes Bergdb4d1162008-02-25 16:27:45 +0100240 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400241 return;
242
Johannes Bergad0e2b52010-06-01 10:19:19 +0200243 assert_key_lock(key->local);
244
245 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400246 return;
247
Johannes Berg89c91ca2012-01-20 13:55:19 +0100248 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100249 sdata = key->sdata;
250
Alexander Wetzel092c4092019-03-16 21:44:43 +0100251 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
252 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
253 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530254 increment_tailroom_need_count(sdata);
255
Alexander Wetzel62872a92018-08-31 15:00:38 +0200256 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg12375ef2009-11-25 20:30:31 +0100257 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100258 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400259
260 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200261 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700262 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100263 key->conf.keyidx,
264 sta ? sta->sta.addr : bcast_addr, ret);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200265}
Johannes Berg11a843b2007-08-28 17:01:55 -0400266
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100267int ieee80211_set_tx_key(struct ieee80211_key *key)
268{
269 struct sta_info *sta = key->sta;
270 struct ieee80211_local *local = key->local;
271 struct ieee80211_key *old;
272
273 assert_key_lock(local);
274
275 old = key_mtx_dereference(local, sta->ptk[sta->ptk_idx]);
276 sta->ptk_idx = key->conf.keyidx;
277 ieee80211_check_fast_xmit(sta);
278
279 return 0;
280}
281
Alexander Wetzel62872a92018-08-31 15:00:38 +0200282static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
283 struct ieee80211_key *new_key,
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100284 bool pairwise)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200285{
286 struct ieee80211_sub_if_data *sdata;
287 struct ieee80211_local *local;
288 struct sta_info *sta;
289 int ret;
290
291 /* Aggregation sessions are OK when running on SW crypto.
292 * A broken remote STA may cause issues not observed with HW
293 * crypto, though.
294 */
295 if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
296 return 0;
297
298 assert_key_lock(old_key->local);
299 sta = old_key->sta;
300
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100301 /* Unicast rekey without Extended Key ID needs special handling */
302 if (new_key && sta && pairwise &&
303 rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200304 local = old_key->local;
305 sdata = old_key->sdata;
306
307 /* Stop TX till we are on the new key */
308 old_key->flags |= KEY_FLAG_TAINTED;
309 ieee80211_clear_fast_xmit(sta);
310
311 /* Aggregation sessions during rekey are complicated due to the
312 * reorder buffer and retransmits. Side step that by blocking
313 * aggregation during rekey and tear down running sessions.
314 */
315 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
316 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
317 ieee80211_sta_tear_down_BA_sessions(sta,
318 AGG_STOP_LOCAL_REQUEST);
319 }
320
321 if (!wiphy_ext_feature_isset(local->hw.wiphy,
322 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
323 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
324 sta->sta.addr);
325 /* Flushing the driver queues *may* help prevent
326 * the clear text leaks and freezes.
327 */
328 ieee80211_flush_queues(local, sdata, false);
329 }
330 }
331
332 ieee80211_key_disable_hw_accel(old_key);
333
334 if (new_key)
335 ret = ieee80211_key_enable_hw_accel(new_key);
336 else
337 ret = 0;
338
339 return ret;
Johannes Berg3b967662008-04-08 17:56:52 +0200340}
341
342static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100343 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200344{
345 struct ieee80211_key *key = NULL;
346
Johannes Bergad0e2b52010-06-01 10:19:19 +0200347 assert_key_lock(sdata->local);
348
Johannes Berg3b967662008-04-08 17:56:52 +0200349 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200350 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200351
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300352 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100353 rcu_assign_pointer(sdata->default_unicast_key, key);
Johannes Berg17c18bf2015-03-21 15:25:43 +0100354 ieee80211_check_fast_xmit_iface(sdata);
Johannes Bergec4efc42016-12-13 09:39:18 +0100355 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
356 drv_set_default_unicast_key(sdata->local, sdata, idx);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300357 }
358
Johannes Bergf7e01042010-12-09 19:49:02 +0100359 if (multi)
360 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200361
Johannes Bergf7e01042010-12-09 19:49:02 +0100362 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200363}
364
Johannes Bergf7e01042010-12-09 19:49:02 +0100365void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
366 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200367{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200368 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100369 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200370 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200371}
372
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200373static void
374__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
375{
376 struct ieee80211_key *key = NULL;
377
Johannes Bergad0e2b52010-06-01 10:19:19 +0200378 assert_key_lock(sdata->local);
379
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200380 if (idx >= NUM_DEFAULT_KEYS &&
381 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200382 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200383
384 rcu_assign_pointer(sdata->default_mgmt_key, key);
385
Johannes Bergf7e01042010-12-09 19:49:02 +0100386 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200387}
388
389void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
390 int idx)
391{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200392 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200393 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200394 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200395}
396
Johannes Berg3b967662008-04-08 17:56:52 +0200397
Alexander Wetzel62872a92018-08-31 15:00:38 +0200398static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100399 struct sta_info *sta,
400 bool pairwise,
401 struct ieee80211_key *old,
402 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200403{
Johannes Bergf7e01042010-12-09 19:49:02 +0100404 int idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200405 int ret;
Johannes Bergf7e01042010-12-09 19:49:02 +0100406 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200407
Johannes Berg5282c3b2013-10-29 10:00:08 +0100408 /* caller must provide at least one old/new */
409 if (WARN_ON(!new && !old))
Alexander Wetzel62872a92018-08-31 15:00:38 +0200410 return 0;
Johannes Berg5282c3b2013-10-29 10:00:08 +0100411
Johannes Berg3b967662008-04-08 17:56:52 +0200412 if (new)
Eliad Pelleref044762015-11-17 10:24:37 +0200413 list_add_tail_rcu(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200414
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200415 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
416
Alexander Wetzel62872a92018-08-31 15:00:38 +0200417 if (old) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200418 idx = old->conf.keyidx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200419 ret = ieee80211_hw_key_replace(old, new, pairwise);
420 } else {
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500421 /* new must be provided in case old is not */
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200422 idx = new->conf.keyidx;
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500423 if (!new->local->wowlan)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200424 ret = ieee80211_key_enable_hw_accel(new);
425 else
426 ret = 0;
427 }
428
429 if (ret)
430 return ret;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200431
432 if (sta) {
433 if (pairwise) {
434 rcu_assign_pointer(sta->ptk[idx], new);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100435 if (new &&
436 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
437 sta->ptk_idx = idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200438 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
439 ieee80211_check_fast_xmit(sta);
440 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200441 } else {
442 rcu_assign_pointer(sta->gtk[idx], new);
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200443 }
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100444 /* Only needed for transition from no key -> key.
445 * Still triggers unnecessary when using Extended Key ID
446 * and installing the second key ID the first time.
447 */
448 if (new && !old)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200449 ieee80211_check_fast_rx(sta);
Johannes Berg3b967662008-04-08 17:56:52 +0200450 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200451 defunikey = old &&
452 old == key_mtx_dereference(sdata->local,
453 sdata->default_unicast_key);
454 defmultikey = old &&
455 old == key_mtx_dereference(sdata->local,
456 sdata->default_multicast_key);
457 defmgmtkey = old &&
458 old == key_mtx_dereference(sdata->local,
459 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200460
Johannes Bergf7e01042010-12-09 19:49:02 +0100461 if (defunikey && !new)
462 __ieee80211_set_default_key(sdata, -1, true, false);
463 if (defmultikey && !new)
464 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200465 if (defmgmtkey && !new)
466 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200467
468 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100469 if (defunikey && new)
470 __ieee80211_set_default_key(sdata, new->conf.keyidx,
471 true, false);
472 if (defmultikey && new)
473 __ieee80211_set_default_key(sdata, new->conf.keyidx,
474 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200475 if (defmgmtkey && new)
476 __ieee80211_set_default_mgmt_key(sdata,
477 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200478 }
479
Johannes Bergb5c34f62011-01-03 19:51:09 +0100480 if (old)
Eliad Pelleref044762015-11-17 10:24:37 +0200481 list_del_rcu(&old->list);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200482
483 return 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400484}
485
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200486struct ieee80211_key *
487ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
488 const u8 *key_data,
489 size_t seq_len, const u8 *seq,
490 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200491{
492 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100493 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200494
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200495 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
496 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400497
498 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200499 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100500 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400501
502 /*
503 * Default to software encryption; we'll later upload the
504 * key to the hardware if possible.
505 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400506 key->conf.flags = 0;
507 key->flags = 0;
508
Johannes Berg97359d12010-08-10 09:46:38 +0200509 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400510 key->conf.keyidx = idx;
511 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200512 switch (cipher) {
513 case WLAN_CIPHER_SUITE_WEP40:
514 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200515 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
516 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200517 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200518 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200519 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
520 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300521 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100522 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300523 key->u.tkip.rx[i].iv32 =
524 get_unaligned_le32(&seq[2]);
525 key->u.tkip.rx[i].iv16 =
526 get_unaligned_le16(seq);
527 }
528 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200529 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200530 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200531 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200532 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
533 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300534 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100535 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200536 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300537 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200538 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300539 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400540 /*
541 * Initialize AES key state here as an optimization so that
542 * it does not need to be initialized for every packet.
543 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200544 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
545 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
546 if (IS_ERR(key->u.ccmp.tfm)) {
547 err = PTR_ERR(key->u.ccmp.tfm);
548 kfree(key);
549 return ERR_PTR(err);
550 }
551 break;
552 case WLAN_CIPHER_SUITE_CCMP_256:
553 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
554 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
555 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
556 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
557 key->u.ccmp.rx_pn[i][j] =
558 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
559 /* Initialize AES key state here as an optimization so that
560 * it does not need to be initialized for every packet.
561 */
562 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
563 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100564 if (IS_ERR(key->u.ccmp.tfm)) {
565 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200566 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200567 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400568 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200569 break;
570 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200571 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200572 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200573 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
574 key->conf.icv_len = sizeof(struct ieee80211_mmie);
575 else
576 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200577 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200578 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100579 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200580 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200581 /*
582 * Initialize AES key state here as an optimization so that
583 * it does not need to be initialized for every packet.
584 */
585 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200586 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100587 if (IS_ERR(key->u.aes_cmac.tfm)) {
588 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200589 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200590 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200591 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200592 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200593 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
594 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
595 key->conf.iv_len = 0;
596 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
597 if (seq)
598 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
599 key->u.aes_gmac.rx_pn[j] =
600 seq[IEEE80211_GMAC_PN_LEN - j - 1];
601 /* Initialize AES key state here as an optimization so that
602 * it does not need to be initialized for every packet.
603 */
604 key->u.aes_gmac.tfm =
605 ieee80211_aes_gmac_key_setup(key_data, key_len);
606 if (IS_ERR(key->u.aes_gmac.tfm)) {
607 err = PTR_ERR(key->u.aes_gmac.tfm);
608 kfree(key);
609 return ERR_PTR(err);
610 }
611 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200612 case WLAN_CIPHER_SUITE_GCMP:
613 case WLAN_CIPHER_SUITE_GCMP_256:
614 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
615 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
616 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
617 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
618 key->u.gcmp.rx_pn[i][j] =
619 seq[IEEE80211_GCMP_PN_LEN - j - 1];
620 /* Initialize AES key state here as an optimization so that
621 * it does not need to be initialized for every packet.
622 */
623 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
624 key_len);
625 if (IS_ERR(key->u.gcmp.tfm)) {
626 err = PTR_ERR(key->u.gcmp.tfm);
627 kfree(key);
628 return ERR_PTR(err);
629 }
630 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200631 default:
632 if (cs) {
Johannes Berge3a55b52015-05-05 16:32:29 +0200633 if (seq_len && seq_len != cs->pn_len) {
634 kfree(key);
635 return ERR_PTR(-EINVAL);
636 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200637
638 key->conf.iv_len = cs->hdr_len;
639 key->conf.icv_len = cs->mic_len;
640 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berge3a55b52015-05-05 16:32:29 +0200641 for (j = 0; j < seq_len; j++)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200642 key->u.gen.rx_pn[i][j] =
Johannes Berge3a55b52015-05-05 16:32:29 +0200643 seq[seq_len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000644 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200645 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200646 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200647 memcpy(key->conf.key, key_data, key_len);
648 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200649
Johannes Bergdb4d1162008-02-25 16:27:45 +0100650 return key;
651}
Johannes Berg11a843b2007-08-28 17:01:55 -0400652
Johannes Berg79cf2df2013-03-06 22:53:52 +0100653static void ieee80211_key_free_common(struct ieee80211_key *key)
654{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200655 switch (key->conf.cipher) {
656 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200657 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100658 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200659 break;
660 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200661 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100662 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200663 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200664 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
665 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
666 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
667 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200668 case WLAN_CIPHER_SUITE_GCMP:
669 case WLAN_CIPHER_SUITE_GCMP_256:
670 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
671 break;
672 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300673 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100674}
675
Johannes Berg6d10e462013-03-06 23:09:11 +0100676static void __ieee80211_key_destroy(struct ieee80211_key *key,
677 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200678{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530679 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100680 struct ieee80211_sub_if_data *sdata = key->sdata;
681
Jouni Malinen32162a42010-07-26 15:52:03 -0700682 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100683
684 if (delay_tailroom) {
685 /* see ieee80211_delayed_tailroom_dec */
686 sdata->crypto_tx_tailroom_pending_dec++;
687 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
688 HZ/2);
689 } else {
Michal Kaziorf9dca802015-05-13 09:16:48 +0000690 decrease_tailroom_need_count(sdata, 1);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100691 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530692 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200693
Johannes Berg79cf2df2013-03-06 22:53:52 +0100694 ieee80211_key_free_common(key);
695}
696
Johannes Berg6d10e462013-03-06 23:09:11 +0100697static void ieee80211_key_destroy(struct ieee80211_key *key,
698 bool delay_tailroom)
699{
700 if (!key)
701 return;
702
703 /*
Eliad Pelleref044762015-11-17 10:24:37 +0200704 * Synchronize so the TX path and rcu key iterators
705 * can no longer be using this key before we free/remove it.
Johannes Berg6d10e462013-03-06 23:09:11 +0100706 */
707 synchronize_net();
708
709 __ieee80211_key_destroy(key, delay_tailroom);
710}
711
Johannes Berg79cf2df2013-03-06 22:53:52 +0100712void ieee80211_key_free_unused(struct ieee80211_key *key)
713{
714 WARN_ON(key->sdata || key->local);
715 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200716}
717
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200718static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
719 struct ieee80211_key *old,
720 struct ieee80211_key *new)
721{
722 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
723 u8 *tk_old, *tk_new;
724
725 if (!old || new->conf.keylen != old->conf.keylen)
726 return false;
727
728 tk_old = old->conf.key;
729 tk_new = new->conf.key;
730
731 /*
732 * In station mode, don't compare the TX MIC key, as it's never used
733 * and offloaded rekeying may not care to send it to the host. This
734 * is the case in iwlwifi, for example.
735 */
736 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
737 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
738 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
739 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
740 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
741 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
742 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
743 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
744 tk_old = tkip_old;
745 tk_new = tkip_new;
746 }
747
748 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
749}
750
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300751int ieee80211_key_link(struct ieee80211_key *key,
752 struct ieee80211_sub_if_data *sdata,
753 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100754{
755 struct ieee80211_key *old_key;
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +0530756 int idx = key->conf.keyidx;
757 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
758 /*
759 * We want to delay tailroom updates only for station - in that
760 * case it helps roaming speed, but in other cases it hurts and
761 * can cause warnings to appear.
762 */
763 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100764 int ret = -EOPNOTSUPP;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100765
Johannes Bergad0e2b52010-06-01 10:19:19 +0200766 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200767
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100768 if (sta && pairwise) {
769 struct ieee80211_key *alt_key;
770
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200771 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100772 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
773
774 /* The rekey code assumes that the old and new key are using
775 * the same cipher. Enforce the assumption for pairwise keys.
776 */
777 if (key &&
778 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
779 (old_key && old_key->conf.cipher != key->conf.cipher)))
780 goto out;
781 } else if (sta) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200782 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100783 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200784 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100785 }
786
787 /* Non-pairwise keys must also not switch the cipher on rekey */
788 if (!pairwise) {
789 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
790 goto out;
791 }
Johannes Bergdb4d1162008-02-25 16:27:45 +0100792
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200793 /*
794 * Silently accept key re-installation without really installing the
795 * new version of the key to avoid nonce reuse or replay issues.
796 */
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200797 if (ieee80211_key_identical(sdata, old_key, key)) {
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200798 ieee80211_key_free_unused(key);
799 ret = 0;
800 goto out;
801 }
802
803 key->local = sdata->local;
804 key->sdata = sdata;
805 key->sta = sta;
806
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530807 increment_tailroom_need_count(sdata);
808
Alexander Wetzel62872a92018-08-31 15:00:38 +0200809 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400810
Alexander Wetzel62872a92018-08-31 15:00:38 +0200811 if (!ret) {
812 ieee80211_debugfs_key_add(key);
813 ieee80211_key_destroy(old_key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200814 } else {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200815 ieee80211_key_free(key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200816 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100817
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200818 out:
Johannes Bergad0e2b52010-06-01 10:19:19 +0200819 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300820
821 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200822}
823
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100824void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200825{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200826 if (!key)
827 return;
828
Johannes Berg3a245762008-04-09 16:45:37 +0200829 /*
830 * Replace key with nothingness if it was ever used.
831 */
832 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100833 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200834 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
835 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100836 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200837}
838
Johannes Berg3b967662008-04-08 17:56:52 +0200839void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
840{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200841 struct ieee80211_key *key;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000842 struct ieee80211_sub_if_data *vlan;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200843
Johannes Berg3a245762008-04-09 16:45:37 +0200844 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200845
Johannes Berg9607e6b662009-12-23 13:15:31 +0100846 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200847 return;
848
Johannes Bergad0e2b52010-06-01 10:19:19 +0200849 mutex_lock(&sdata->local->key_mtx);
850
Michal Kaziorf9dca802015-05-13 09:16:48 +0000851 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
852 sdata->crypto_tx_tailroom_pending_dec);
853
854 if (sdata->vif.type == NL80211_IFTYPE_AP) {
855 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
856 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
857 vlan->crypto_tx_tailroom_pending_dec);
858 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530859
860 list_for_each_entry(key, &sdata->key_list, list) {
861 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200862 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530863 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200864
865 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200866}
867
Michal Kaziorf9dca802015-05-13 09:16:48 +0000868void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
869{
870 struct ieee80211_sub_if_data *vlan;
871
872 mutex_lock(&sdata->local->key_mtx);
873
874 sdata->crypto_tx_tailroom_needed_cnt = 0;
875
876 if (sdata->vif.type == NL80211_IFTYPE_AP) {
877 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
878 vlan->crypto_tx_tailroom_needed_cnt = 0;
879 }
880
881 mutex_unlock(&sdata->local->key_mtx);
882}
883
Johannes Berg830af022011-07-05 16:35:39 +0200884void ieee80211_iter_keys(struct ieee80211_hw *hw,
885 struct ieee80211_vif *vif,
886 void (*iter)(struct ieee80211_hw *hw,
887 struct ieee80211_vif *vif,
888 struct ieee80211_sta *sta,
889 struct ieee80211_key_conf *key,
890 void *data),
891 void *iter_data)
892{
893 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200894 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200895 struct ieee80211_sub_if_data *sdata;
896
897 ASSERT_RTNL();
898
899 mutex_lock(&local->key_mtx);
900 if (vif) {
901 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200902 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200903 iter(hw, &sdata->vif,
904 key->sta ? &key->sta->sta : NULL,
905 &key->conf, iter_data);
906 } else {
907 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200908 list_for_each_entry_safe(key, tmp,
909 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200910 iter(hw, &sdata->vif,
911 key->sta ? &key->sta->sta : NULL,
912 &key->conf, iter_data);
913 }
914 mutex_unlock(&local->key_mtx);
915}
916EXPORT_SYMBOL(ieee80211_iter_keys);
917
Eliad Pelleref044762015-11-17 10:24:37 +0200918static void
919_ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
920 struct ieee80211_sub_if_data *sdata,
921 void (*iter)(struct ieee80211_hw *hw,
922 struct ieee80211_vif *vif,
923 struct ieee80211_sta *sta,
924 struct ieee80211_key_conf *key,
925 void *data),
926 void *iter_data)
927{
928 struct ieee80211_key *key;
929
930 list_for_each_entry_rcu(key, &sdata->key_list, list) {
931 /* skip keys of station in removal process */
932 if (key->sta && key->sta->removed)
933 continue;
934 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
935 continue;
936
937 iter(hw, &sdata->vif,
938 key->sta ? &key->sta->sta : NULL,
939 &key->conf, iter_data);
940 }
941}
942
943void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
944 struct ieee80211_vif *vif,
945 void (*iter)(struct ieee80211_hw *hw,
946 struct ieee80211_vif *vif,
947 struct ieee80211_sta *sta,
948 struct ieee80211_key_conf *key,
949 void *data),
950 void *iter_data)
951{
952 struct ieee80211_local *local = hw_to_local(hw);
953 struct ieee80211_sub_if_data *sdata;
954
955 if (vif) {
956 sdata = vif_to_sdata(vif);
957 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
958 } else {
959 list_for_each_entry_rcu(sdata, &local->interfaces, list)
960 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
961 }
962}
963EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
964
Johannes Berg7907c7d2013-12-04 23:47:09 +0100965static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
966 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400967{
968 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200969
Michal Kaziorf9dca802015-05-13 09:16:48 +0000970 decrease_tailroom_need_count(sdata,
971 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100972 sdata->crypto_tx_tailroom_pending_dec = 0;
973
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200974 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400975
Johannes Berg6d10e462013-03-06 23:09:11 +0100976 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
977 ieee80211_key_replace(key->sdata, key->sta,
978 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
979 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100980 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100981 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400982
Johannes Bergf7e01042010-12-09 19:49:02 +0100983 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100984}
Johannes Bergf7e01042010-12-09 19:49:02 +0100985
Johannes Berg7907c7d2013-12-04 23:47:09 +0100986void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
987 bool force_synchronize)
988{
989 struct ieee80211_local *local = sdata->local;
990 struct ieee80211_sub_if_data *vlan;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000991 struct ieee80211_sub_if_data *master;
Johannes Berg7907c7d2013-12-04 23:47:09 +0100992 struct ieee80211_key *key, *tmp;
993 LIST_HEAD(keys);
994
995 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
996
997 mutex_lock(&local->key_mtx);
998
999 ieee80211_free_keys_iface(sdata, &keys);
1000
1001 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1002 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1003 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +01001004 }
1005
Johannes Berg7907c7d2013-12-04 23:47:09 +01001006 if (!list_empty(&keys) || force_synchronize)
1007 synchronize_net();
1008 list_for_each_entry_safe(key, tmp, &keys, list)
1009 __ieee80211_key_destroy(key, false);
1010
Michal Kaziorf9dca802015-05-13 09:16:48 +00001011 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012 if (sdata->bss) {
1013 master = container_of(sdata->bss,
1014 struct ieee80211_sub_if_data,
1015 u.ap);
1016
1017 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1018 master->crypto_tx_tailroom_needed_cnt);
1019 }
1020 } else {
1021 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1022 sdata->crypto_tx_tailroom_pending_dec);
1023 }
1024
Johannes Berg7907c7d2013-12-04 23:47:09 +01001025 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1026 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1027 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1028 vlan->crypto_tx_tailroom_pending_dec);
1029 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001030
Johannes Berg7907c7d2013-12-04 23:47:09 +01001031 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -04001032}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001033
Johannes Berg6d10e462013-03-06 23:09:11 +01001034void ieee80211_free_sta_keys(struct ieee80211_local *local,
1035 struct sta_info *sta)
1036{
Johannes Bergc8782072013-12-04 23:05:45 +01001037 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +01001038 int i;
1039
1040 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +01001041 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +01001042 key = key_mtx_dereference(local, sta->gtk[i]);
1043 if (!key)
1044 continue;
1045 ieee80211_key_replace(key->sdata, key->sta,
1046 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1047 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301048 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1049 NL80211_IFTYPE_STATION);
Johannes Berg6d10e462013-03-06 23:09:11 +01001050 }
1051
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001052 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1053 key = key_mtx_dereference(local, sta->ptk[i]);
1054 if (!key)
1055 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +01001056 ieee80211_key_replace(key->sdata, key->sta,
1057 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1058 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301059 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1060 NL80211_IFTYPE_STATION);
Johannes Bergc8782072013-12-04 23:05:45 +01001061 }
Johannes Berg6d10e462013-03-06 23:09:11 +01001062
1063 mutex_unlock(&local->key_mtx);
1064}
1065
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001066void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1067{
1068 struct ieee80211_sub_if_data *sdata;
1069
1070 sdata = container_of(wk, struct ieee80211_sub_if_data,
1071 dec_tailroom_needed_wk.work);
1072
1073 /*
1074 * The reason for the delayed tailroom needed decrementing is to
1075 * make roaming faster: during roaming, all keys are first deleted
1076 * and then new keys are installed. The first new key causes the
1077 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1078 * the cost of synchronize_net() (which can be slow). Avoid this
1079 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1080 * key removal for a while, so if we roam the value is larger than
1081 * zero and no 0->1 transition happens.
1082 *
1083 * The cost is that if the AP switching was from an AP with keys
1084 * to one without, we still allocate tailroom while it would no
1085 * longer be needed. However, in the typical (fast) roaming case
1086 * within an ESS this usually won't happen.
1087 */
1088
1089 mutex_lock(&sdata->local->key_mtx);
Michal Kaziorf9dca802015-05-13 09:16:48 +00001090 decrease_tailroom_need_count(sdata,
1091 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001092 sdata->crypto_tx_tailroom_pending_dec = 0;
1093 mutex_unlock(&sdata->local->key_mtx);
1094}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001095
1096void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1097 const u8 *replay_ctr, gfp_t gfp)
1098{
1099 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1100
1101 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1102
1103 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1104}
1105EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001106
Johannes Berg3ea542d2011-07-07 18:58:00 +02001107void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1108 int tid, struct ieee80211_key_seq *seq)
1109{
1110 struct ieee80211_key *key;
1111 const u8 *pn;
1112
1113 key = container_of(keyconf, struct ieee80211_key, conf);
1114
1115 switch (key->conf.cipher) {
1116 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +01001117 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001118 return;
1119 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1120 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1121 break;
1122 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001123 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +01001124 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001125 return;
1126 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +01001127 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +02001128 else
1129 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +02001130 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001131 break;
1132 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001133 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +02001134 if (WARN_ON(tid != 0))
1135 return;
1136 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001137 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001138 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001139 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1140 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1141 if (WARN_ON(tid != 0))
1142 return;
1143 pn = key->u.aes_gmac.rx_pn;
1144 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1145 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001146 case WLAN_CIPHER_SUITE_GCMP:
1147 case WLAN_CIPHER_SUITE_GCMP_256:
1148 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1149 return;
1150 if (tid < 0)
1151 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1152 else
1153 pn = key->u.gcmp.rx_pn[tid];
1154 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1155 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +02001156 }
1157}
1158EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001159
Johannes Berg27b3eb92013-08-07 20:11:55 +02001160void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1161 int tid, struct ieee80211_key_seq *seq)
1162{
1163 struct ieee80211_key *key;
1164 u8 *pn;
1165
1166 key = container_of(keyconf, struct ieee80211_key, conf);
1167
1168 switch (key->conf.cipher) {
1169 case WLAN_CIPHER_SUITE_TKIP:
1170 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1171 return;
1172 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1173 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1174 break;
1175 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001176 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001177 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1178 return;
1179 if (tid < 0)
1180 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1181 else
1182 pn = key->u.ccmp.rx_pn[tid];
1183 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1184 break;
1185 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001186 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001187 if (WARN_ON(tid != 0))
1188 return;
1189 pn = key->u.aes_cmac.rx_pn;
1190 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1191 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001192 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1193 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1194 if (WARN_ON(tid != 0))
1195 return;
1196 pn = key->u.aes_gmac.rx_pn;
1197 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1198 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001199 case WLAN_CIPHER_SUITE_GCMP:
1200 case WLAN_CIPHER_SUITE_GCMP_256:
1201 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1202 return;
1203 if (tid < 0)
1204 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1205 else
1206 pn = key->u.gcmp.rx_pn[tid];
1207 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1208 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001209 default:
1210 WARN_ON(1);
1211 break;
1212 }
1213}
1214EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1215
1216void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1217{
1218 struct ieee80211_key *key;
1219
1220 key = container_of(keyconf, struct ieee80211_key, conf);
1221
1222 assert_key_lock(key->local);
1223
1224 /*
1225 * if key was uploaded, we assume the driver will/has remove(d)
1226 * it, so adjust bookkeeping accordingly
1227 */
1228 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1229 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1230
Alexander Wetzel092c4092019-03-16 21:44:43 +01001231 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1232 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1233 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001234 increment_tailroom_need_count(key->sdata);
1235 }
1236
1237 ieee80211_key_free(key, false);
1238}
1239EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1240
1241struct ieee80211_key_conf *
1242ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1243 struct ieee80211_key_conf *keyconf)
1244{
1245 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1246 struct ieee80211_local *local = sdata->local;
1247 struct ieee80211_key *key;
1248 int err;
1249
1250 if (WARN_ON(!local->wowlan))
1251 return ERR_PTR(-EINVAL);
1252
1253 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1254 return ERR_PTR(-EINVAL);
1255
1256 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1257 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001258 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001259 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001260 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001261
1262 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1263 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1264
1265 err = ieee80211_key_link(key, sdata, NULL);
1266 if (err)
1267 return ERR_PTR(err);
1268
1269 return &key->conf;
1270}
1271EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);