blob: 42d52cded4c12875a3515bd0764e19acd9f0e974 [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 */
176 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
177 goto out_unsupported;
Helmut Schaa18890d42010-11-19 08:11:01 +0100178 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400179
Johannes Berg89c91ca2012-01-20 13:55:19 +0100180 ret = drv_set_key(key->local, SET_KEY, sdata,
181 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400182
Johannes Berge31b8212010-10-05 19:39:30 +0200183 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400184 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530185
Alexander Wetzel092c4092019-03-16 21:44:43 +0100186 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
187 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
188 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Michal Kaziorf9dca802015-05-13 09:16:48 +0000189 decrease_tailroom_need_count(sdata, 1);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530190
Arik Nemtsov077a9152011-10-23 08:21:41 +0200191 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
192 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
193
David Spinadel9de18d82017-12-01 13:50:52 +0200194 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) &&
195 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC));
196
Johannes Berge31b8212010-10-05 19:39:30 +0200197 return 0;
198 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400199
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100200 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200201 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700202 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100203 key->conf.keyidx,
204 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300205
Johannes Berge31b8212010-10-05 19:39:30 +0200206 out_unsupported:
207 switch (key->conf.cipher) {
208 case WLAN_CIPHER_SUITE_WEP40:
209 case WLAN_CIPHER_SUITE_WEP104:
210 case WLAN_CIPHER_SUITE_TKIP:
211 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200212 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200213 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200214 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200215 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
216 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200217 case WLAN_CIPHER_SUITE_GCMP:
218 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100219 /* all of these we can do in software - if driver can */
220 if (ret == 1)
221 return 0;
Manikanta Pubbisettydb3bdcb2018-03-28 18:34:19 +0530222 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL)) {
223 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
224 return 0;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100225 return -EINVAL;
Manikanta Pubbisettydb3bdcb2018-03-28 18:34:19 +0530226 }
Johannes Berge31b8212010-10-05 19:39:30 +0200227 return 0;
228 default:
229 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300230 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400231}
232
233static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
234{
Johannes Bergdc822b52008-12-29 12:55:09 +0100235 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100236 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400237 int ret;
238
Johannes Berg3b967662008-04-08 17:56:52 +0200239 might_sleep();
240
Johannes Bergdb4d1162008-02-25 16:27:45 +0100241 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400242 return;
243
Johannes Bergad0e2b52010-06-01 10:19:19 +0200244 assert_key_lock(key->local);
245
246 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400247 return;
248
Johannes Berg89c91ca2012-01-20 13:55:19 +0100249 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100250 sdata = key->sdata;
251
Alexander Wetzel092c4092019-03-16 21:44:43 +0100252 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
253 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
254 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530255 increment_tailroom_need_count(sdata);
256
Alexander Wetzel62872a92018-08-31 15:00:38 +0200257 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg12375ef2009-11-25 20:30:31 +0100258 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100259 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400260
261 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200262 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700263 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100264 key->conf.keyidx,
265 sta ? sta->sta.addr : bcast_addr, ret);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200266}
Johannes Berg11a843b2007-08-28 17:01:55 -0400267
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100268int ieee80211_set_tx_key(struct ieee80211_key *key)
269{
270 struct sta_info *sta = key->sta;
271 struct ieee80211_local *local = key->local;
272 struct ieee80211_key *old;
273
274 assert_key_lock(local);
275
276 old = key_mtx_dereference(local, sta->ptk[sta->ptk_idx]);
277 sta->ptk_idx = key->conf.keyidx;
278 ieee80211_check_fast_xmit(sta);
279
280 return 0;
281}
282
Alexander Wetzel62872a92018-08-31 15:00:38 +0200283static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
284 struct ieee80211_key *new_key,
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100285 bool pairwise)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200286{
287 struct ieee80211_sub_if_data *sdata;
288 struct ieee80211_local *local;
289 struct sta_info *sta;
290 int ret;
291
292 /* Aggregation sessions are OK when running on SW crypto.
293 * A broken remote STA may cause issues not observed with HW
294 * crypto, though.
295 */
296 if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
297 return 0;
298
299 assert_key_lock(old_key->local);
300 sta = old_key->sta;
301
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100302 /* Unicast rekey without Extended Key ID needs special handling */
303 if (new_key && sta && pairwise &&
304 rcu_access_pointer(sta->ptk[sta->ptk_idx]) == old_key) {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200305 local = old_key->local;
306 sdata = old_key->sdata;
307
308 /* Stop TX till we are on the new key */
309 old_key->flags |= KEY_FLAG_TAINTED;
310 ieee80211_clear_fast_xmit(sta);
311
312 /* Aggregation sessions during rekey are complicated due to the
313 * reorder buffer and retransmits. Side step that by blocking
314 * aggregation during rekey and tear down running sessions.
315 */
316 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
317 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
318 ieee80211_sta_tear_down_BA_sessions(sta,
319 AGG_STOP_LOCAL_REQUEST);
320 }
321
322 if (!wiphy_ext_feature_isset(local->hw.wiphy,
323 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
324 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
325 sta->sta.addr);
326 /* Flushing the driver queues *may* help prevent
327 * the clear text leaks and freezes.
328 */
329 ieee80211_flush_queues(local, sdata, false);
330 }
331 }
332
333 ieee80211_key_disable_hw_accel(old_key);
334
335 if (new_key)
336 ret = ieee80211_key_enable_hw_accel(new_key);
337 else
338 ret = 0;
339
340 return ret;
Johannes Berg3b967662008-04-08 17:56:52 +0200341}
342
343static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100344 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200345{
346 struct ieee80211_key *key = NULL;
347
Johannes Bergad0e2b52010-06-01 10:19:19 +0200348 assert_key_lock(sdata->local);
349
Johannes Berg3b967662008-04-08 17:56:52 +0200350 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200351 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200352
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300353 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100354 rcu_assign_pointer(sdata->default_unicast_key, key);
Johannes Berg17c18bf2015-03-21 15:25:43 +0100355 ieee80211_check_fast_xmit_iface(sdata);
Johannes Bergec4efc42016-12-13 09:39:18 +0100356 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
357 drv_set_default_unicast_key(sdata->local, sdata, idx);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300358 }
359
Johannes Bergf7e01042010-12-09 19:49:02 +0100360 if (multi)
361 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200362
Johannes Bergf7e01042010-12-09 19:49:02 +0100363 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200364}
365
Johannes Bergf7e01042010-12-09 19:49:02 +0100366void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
367 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200368{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200369 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100370 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200371 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200372}
373
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200374static void
375__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
376{
377 struct ieee80211_key *key = NULL;
378
Johannes Bergad0e2b52010-06-01 10:19:19 +0200379 assert_key_lock(sdata->local);
380
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200381 if (idx >= NUM_DEFAULT_KEYS &&
382 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200383 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200384
385 rcu_assign_pointer(sdata->default_mgmt_key, key);
386
Johannes Bergf7e01042010-12-09 19:49:02 +0100387 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200388}
389
390void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
391 int idx)
392{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200393 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200394 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200395 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200396}
397
Johannes Berg3b967662008-04-08 17:56:52 +0200398
Alexander Wetzel62872a92018-08-31 15:00:38 +0200399static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100400 struct sta_info *sta,
401 bool pairwise,
402 struct ieee80211_key *old,
403 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200404{
Johannes Bergf7e01042010-12-09 19:49:02 +0100405 int idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200406 int ret;
Johannes Bergf7e01042010-12-09 19:49:02 +0100407 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200408
Johannes Berg5282c3b2013-10-29 10:00:08 +0100409 /* caller must provide at least one old/new */
410 if (WARN_ON(!new && !old))
Alexander Wetzel62872a92018-08-31 15:00:38 +0200411 return 0;
Johannes Berg5282c3b2013-10-29 10:00:08 +0100412
Johannes Berg3b967662008-04-08 17:56:52 +0200413 if (new)
Eliad Pelleref044762015-11-17 10:24:37 +0200414 list_add_tail_rcu(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200415
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200416 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
417
Alexander Wetzel62872a92018-08-31 15:00:38 +0200418 if (old) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200419 idx = old->conf.keyidx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200420 ret = ieee80211_hw_key_replace(old, new, pairwise);
421 } else {
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500422 /* new must be provided in case old is not */
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200423 idx = new->conf.keyidx;
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500424 if (!new->local->wowlan)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200425 ret = ieee80211_key_enable_hw_accel(new);
426 else
427 ret = 0;
428 }
429
430 if (ret)
431 return ret;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200432
433 if (sta) {
434 if (pairwise) {
435 rcu_assign_pointer(sta->ptk[idx], new);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100436 if (new &&
437 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) {
438 sta->ptk_idx = idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200439 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
440 ieee80211_check_fast_xmit(sta);
441 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200442 } else {
443 rcu_assign_pointer(sta->gtk[idx], new);
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200444 }
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100445 /* Only needed for transition from no key -> key.
446 * Still triggers unnecessary when using Extended Key ID
447 * and installing the second key ID the first time.
448 */
449 if (new && !old)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200450 ieee80211_check_fast_rx(sta);
Johannes Berg3b967662008-04-08 17:56:52 +0200451 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200452 defunikey = old &&
453 old == key_mtx_dereference(sdata->local,
454 sdata->default_unicast_key);
455 defmultikey = old &&
456 old == key_mtx_dereference(sdata->local,
457 sdata->default_multicast_key);
458 defmgmtkey = old &&
459 old == key_mtx_dereference(sdata->local,
460 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200461
Johannes Bergf7e01042010-12-09 19:49:02 +0100462 if (defunikey && !new)
463 __ieee80211_set_default_key(sdata, -1, true, false);
464 if (defmultikey && !new)
465 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200466 if (defmgmtkey && !new)
467 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200468
469 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100470 if (defunikey && new)
471 __ieee80211_set_default_key(sdata, new->conf.keyidx,
472 true, false);
473 if (defmultikey && new)
474 __ieee80211_set_default_key(sdata, new->conf.keyidx,
475 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200476 if (defmgmtkey && new)
477 __ieee80211_set_default_mgmt_key(sdata,
478 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200479 }
480
Johannes Bergb5c34f62011-01-03 19:51:09 +0100481 if (old)
Eliad Pelleref044762015-11-17 10:24:37 +0200482 list_del_rcu(&old->list);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200483
484 return 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400485}
486
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200487struct ieee80211_key *
488ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
489 const u8 *key_data,
490 size_t seq_len, const u8 *seq,
491 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200492{
493 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100494 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200495
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200496 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
497 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400498
499 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200500 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100501 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400502
503 /*
504 * Default to software encryption; we'll later upload the
505 * key to the hardware if possible.
506 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400507 key->conf.flags = 0;
508 key->flags = 0;
509
Johannes Berg97359d12010-08-10 09:46:38 +0200510 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400511 key->conf.keyidx = idx;
512 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200513 switch (cipher) {
514 case WLAN_CIPHER_SUITE_WEP40:
515 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200516 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
517 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200518 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200519 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200520 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
521 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300522 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100523 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300524 key->u.tkip.rx[i].iv32 =
525 get_unaligned_le32(&seq[2]);
526 key->u.tkip.rx[i].iv16 =
527 get_unaligned_le16(seq);
528 }
529 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200530 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200531 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200532 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200533 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
534 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300535 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100536 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200537 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300538 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200539 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300540 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400541 /*
542 * Initialize AES key state here as an optimization so that
543 * it does not need to be initialized for every packet.
544 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200545 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
546 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
547 if (IS_ERR(key->u.ccmp.tfm)) {
548 err = PTR_ERR(key->u.ccmp.tfm);
549 kfree(key);
550 return ERR_PTR(err);
551 }
552 break;
553 case WLAN_CIPHER_SUITE_CCMP_256:
554 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
555 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
556 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
557 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
558 key->u.ccmp.rx_pn[i][j] =
559 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
560 /* Initialize AES key state here as an optimization so that
561 * it does not need to be initialized for every packet.
562 */
563 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
564 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100565 if (IS_ERR(key->u.ccmp.tfm)) {
566 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200567 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200568 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400569 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200570 break;
571 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200572 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200573 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200574 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
575 key->conf.icv_len = sizeof(struct ieee80211_mmie);
576 else
577 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200578 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200579 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100580 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200581 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200582 /*
583 * Initialize AES key state here as an optimization so that
584 * it does not need to be initialized for every packet.
585 */
586 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200587 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100588 if (IS_ERR(key->u.aes_cmac.tfm)) {
589 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200590 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200591 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200592 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200593 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200594 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
595 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
596 key->conf.iv_len = 0;
597 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
598 if (seq)
599 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
600 key->u.aes_gmac.rx_pn[j] =
601 seq[IEEE80211_GMAC_PN_LEN - j - 1];
602 /* Initialize AES key state here as an optimization so that
603 * it does not need to be initialized for every packet.
604 */
605 key->u.aes_gmac.tfm =
606 ieee80211_aes_gmac_key_setup(key_data, key_len);
607 if (IS_ERR(key->u.aes_gmac.tfm)) {
608 err = PTR_ERR(key->u.aes_gmac.tfm);
609 kfree(key);
610 return ERR_PTR(err);
611 }
612 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200613 case WLAN_CIPHER_SUITE_GCMP:
614 case WLAN_CIPHER_SUITE_GCMP_256:
615 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
616 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
617 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
618 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
619 key->u.gcmp.rx_pn[i][j] =
620 seq[IEEE80211_GCMP_PN_LEN - j - 1];
621 /* Initialize AES key state here as an optimization so that
622 * it does not need to be initialized for every packet.
623 */
624 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
625 key_len);
626 if (IS_ERR(key->u.gcmp.tfm)) {
627 err = PTR_ERR(key->u.gcmp.tfm);
628 kfree(key);
629 return ERR_PTR(err);
630 }
631 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200632 default:
633 if (cs) {
Johannes Berge3a55b52015-05-05 16:32:29 +0200634 if (seq_len && seq_len != cs->pn_len) {
635 kfree(key);
636 return ERR_PTR(-EINVAL);
637 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200638
639 key->conf.iv_len = cs->hdr_len;
640 key->conf.icv_len = cs->mic_len;
641 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berge3a55b52015-05-05 16:32:29 +0200642 for (j = 0; j < seq_len; j++)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200643 key->u.gen.rx_pn[i][j] =
Johannes Berge3a55b52015-05-05 16:32:29 +0200644 seq[seq_len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000645 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200646 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200647 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200648 memcpy(key->conf.key, key_data, key_len);
649 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200650
Johannes Bergdb4d1162008-02-25 16:27:45 +0100651 return key;
652}
Johannes Berg11a843b2007-08-28 17:01:55 -0400653
Johannes Berg79cf2df2013-03-06 22:53:52 +0100654static void ieee80211_key_free_common(struct ieee80211_key *key)
655{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200656 switch (key->conf.cipher) {
657 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200658 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100659 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200660 break;
661 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200662 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100663 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200664 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200665 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
666 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
667 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
668 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200669 case WLAN_CIPHER_SUITE_GCMP:
670 case WLAN_CIPHER_SUITE_GCMP_256:
671 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
672 break;
673 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300674 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100675}
676
Johannes Berg6d10e462013-03-06 23:09:11 +0100677static void __ieee80211_key_destroy(struct ieee80211_key *key,
678 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200679{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530680 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100681 struct ieee80211_sub_if_data *sdata = key->sdata;
682
Jouni Malinen32162a42010-07-26 15:52:03 -0700683 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100684
685 if (delay_tailroom) {
686 /* see ieee80211_delayed_tailroom_dec */
687 sdata->crypto_tx_tailroom_pending_dec++;
688 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
689 HZ/2);
690 } else {
Michal Kaziorf9dca802015-05-13 09:16:48 +0000691 decrease_tailroom_need_count(sdata, 1);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100692 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530693 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200694
Johannes Berg79cf2df2013-03-06 22:53:52 +0100695 ieee80211_key_free_common(key);
696}
697
Johannes Berg6d10e462013-03-06 23:09:11 +0100698static void ieee80211_key_destroy(struct ieee80211_key *key,
699 bool delay_tailroom)
700{
701 if (!key)
702 return;
703
704 /*
Eliad Pelleref044762015-11-17 10:24:37 +0200705 * Synchronize so the TX path and rcu key iterators
706 * can no longer be using this key before we free/remove it.
Johannes Berg6d10e462013-03-06 23:09:11 +0100707 */
708 synchronize_net();
709
710 __ieee80211_key_destroy(key, delay_tailroom);
711}
712
Johannes Berg79cf2df2013-03-06 22:53:52 +0100713void ieee80211_key_free_unused(struct ieee80211_key *key)
714{
715 WARN_ON(key->sdata || key->local);
716 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200717}
718
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200719static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
720 struct ieee80211_key *old,
721 struct ieee80211_key *new)
722{
723 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
724 u8 *tk_old, *tk_new;
725
726 if (!old || new->conf.keylen != old->conf.keylen)
727 return false;
728
729 tk_old = old->conf.key;
730 tk_new = new->conf.key;
731
732 /*
733 * In station mode, don't compare the TX MIC key, as it's never used
734 * and offloaded rekeying may not care to send it to the host. This
735 * is the case in iwlwifi, for example.
736 */
737 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
738 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
739 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
740 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
741 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
742 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
743 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
744 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
745 tk_old = tkip_old;
746 tk_new = tkip_new;
747 }
748
749 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
750}
751
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300752int ieee80211_key_link(struct ieee80211_key *key,
753 struct ieee80211_sub_if_data *sdata,
754 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100755{
756 struct ieee80211_key *old_key;
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +0530757 int idx = key->conf.keyidx;
758 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
759 /*
760 * We want to delay tailroom updates only for station - in that
761 * case it helps roaming speed, but in other cases it hurts and
762 * can cause warnings to appear.
763 */
764 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100765 int ret = -EOPNOTSUPP;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100766
Johannes Bergad0e2b52010-06-01 10:19:19 +0200767 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200768
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100769 if (sta && pairwise) {
770 struct ieee80211_key *alt_key;
771
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200772 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100773 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]);
774
775 /* The rekey code assumes that the old and new key are using
776 * the same cipher. Enforce the assumption for pairwise keys.
777 */
778 if (key &&
779 ((alt_key && alt_key->conf.cipher != key->conf.cipher) ||
780 (old_key && old_key->conf.cipher != key->conf.cipher)))
781 goto out;
782 } else if (sta) {
Johannes Berg40b275b2011-05-13 14:15:49 +0200783 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100784 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200785 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100786 }
787
788 /* Non-pairwise keys must also not switch the cipher on rekey */
789 if (!pairwise) {
790 if (key && old_key && old_key->conf.cipher != key->conf.cipher)
791 goto out;
792 }
Johannes Bergdb4d1162008-02-25 16:27:45 +0100793
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200794 /*
795 * Silently accept key re-installation without really installing the
796 * new version of the key to avoid nonce reuse or replay issues.
797 */
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200798 if (ieee80211_key_identical(sdata, old_key, key)) {
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200799 ieee80211_key_free_unused(key);
800 ret = 0;
801 goto out;
802 }
803
804 key->local = sdata->local;
805 key->sdata = sdata;
806 key->sta = sta;
807
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530808 increment_tailroom_need_count(sdata);
809
Alexander Wetzel62872a92018-08-31 15:00:38 +0200810 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400811
Alexander Wetzel62872a92018-08-31 15:00:38 +0200812 if (!ret) {
813 ieee80211_debugfs_key_add(key);
814 ieee80211_key_destroy(old_key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200815 } else {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200816 ieee80211_key_free(key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200817 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100818
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200819 out:
Johannes Bergad0e2b52010-06-01 10:19:19 +0200820 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300821
822 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200823}
824
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100825void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200826{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200827 if (!key)
828 return;
829
Johannes Berg3a245762008-04-09 16:45:37 +0200830 /*
831 * Replace key with nothingness if it was ever used.
832 */
833 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100834 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200835 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
836 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100837 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200838}
839
Johannes Berg3b967662008-04-08 17:56:52 +0200840void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
841{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200842 struct ieee80211_key *key;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000843 struct ieee80211_sub_if_data *vlan;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200844
Johannes Berg3a245762008-04-09 16:45:37 +0200845 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200846
Johannes Berg9607e6b662009-12-23 13:15:31 +0100847 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200848 return;
849
Johannes Bergad0e2b52010-06-01 10:19:19 +0200850 mutex_lock(&sdata->local->key_mtx);
851
Michal Kaziorf9dca802015-05-13 09:16:48 +0000852 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
853 sdata->crypto_tx_tailroom_pending_dec);
854
855 if (sdata->vif.type == NL80211_IFTYPE_AP) {
856 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
857 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
858 vlan->crypto_tx_tailroom_pending_dec);
859 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530860
861 list_for_each_entry(key, &sdata->key_list, list) {
862 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200863 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530864 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200865
866 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200867}
868
Michal Kaziorf9dca802015-05-13 09:16:48 +0000869void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
870{
871 struct ieee80211_sub_if_data *vlan;
872
873 mutex_lock(&sdata->local->key_mtx);
874
875 sdata->crypto_tx_tailroom_needed_cnt = 0;
876
877 if (sdata->vif.type == NL80211_IFTYPE_AP) {
878 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
879 vlan->crypto_tx_tailroom_needed_cnt = 0;
880 }
881
882 mutex_unlock(&sdata->local->key_mtx);
883}
884
Johannes Berg830af022011-07-05 16:35:39 +0200885void ieee80211_iter_keys(struct ieee80211_hw *hw,
886 struct ieee80211_vif *vif,
887 void (*iter)(struct ieee80211_hw *hw,
888 struct ieee80211_vif *vif,
889 struct ieee80211_sta *sta,
890 struct ieee80211_key_conf *key,
891 void *data),
892 void *iter_data)
893{
894 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200895 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200896 struct ieee80211_sub_if_data *sdata;
897
898 ASSERT_RTNL();
899
900 mutex_lock(&local->key_mtx);
901 if (vif) {
902 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200903 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200904 iter(hw, &sdata->vif,
905 key->sta ? &key->sta->sta : NULL,
906 &key->conf, iter_data);
907 } else {
908 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200909 list_for_each_entry_safe(key, tmp,
910 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200911 iter(hw, &sdata->vif,
912 key->sta ? &key->sta->sta : NULL,
913 &key->conf, iter_data);
914 }
915 mutex_unlock(&local->key_mtx);
916}
917EXPORT_SYMBOL(ieee80211_iter_keys);
918
Eliad Pelleref044762015-11-17 10:24:37 +0200919static void
920_ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
921 struct ieee80211_sub_if_data *sdata,
922 void (*iter)(struct ieee80211_hw *hw,
923 struct ieee80211_vif *vif,
924 struct ieee80211_sta *sta,
925 struct ieee80211_key_conf *key,
926 void *data),
927 void *iter_data)
928{
929 struct ieee80211_key *key;
930
931 list_for_each_entry_rcu(key, &sdata->key_list, list) {
932 /* skip keys of station in removal process */
933 if (key->sta && key->sta->removed)
934 continue;
935 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
936 continue;
937
938 iter(hw, &sdata->vif,
939 key->sta ? &key->sta->sta : NULL,
940 &key->conf, iter_data);
941 }
942}
943
944void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
945 struct ieee80211_vif *vif,
946 void (*iter)(struct ieee80211_hw *hw,
947 struct ieee80211_vif *vif,
948 struct ieee80211_sta *sta,
949 struct ieee80211_key_conf *key,
950 void *data),
951 void *iter_data)
952{
953 struct ieee80211_local *local = hw_to_local(hw);
954 struct ieee80211_sub_if_data *sdata;
955
956 if (vif) {
957 sdata = vif_to_sdata(vif);
958 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
959 } else {
960 list_for_each_entry_rcu(sdata, &local->interfaces, list)
961 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
962 }
963}
964EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
965
Johannes Berg7907c7d2013-12-04 23:47:09 +0100966static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
967 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400968{
969 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200970
Michal Kaziorf9dca802015-05-13 09:16:48 +0000971 decrease_tailroom_need_count(sdata,
972 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100973 sdata->crypto_tx_tailroom_pending_dec = 0;
974
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200975 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400976
Johannes Berg6d10e462013-03-06 23:09:11 +0100977 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
978 ieee80211_key_replace(key->sdata, key->sta,
979 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
980 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100981 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100982 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400983
Johannes Bergf7e01042010-12-09 19:49:02 +0100984 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100985}
Johannes Bergf7e01042010-12-09 19:49:02 +0100986
Johannes Berg7907c7d2013-12-04 23:47:09 +0100987void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
988 bool force_synchronize)
989{
990 struct ieee80211_local *local = sdata->local;
991 struct ieee80211_sub_if_data *vlan;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000992 struct ieee80211_sub_if_data *master;
Johannes Berg7907c7d2013-12-04 23:47:09 +0100993 struct ieee80211_key *key, *tmp;
994 LIST_HEAD(keys);
995
996 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
997
998 mutex_lock(&local->key_mtx);
999
1000 ieee80211_free_keys_iface(sdata, &keys);
1001
1002 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1003 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1004 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +01001005 }
1006
Johannes Berg7907c7d2013-12-04 23:47:09 +01001007 if (!list_empty(&keys) || force_synchronize)
1008 synchronize_net();
1009 list_for_each_entry_safe(key, tmp, &keys, list)
1010 __ieee80211_key_destroy(key, false);
1011
Michal Kaziorf9dca802015-05-13 09:16:48 +00001012 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1013 if (sdata->bss) {
1014 master = container_of(sdata->bss,
1015 struct ieee80211_sub_if_data,
1016 u.ap);
1017
1018 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
1019 master->crypto_tx_tailroom_needed_cnt);
1020 }
1021 } else {
1022 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
1023 sdata->crypto_tx_tailroom_pending_dec);
1024 }
1025
Johannes Berg7907c7d2013-12-04 23:47:09 +01001026 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1027 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1028 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
1029 vlan->crypto_tx_tailroom_pending_dec);
1030 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001031
Johannes Berg7907c7d2013-12-04 23:47:09 +01001032 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -04001033}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001034
Johannes Berg6d10e462013-03-06 23:09:11 +01001035void ieee80211_free_sta_keys(struct ieee80211_local *local,
1036 struct sta_info *sta)
1037{
Johannes Bergc8782072013-12-04 23:05:45 +01001038 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +01001039 int i;
1040
1041 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +01001042 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +01001043 key = key_mtx_dereference(local, sta->gtk[i]);
1044 if (!key)
1045 continue;
1046 ieee80211_key_replace(key->sdata, key->sta,
1047 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1048 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301049 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1050 NL80211_IFTYPE_STATION);
Johannes Berg6d10e462013-03-06 23:09:11 +01001051 }
1052
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001053 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1054 key = key_mtx_dereference(local, sta->ptk[i]);
1055 if (!key)
1056 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +01001057 ieee80211_key_replace(key->sdata, key->sta,
1058 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1059 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301060 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1061 NL80211_IFTYPE_STATION);
Johannes Bergc8782072013-12-04 23:05:45 +01001062 }
Johannes Berg6d10e462013-03-06 23:09:11 +01001063
1064 mutex_unlock(&local->key_mtx);
1065}
1066
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001067void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1068{
1069 struct ieee80211_sub_if_data *sdata;
1070
1071 sdata = container_of(wk, struct ieee80211_sub_if_data,
1072 dec_tailroom_needed_wk.work);
1073
1074 /*
1075 * The reason for the delayed tailroom needed decrementing is to
1076 * make roaming faster: during roaming, all keys are first deleted
1077 * and then new keys are installed. The first new key causes the
1078 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1079 * the cost of synchronize_net() (which can be slow). Avoid this
1080 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1081 * key removal for a while, so if we roam the value is larger than
1082 * zero and no 0->1 transition happens.
1083 *
1084 * The cost is that if the AP switching was from an AP with keys
1085 * to one without, we still allocate tailroom while it would no
1086 * longer be needed. However, in the typical (fast) roaming case
1087 * within an ESS this usually won't happen.
1088 */
1089
1090 mutex_lock(&sdata->local->key_mtx);
Michal Kaziorf9dca802015-05-13 09:16:48 +00001091 decrease_tailroom_need_count(sdata,
1092 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001093 sdata->crypto_tx_tailroom_pending_dec = 0;
1094 mutex_unlock(&sdata->local->key_mtx);
1095}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001096
1097void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1098 const u8 *replay_ctr, gfp_t gfp)
1099{
1100 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1101
1102 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1103
1104 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1105}
1106EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001107
Johannes Berg3ea542d2011-07-07 18:58:00 +02001108void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1109 int tid, struct ieee80211_key_seq *seq)
1110{
1111 struct ieee80211_key *key;
1112 const u8 *pn;
1113
1114 key = container_of(keyconf, struct ieee80211_key, conf);
1115
1116 switch (key->conf.cipher) {
1117 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +01001118 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001119 return;
1120 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1121 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1122 break;
1123 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001124 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +01001125 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001126 return;
1127 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +01001128 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +02001129 else
1130 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +02001131 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001132 break;
1133 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001134 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +02001135 if (WARN_ON(tid != 0))
1136 return;
1137 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001138 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001139 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001140 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1141 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1142 if (WARN_ON(tid != 0))
1143 return;
1144 pn = key->u.aes_gmac.rx_pn;
1145 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1146 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001147 case WLAN_CIPHER_SUITE_GCMP:
1148 case WLAN_CIPHER_SUITE_GCMP_256:
1149 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1150 return;
1151 if (tid < 0)
1152 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1153 else
1154 pn = key->u.gcmp.rx_pn[tid];
1155 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1156 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +02001157 }
1158}
1159EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001160
Johannes Berg27b3eb92013-08-07 20:11:55 +02001161void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1162 int tid, struct ieee80211_key_seq *seq)
1163{
1164 struct ieee80211_key *key;
1165 u8 *pn;
1166
1167 key = container_of(keyconf, struct ieee80211_key, conf);
1168
1169 switch (key->conf.cipher) {
1170 case WLAN_CIPHER_SUITE_TKIP:
1171 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1172 return;
1173 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1174 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1175 break;
1176 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001177 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001178 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1179 return;
1180 if (tid < 0)
1181 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1182 else
1183 pn = key->u.ccmp.rx_pn[tid];
1184 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1185 break;
1186 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001187 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001188 if (WARN_ON(tid != 0))
1189 return;
1190 pn = key->u.aes_cmac.rx_pn;
1191 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1192 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001193 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1194 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1195 if (WARN_ON(tid != 0))
1196 return;
1197 pn = key->u.aes_gmac.rx_pn;
1198 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1199 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001200 case WLAN_CIPHER_SUITE_GCMP:
1201 case WLAN_CIPHER_SUITE_GCMP_256:
1202 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1203 return;
1204 if (tid < 0)
1205 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1206 else
1207 pn = key->u.gcmp.rx_pn[tid];
1208 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1209 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001210 default:
1211 WARN_ON(1);
1212 break;
1213 }
1214}
1215EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1216
1217void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1218{
1219 struct ieee80211_key *key;
1220
1221 key = container_of(keyconf, struct ieee80211_key, conf);
1222
1223 assert_key_lock(key->local);
1224
1225 /*
1226 * if key was uploaded, we assume the driver will/has remove(d)
1227 * it, so adjust bookkeeping accordingly
1228 */
1229 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1230 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1231
Alexander Wetzel092c4092019-03-16 21:44:43 +01001232 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1233 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1234 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001235 increment_tailroom_need_count(key->sdata);
1236 }
1237
1238 ieee80211_key_free(key, false);
1239}
1240EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1241
1242struct ieee80211_key_conf *
1243ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1244 struct ieee80211_key_conf *keyconf)
1245{
1246 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1247 struct ieee80211_local *local = sdata->local;
1248 struct ieee80211_key *key;
1249 int err;
1250
1251 if (WARN_ON(!local->wowlan))
1252 return ERR_PTR(-EINVAL);
1253
1254 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1255 return ERR_PTR(-EINVAL);
1256
1257 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1258 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001259 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001260 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001261 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001262
1263 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1264 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1265
1266 err = ieee80211_key_link(key, sdata, NULL);
1267 if (err)
1268 return ERR_PTR(err);
1269
1270 return &key->conf;
1271}
1272EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);