blob: 41b8db37c7c10ac58876d21ef7eddfa315e47787 [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 Wetzel62872a92018-08-31 15:00:38 +0200268static int ieee80211_hw_key_replace(struct ieee80211_key *old_key,
269 struct ieee80211_key *new_key,
270 bool ptk0rekey)
271{
272 struct ieee80211_sub_if_data *sdata;
273 struct ieee80211_local *local;
274 struct sta_info *sta;
275 int ret;
276
277 /* Aggregation sessions are OK when running on SW crypto.
278 * A broken remote STA may cause issues not observed with HW
279 * crypto, though.
280 */
281 if (!(old_key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
282 return 0;
283
284 assert_key_lock(old_key->local);
285 sta = old_key->sta;
286
287 /* PTK only using key ID 0 needs special handling on rekey */
288 if (new_key && sta && ptk0rekey) {
289 local = old_key->local;
290 sdata = old_key->sdata;
291
292 /* Stop TX till we are on the new key */
293 old_key->flags |= KEY_FLAG_TAINTED;
294 ieee80211_clear_fast_xmit(sta);
295
296 /* Aggregation sessions during rekey are complicated due to the
297 * reorder buffer and retransmits. Side step that by blocking
298 * aggregation during rekey and tear down running sessions.
299 */
300 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
301 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
302 ieee80211_sta_tear_down_BA_sessions(sta,
303 AGG_STOP_LOCAL_REQUEST);
304 }
305
306 if (!wiphy_ext_feature_isset(local->hw.wiphy,
307 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) {
308 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.",
309 sta->sta.addr);
310 /* Flushing the driver queues *may* help prevent
311 * the clear text leaks and freezes.
312 */
313 ieee80211_flush_queues(local, sdata, false);
314 }
315 }
316
317 ieee80211_key_disable_hw_accel(old_key);
318
319 if (new_key)
320 ret = ieee80211_key_enable_hw_accel(new_key);
321 else
322 ret = 0;
323
324 return ret;
Johannes Berg3b967662008-04-08 17:56:52 +0200325}
326
327static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100328 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200329{
330 struct ieee80211_key *key = NULL;
331
Johannes Bergad0e2b52010-06-01 10:19:19 +0200332 assert_key_lock(sdata->local);
333
Johannes Berg3b967662008-04-08 17:56:52 +0200334 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200335 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200336
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300337 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100338 rcu_assign_pointer(sdata->default_unicast_key, key);
Johannes Berg17c18bf2015-03-21 15:25:43 +0100339 ieee80211_check_fast_xmit_iface(sdata);
Johannes Bergec4efc42016-12-13 09:39:18 +0100340 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
341 drv_set_default_unicast_key(sdata->local, sdata, idx);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300342 }
343
Johannes Bergf7e01042010-12-09 19:49:02 +0100344 if (multi)
345 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200346
Johannes Bergf7e01042010-12-09 19:49:02 +0100347 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200348}
349
Johannes Bergf7e01042010-12-09 19:49:02 +0100350void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
351 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200352{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200353 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100354 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200355 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200356}
357
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200358static void
359__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
360{
361 struct ieee80211_key *key = NULL;
362
Johannes Bergad0e2b52010-06-01 10:19:19 +0200363 assert_key_lock(sdata->local);
364
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200365 if (idx >= NUM_DEFAULT_KEYS &&
366 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200367 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200368
369 rcu_assign_pointer(sdata->default_mgmt_key, key);
370
Johannes Bergf7e01042010-12-09 19:49:02 +0100371 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200372}
373
374void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
375 int idx)
376{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200377 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200378 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200379 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200380}
381
Johannes Berg3b967662008-04-08 17:56:52 +0200382
Alexander Wetzel62872a92018-08-31 15:00:38 +0200383static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100384 struct sta_info *sta,
385 bool pairwise,
386 struct ieee80211_key *old,
387 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200388{
Johannes Bergf7e01042010-12-09 19:49:02 +0100389 int idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200390 int ret;
Johannes Bergf7e01042010-12-09 19:49:02 +0100391 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200392
Johannes Berg5282c3b2013-10-29 10:00:08 +0100393 /* caller must provide at least one old/new */
394 if (WARN_ON(!new && !old))
Alexander Wetzel62872a92018-08-31 15:00:38 +0200395 return 0;
Johannes Berg5282c3b2013-10-29 10:00:08 +0100396
Johannes Berg3b967662008-04-08 17:56:52 +0200397 if (new)
Eliad Pelleref044762015-11-17 10:24:37 +0200398 list_add_tail_rcu(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200399
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200400 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
401
Alexander Wetzel62872a92018-08-31 15:00:38 +0200402 if (old) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200403 idx = old->conf.keyidx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200404 /* TODO: proper implement and test "Extended Key ID for
405 * Individually Addressed Frames" from IEEE 802.11-2016.
406 * Till then always assume only key ID 0 is used for
407 * pairwise keys.*/
408 ret = ieee80211_hw_key_replace(old, new, pairwise);
409 } else {
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500410 /* new must be provided in case old is not */
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200411 idx = new->conf.keyidx;
Gustavo A. R. Silva40b5a0f2018-09-04 08:20:01 -0500412 if (!new->local->wowlan)
Alexander Wetzel62872a92018-08-31 15:00:38 +0200413 ret = ieee80211_key_enable_hw_accel(new);
414 else
415 ret = 0;
416 }
417
418 if (ret)
419 return ret;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200420
421 if (sta) {
422 if (pairwise) {
423 rcu_assign_pointer(sta->ptk[idx], new);
424 sta->ptk_idx = idx;
Alexander Wetzel62872a92018-08-31 15:00:38 +0200425 if (new) {
426 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
427 ieee80211_check_fast_xmit(sta);
428 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200429 } else {
430 rcu_assign_pointer(sta->gtk[idx], new);
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200431 }
Alexander Wetzel62872a92018-08-31 15:00:38 +0200432 if (new)
433 ieee80211_check_fast_rx(sta);
Johannes Berg3b967662008-04-08 17:56:52 +0200434 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200435 defunikey = old &&
436 old == key_mtx_dereference(sdata->local,
437 sdata->default_unicast_key);
438 defmultikey = old &&
439 old == key_mtx_dereference(sdata->local,
440 sdata->default_multicast_key);
441 defmgmtkey = old &&
442 old == key_mtx_dereference(sdata->local,
443 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200444
Johannes Bergf7e01042010-12-09 19:49:02 +0100445 if (defunikey && !new)
446 __ieee80211_set_default_key(sdata, -1, true, false);
447 if (defmultikey && !new)
448 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200449 if (defmgmtkey && !new)
450 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200451
452 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100453 if (defunikey && new)
454 __ieee80211_set_default_key(sdata, new->conf.keyidx,
455 true, false);
456 if (defmultikey && new)
457 __ieee80211_set_default_key(sdata, new->conf.keyidx,
458 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200459 if (defmgmtkey && new)
460 __ieee80211_set_default_mgmt_key(sdata,
461 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200462 }
463
Johannes Bergb5c34f62011-01-03 19:51:09 +0100464 if (old)
Eliad Pelleref044762015-11-17 10:24:37 +0200465 list_del_rcu(&old->list);
Alexander Wetzel62872a92018-08-31 15:00:38 +0200466
467 return 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400468}
469
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200470struct ieee80211_key *
471ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
472 const u8 *key_data,
473 size_t seq_len, const u8 *seq,
474 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200475{
476 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100477 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200478
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200479 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
480 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400481
482 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200483 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100484 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400485
486 /*
487 * Default to software encryption; we'll later upload the
488 * key to the hardware if possible.
489 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400490 key->conf.flags = 0;
491 key->flags = 0;
492
Johannes Berg97359d12010-08-10 09:46:38 +0200493 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400494 key->conf.keyidx = idx;
495 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200496 switch (cipher) {
497 case WLAN_CIPHER_SUITE_WEP40:
498 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200499 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
500 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200501 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200502 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200503 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
504 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300505 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100506 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300507 key->u.tkip.rx[i].iv32 =
508 get_unaligned_le32(&seq[2]);
509 key->u.tkip.rx[i].iv16 =
510 get_unaligned_le16(seq);
511 }
512 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200513 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200514 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200515 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200516 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
517 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300518 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100519 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200520 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300521 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200522 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300523 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400524 /*
525 * Initialize AES key state here as an optimization so that
526 * it does not need to be initialized for every packet.
527 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200528 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
529 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
530 if (IS_ERR(key->u.ccmp.tfm)) {
531 err = PTR_ERR(key->u.ccmp.tfm);
532 kfree(key);
533 return ERR_PTR(err);
534 }
535 break;
536 case WLAN_CIPHER_SUITE_CCMP_256:
537 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
538 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
539 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
540 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
541 key->u.ccmp.rx_pn[i][j] =
542 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
543 /* Initialize AES key state here as an optimization so that
544 * it does not need to be initialized for every packet.
545 */
546 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
547 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100548 if (IS_ERR(key->u.ccmp.tfm)) {
549 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200550 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200551 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400552 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200553 break;
554 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200555 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200556 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200557 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
558 key->conf.icv_len = sizeof(struct ieee80211_mmie);
559 else
560 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200561 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200562 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100563 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200564 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200565 /*
566 * Initialize AES key state here as an optimization so that
567 * it does not need to be initialized for every packet.
568 */
569 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200570 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100571 if (IS_ERR(key->u.aes_cmac.tfm)) {
572 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200573 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200574 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200575 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200576 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200577 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
578 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
579 key->conf.iv_len = 0;
580 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
581 if (seq)
582 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
583 key->u.aes_gmac.rx_pn[j] =
584 seq[IEEE80211_GMAC_PN_LEN - j - 1];
585 /* Initialize AES key state here as an optimization so that
586 * it does not need to be initialized for every packet.
587 */
588 key->u.aes_gmac.tfm =
589 ieee80211_aes_gmac_key_setup(key_data, key_len);
590 if (IS_ERR(key->u.aes_gmac.tfm)) {
591 err = PTR_ERR(key->u.aes_gmac.tfm);
592 kfree(key);
593 return ERR_PTR(err);
594 }
595 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200596 case WLAN_CIPHER_SUITE_GCMP:
597 case WLAN_CIPHER_SUITE_GCMP_256:
598 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
599 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
600 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
601 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
602 key->u.gcmp.rx_pn[i][j] =
603 seq[IEEE80211_GCMP_PN_LEN - j - 1];
604 /* Initialize AES key state here as an optimization so that
605 * it does not need to be initialized for every packet.
606 */
607 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
608 key_len);
609 if (IS_ERR(key->u.gcmp.tfm)) {
610 err = PTR_ERR(key->u.gcmp.tfm);
611 kfree(key);
612 return ERR_PTR(err);
613 }
614 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200615 default:
616 if (cs) {
Johannes Berge3a55b52015-05-05 16:32:29 +0200617 if (seq_len && seq_len != cs->pn_len) {
618 kfree(key);
619 return ERR_PTR(-EINVAL);
620 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200621
622 key->conf.iv_len = cs->hdr_len;
623 key->conf.icv_len = cs->mic_len;
624 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berge3a55b52015-05-05 16:32:29 +0200625 for (j = 0; j < seq_len; j++)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200626 key->u.gen.rx_pn[i][j] =
Johannes Berge3a55b52015-05-05 16:32:29 +0200627 seq[seq_len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000628 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200629 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200630 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200631 memcpy(key->conf.key, key_data, key_len);
632 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200633
Johannes Bergdb4d1162008-02-25 16:27:45 +0100634 return key;
635}
Johannes Berg11a843b2007-08-28 17:01:55 -0400636
Johannes Berg79cf2df2013-03-06 22:53:52 +0100637static void ieee80211_key_free_common(struct ieee80211_key *key)
638{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200639 switch (key->conf.cipher) {
640 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200641 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100642 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200643 break;
644 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200645 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100646 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200647 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200648 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
649 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
650 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
651 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200652 case WLAN_CIPHER_SUITE_GCMP:
653 case WLAN_CIPHER_SUITE_GCMP_256:
654 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
655 break;
656 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300657 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100658}
659
Johannes Berg6d10e462013-03-06 23:09:11 +0100660static void __ieee80211_key_destroy(struct ieee80211_key *key,
661 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200662{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530663 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100664 struct ieee80211_sub_if_data *sdata = key->sdata;
665
Jouni Malinen32162a42010-07-26 15:52:03 -0700666 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100667
668 if (delay_tailroom) {
669 /* see ieee80211_delayed_tailroom_dec */
670 sdata->crypto_tx_tailroom_pending_dec++;
671 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
672 HZ/2);
673 } else {
Michal Kaziorf9dca802015-05-13 09:16:48 +0000674 decrease_tailroom_need_count(sdata, 1);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100675 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530676 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200677
Johannes Berg79cf2df2013-03-06 22:53:52 +0100678 ieee80211_key_free_common(key);
679}
680
Johannes Berg6d10e462013-03-06 23:09:11 +0100681static void ieee80211_key_destroy(struct ieee80211_key *key,
682 bool delay_tailroom)
683{
684 if (!key)
685 return;
686
687 /*
Eliad Pelleref044762015-11-17 10:24:37 +0200688 * Synchronize so the TX path and rcu key iterators
689 * can no longer be using this key before we free/remove it.
Johannes Berg6d10e462013-03-06 23:09:11 +0100690 */
691 synchronize_net();
692
693 __ieee80211_key_destroy(key, delay_tailroom);
694}
695
Johannes Berg79cf2df2013-03-06 22:53:52 +0100696void ieee80211_key_free_unused(struct ieee80211_key *key)
697{
698 WARN_ON(key->sdata || key->local);
699 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200700}
701
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200702static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
703 struct ieee80211_key *old,
704 struct ieee80211_key *new)
705{
706 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
707 u8 *tk_old, *tk_new;
708
709 if (!old || new->conf.keylen != old->conf.keylen)
710 return false;
711
712 tk_old = old->conf.key;
713 tk_new = new->conf.key;
714
715 /*
716 * In station mode, don't compare the TX MIC key, as it's never used
717 * and offloaded rekeying may not care to send it to the host. This
718 * is the case in iwlwifi, for example.
719 */
720 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
721 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
722 new->conf.keylen == WLAN_KEY_LEN_TKIP &&
723 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
724 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
725 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
726 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
727 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
728 tk_old = tkip_old;
729 tk_new = tkip_new;
730 }
731
732 return !crypto_memneq(tk_old, tk_new, new->conf.keylen);
733}
734
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300735int ieee80211_key_link(struct ieee80211_key *key,
736 struct ieee80211_sub_if_data *sdata,
737 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100738{
739 struct ieee80211_key *old_key;
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +0530740 int idx = key->conf.keyidx;
741 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
742 /*
743 * We want to delay tailroom updates only for station - in that
744 * case it helps roaming speed, but in other cases it hurts and
745 * can cause warnings to appear.
746 */
747 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION;
748 int ret;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100749
Johannes Bergad0e2b52010-06-01 10:19:19 +0200750 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200751
Johannes Berge31b8212010-10-05 19:39:30 +0200752 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200753 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200754 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200755 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400756 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200757 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100758
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200759 /*
760 * Silently accept key re-installation without really installing the
761 * new version of the key to avoid nonce reuse or replay issues.
762 */
Johannes Bergcfbb0d92017-10-24 21:12:13 +0200763 if (ieee80211_key_identical(sdata, old_key, key)) {
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200764 ieee80211_key_free_unused(key);
765 ret = 0;
766 goto out;
767 }
768
769 key->local = sdata->local;
770 key->sdata = sdata;
771 key->sta = sta;
772
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530773 increment_tailroom_need_count(sdata);
774
Alexander Wetzel62872a92018-08-31 15:00:38 +0200775 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400776
Alexander Wetzel62872a92018-08-31 15:00:38 +0200777 if (!ret) {
778 ieee80211_debugfs_key_add(key);
779 ieee80211_key_destroy(old_key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200780 } else {
Alexander Wetzel62872a92018-08-31 15:00:38 +0200781 ieee80211_key_free(key, delay_tailroom);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200782 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100783
Johannes Bergfdf7cb42017-09-05 14:54:54 +0200784 out:
Johannes Bergad0e2b52010-06-01 10:19:19 +0200785 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300786
787 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200788}
789
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100790void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200791{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200792 if (!key)
793 return;
794
Johannes Berg3a245762008-04-09 16:45:37 +0200795 /*
796 * Replace key with nothingness if it was ever used.
797 */
798 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100799 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200800 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
801 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100802 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200803}
804
Johannes Berg3b967662008-04-08 17:56:52 +0200805void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
806{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200807 struct ieee80211_key *key;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000808 struct ieee80211_sub_if_data *vlan;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200809
Johannes Berg3a245762008-04-09 16:45:37 +0200810 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200811
Johannes Berg9607e6b662009-12-23 13:15:31 +0100812 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200813 return;
814
Johannes Bergad0e2b52010-06-01 10:19:19 +0200815 mutex_lock(&sdata->local->key_mtx);
816
Michal Kaziorf9dca802015-05-13 09:16:48 +0000817 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
818 sdata->crypto_tx_tailroom_pending_dec);
819
820 if (sdata->vif.type == NL80211_IFTYPE_AP) {
821 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
822 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
823 vlan->crypto_tx_tailroom_pending_dec);
824 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530825
826 list_for_each_entry(key, &sdata->key_list, list) {
827 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200828 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530829 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200830
831 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200832}
833
Michal Kaziorf9dca802015-05-13 09:16:48 +0000834void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
835{
836 struct ieee80211_sub_if_data *vlan;
837
838 mutex_lock(&sdata->local->key_mtx);
839
840 sdata->crypto_tx_tailroom_needed_cnt = 0;
841
842 if (sdata->vif.type == NL80211_IFTYPE_AP) {
843 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
844 vlan->crypto_tx_tailroom_needed_cnt = 0;
845 }
846
847 mutex_unlock(&sdata->local->key_mtx);
848}
849
Johannes Berg830af022011-07-05 16:35:39 +0200850void ieee80211_iter_keys(struct ieee80211_hw *hw,
851 struct ieee80211_vif *vif,
852 void (*iter)(struct ieee80211_hw *hw,
853 struct ieee80211_vif *vif,
854 struct ieee80211_sta *sta,
855 struct ieee80211_key_conf *key,
856 void *data),
857 void *iter_data)
858{
859 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200860 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200861 struct ieee80211_sub_if_data *sdata;
862
863 ASSERT_RTNL();
864
865 mutex_lock(&local->key_mtx);
866 if (vif) {
867 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200868 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200869 iter(hw, &sdata->vif,
870 key->sta ? &key->sta->sta : NULL,
871 &key->conf, iter_data);
872 } else {
873 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200874 list_for_each_entry_safe(key, tmp,
875 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200876 iter(hw, &sdata->vif,
877 key->sta ? &key->sta->sta : NULL,
878 &key->conf, iter_data);
879 }
880 mutex_unlock(&local->key_mtx);
881}
882EXPORT_SYMBOL(ieee80211_iter_keys);
883
Eliad Pelleref044762015-11-17 10:24:37 +0200884static void
885_ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
886 struct ieee80211_sub_if_data *sdata,
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_key *key;
895
896 list_for_each_entry_rcu(key, &sdata->key_list, list) {
897 /* skip keys of station in removal process */
898 if (key->sta && key->sta->removed)
899 continue;
900 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
901 continue;
902
903 iter(hw, &sdata->vif,
904 key->sta ? &key->sta->sta : NULL,
905 &key->conf, iter_data);
906 }
907}
908
909void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw,
910 struct ieee80211_vif *vif,
911 void (*iter)(struct ieee80211_hw *hw,
912 struct ieee80211_vif *vif,
913 struct ieee80211_sta *sta,
914 struct ieee80211_key_conf *key,
915 void *data),
916 void *iter_data)
917{
918 struct ieee80211_local *local = hw_to_local(hw);
919 struct ieee80211_sub_if_data *sdata;
920
921 if (vif) {
922 sdata = vif_to_sdata(vif);
923 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
924 } else {
925 list_for_each_entry_rcu(sdata, &local->interfaces, list)
926 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data);
927 }
928}
929EXPORT_SYMBOL(ieee80211_iter_keys_rcu);
930
Johannes Berg7907c7d2013-12-04 23:47:09 +0100931static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
932 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400933{
934 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200935
Michal Kaziorf9dca802015-05-13 09:16:48 +0000936 decrease_tailroom_need_count(sdata,
937 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100938 sdata->crypto_tx_tailroom_pending_dec = 0;
939
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200940 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400941
Johannes Berg6d10e462013-03-06 23:09:11 +0100942 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
943 ieee80211_key_replace(key->sdata, key->sta,
944 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
945 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100946 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100947 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400948
Johannes Bergf7e01042010-12-09 19:49:02 +0100949 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100950}
Johannes Bergf7e01042010-12-09 19:49:02 +0100951
Johannes Berg7907c7d2013-12-04 23:47:09 +0100952void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
953 bool force_synchronize)
954{
955 struct ieee80211_local *local = sdata->local;
956 struct ieee80211_sub_if_data *vlan;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000957 struct ieee80211_sub_if_data *master;
Johannes Berg7907c7d2013-12-04 23:47:09 +0100958 struct ieee80211_key *key, *tmp;
959 LIST_HEAD(keys);
960
961 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
962
963 mutex_lock(&local->key_mtx);
964
965 ieee80211_free_keys_iface(sdata, &keys);
966
967 if (sdata->vif.type == NL80211_IFTYPE_AP) {
968 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
969 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100970 }
971
Johannes Berg7907c7d2013-12-04 23:47:09 +0100972 if (!list_empty(&keys) || force_synchronize)
973 synchronize_net();
974 list_for_each_entry_safe(key, tmp, &keys, list)
975 __ieee80211_key_destroy(key, false);
976
Michal Kaziorf9dca802015-05-13 09:16:48 +0000977 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
978 if (sdata->bss) {
979 master = container_of(sdata->bss,
980 struct ieee80211_sub_if_data,
981 u.ap);
982
983 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
984 master->crypto_tx_tailroom_needed_cnt);
985 }
986 } else {
987 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
988 sdata->crypto_tx_tailroom_pending_dec);
989 }
990
Johannes Berg7907c7d2013-12-04 23:47:09 +0100991 if (sdata->vif.type == NL80211_IFTYPE_AP) {
992 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
993 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
994 vlan->crypto_tx_tailroom_pending_dec);
995 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100996
Johannes Berg7907c7d2013-12-04 23:47:09 +0100997 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400998}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200999
Johannes Berg6d10e462013-03-06 23:09:11 +01001000void ieee80211_free_sta_keys(struct ieee80211_local *local,
1001 struct sta_info *sta)
1002{
Johannes Bergc8782072013-12-04 23:05:45 +01001003 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +01001004 int i;
1005
1006 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +01001007 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +01001008 key = key_mtx_dereference(local, sta->gtk[i]);
1009 if (!key)
1010 continue;
1011 ieee80211_key_replace(key->sdata, key->sta,
1012 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1013 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301014 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1015 NL80211_IFTYPE_STATION);
Johannes Berg6d10e462013-03-06 23:09:11 +01001016 }
1017
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001018 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1019 key = key_mtx_dereference(local, sta->ptk[i]);
1020 if (!key)
1021 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +01001022 ieee80211_key_replace(key->sdata, key->sta,
1023 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
1024 key, NULL);
Manikanta Pubbisetty133bf902018-07-10 16:48:27 +05301025 __ieee80211_key_destroy(key, key->sdata->vif.type ==
1026 NL80211_IFTYPE_STATION);
Johannes Bergc8782072013-12-04 23:05:45 +01001027 }
Johannes Berg6d10e462013-03-06 23:09:11 +01001028
1029 mutex_unlock(&local->key_mtx);
1030}
1031
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001032void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
1033{
1034 struct ieee80211_sub_if_data *sdata;
1035
1036 sdata = container_of(wk, struct ieee80211_sub_if_data,
1037 dec_tailroom_needed_wk.work);
1038
1039 /*
1040 * The reason for the delayed tailroom needed decrementing is to
1041 * make roaming faster: during roaming, all keys are first deleted
1042 * and then new keys are installed. The first new key causes the
1043 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
1044 * the cost of synchronize_net() (which can be slow). Avoid this
1045 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
1046 * key removal for a while, so if we roam the value is larger than
1047 * zero and no 0->1 transition happens.
1048 *
1049 * The cost is that if the AP switching was from an AP with keys
1050 * to one without, we still allocate tailroom while it would no
1051 * longer be needed. However, in the typical (fast) roaming case
1052 * within an ESS this usually won't happen.
1053 */
1054
1055 mutex_lock(&sdata->local->key_mtx);
Michal Kaziorf9dca802015-05-13 09:16:48 +00001056 decrease_tailroom_need_count(sdata,
1057 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +01001058 sdata->crypto_tx_tailroom_pending_dec = 0;
1059 mutex_unlock(&sdata->local->key_mtx);
1060}
Johannes Bergc68f4b82011-07-05 16:35:41 +02001061
1062void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
1063 const u8 *replay_ctr, gfp_t gfp)
1064{
1065 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1066
1067 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
1068
1069 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
1070}
1071EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001072
Johannes Berg3ea542d2011-07-07 18:58:00 +02001073void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
1074 int tid, struct ieee80211_key_seq *seq)
1075{
1076 struct ieee80211_key *key;
1077 const u8 *pn;
1078
1079 key = container_of(keyconf, struct ieee80211_key, conf);
1080
1081 switch (key->conf.cipher) {
1082 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +01001083 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001084 return;
1085 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
1086 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
1087 break;
1088 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001089 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +01001090 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +02001091 return;
1092 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +01001093 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +02001094 else
1095 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +02001096 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001097 break;
1098 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001099 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +02001100 if (WARN_ON(tid != 0))
1101 return;
1102 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001103 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +02001104 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001105 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1106 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1107 if (WARN_ON(tid != 0))
1108 return;
1109 pn = key->u.aes_gmac.rx_pn;
1110 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
1111 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001112 case WLAN_CIPHER_SUITE_GCMP:
1113 case WLAN_CIPHER_SUITE_GCMP_256:
1114 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1115 return;
1116 if (tid < 0)
1117 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1118 else
1119 pn = key->u.gcmp.rx_pn[tid];
1120 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
1121 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +02001122 }
1123}
1124EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001125
Johannes Berg27b3eb92013-08-07 20:11:55 +02001126void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1127 int tid, struct ieee80211_key_seq *seq)
1128{
1129 struct ieee80211_key *key;
1130 u8 *pn;
1131
1132 key = container_of(keyconf, struct ieee80211_key, conf);
1133
1134 switch (key->conf.cipher) {
1135 case WLAN_CIPHER_SUITE_TKIP:
1136 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1137 return;
1138 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1139 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1140 break;
1141 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001142 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001143 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1144 return;
1145 if (tid < 0)
1146 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1147 else
1148 pn = key->u.ccmp.rx_pn[tid];
1149 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1150 break;
1151 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001152 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001153 if (WARN_ON(tid != 0))
1154 return;
1155 pn = key->u.aes_cmac.rx_pn;
1156 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1157 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001158 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1159 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1160 if (WARN_ON(tid != 0))
1161 return;
1162 pn = key->u.aes_gmac.rx_pn;
1163 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1164 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001165 case WLAN_CIPHER_SUITE_GCMP:
1166 case WLAN_CIPHER_SUITE_GCMP_256:
1167 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1168 return;
1169 if (tid < 0)
1170 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1171 else
1172 pn = key->u.gcmp.rx_pn[tid];
1173 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1174 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001175 default:
1176 WARN_ON(1);
1177 break;
1178 }
1179}
1180EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1181
1182void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1183{
1184 struct ieee80211_key *key;
1185
1186 key = container_of(keyconf, struct ieee80211_key, conf);
1187
1188 assert_key_lock(key->local);
1189
1190 /*
1191 * if key was uploaded, we assume the driver will/has remove(d)
1192 * it, so adjust bookkeeping accordingly
1193 */
1194 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1195 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1196
Alexander Wetzel092c4092019-03-16 21:44:43 +01001197 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
1198 IEEE80211_KEY_FLAG_PUT_MIC_SPACE |
1199 IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001200 increment_tailroom_need_count(key->sdata);
1201 }
1202
1203 ieee80211_key_free(key, false);
1204}
1205EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1206
1207struct ieee80211_key_conf *
1208ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1209 struct ieee80211_key_conf *keyconf)
1210{
1211 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1212 struct ieee80211_local *local = sdata->local;
1213 struct ieee80211_key *key;
1214 int err;
1215
1216 if (WARN_ON(!local->wowlan))
1217 return ERR_PTR(-EINVAL);
1218
1219 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1220 return ERR_PTR(-EINVAL);
1221
1222 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1223 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001224 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001225 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001226 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001227
1228 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1229 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1230
1231 err = ieee80211_key_link(key, sdata, NULL);
1232 if (err)
1233 return ERR_PTR(err);
1234
1235 return &key->conf;
1236}
1237EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);