blob: 7ceea9d9fcd274493812a5c44d57063d3f029885 [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 Berg1f5a7e42007-07-27 15:43:23 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Johannes Berg11a843b2007-08-28 17:01:55 -040013#include <linux/if_ether.h>
14#include <linux/etherdevice.h>
15#include <linux/list.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040016#include <linux/rcupdate.h>
Johannes Bergdb4d1162008-02-25 16:27:45 +010017#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040019#include <linux/export.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020020#include <net/mac80211.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010021#include <asm/unaligned.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020022#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020023#include "driver-ops.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020024#include "debugfs_key.h"
25#include "aes_ccm.h"
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +020026#include "aes_cmac.h"
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020027#include "aes_gcm.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020028
Johannes Berg11a843b2007-08-28 17:01:55 -040029
Johannes Bergdbbea672008-02-26 14:34:06 +010030/**
31 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040032 *
33 * Key handling in mac80211 is done based on per-interface (sub_if_data)
34 * keys and per-station keys. Since each station belongs to an interface,
35 * each station key also belongs to that interface.
36 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010037 * Hardware acceleration is done on a best-effort basis for algorithms
38 * that are implemented in software, for each key the hardware is asked
39 * to enable that key for offloading but if it cannot do that the key is
40 * simply kept for software encryption (unless it is for an algorithm
41 * that isn't implemented in software).
42 * There is currently no way of knowing whether a key is handled in SW
43 * or HW except by looking into debugfs.
Johannes Berg11a843b2007-08-28 17:01:55 -040044 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010045 * All key management is internally protected by a mutex. Within all
46 * other parts of mac80211, key references are, just as STA structure
47 * references, protected by RCU. Note, however, that some things are
48 * unprotected, namely the key->sta dereferences within the hardware
49 * acceleration functions. This means that sta_info_destroy() must
50 * remove the key which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040051 */
52
53static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040054
Johannes Bergad0e2b52010-06-01 10:19:19 +020055static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020056{
Johannes Berg46a5eba2010-09-15 13:28:15 +020057 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020058}
59
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053060static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
61{
62 /*
63 * When this count is zero, SKB resizing for allocating tailroom
64 * for IV or MMIC is skipped. But, this check has created two race
65 * cases in xmit path while transiting from zero count to one:
66 *
67 * 1. SKB resize was skipped because no key was added but just before
68 * the xmit key is added and SW encryption kicks off.
69 *
70 * 2. SKB resize was skipped because all the keys were hw planted but
71 * just before xmit one of the key is deleted and SW encryption kicks
72 * off.
73 *
74 * In both the above case SW encryption will find not enough space for
75 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
76 *
77 * Solution has been explained at
78 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
79 */
80
81 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
82 /*
83 * Flush all XMIT packets currently using HW encryption or no
84 * encryption at all if the count transition is from 0 -> 1.
85 */
86 synchronize_net();
87 }
88}
89
Johannes Berg3ffc2a92010-08-27 14:26:52 +030090static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040091{
Johannes Bergdc822b52008-12-29 12:55:09 +010092 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +010093 struct sta_info *sta;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +010094 int ret = -EOPNOTSUPP;
Johannes Berg11a843b2007-08-28 17:01:55 -040095
Johannes Berg3b967662008-04-08 17:56:52 +020096 might_sleep();
97
Johannes Berg46191942014-10-13 13:43:29 +020098 if (key->flags & KEY_FLAG_TAINTED) {
99 /* If we get here, it's during resume and the key is
100 * tainted so shouldn't be used/programmed any more.
101 * However, its flags may still indicate that it was
102 * programmed into the device (since we're in resume)
103 * so clear that flag now to avoid trying to remove
104 * it again later.
105 */
106 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200107 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200108 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200109
Johannes Berge31b8212010-10-05 19:39:30 +0200110 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300111 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400112
Johannes Bergad0e2b52010-06-01 10:19:19 +0200113 assert_key_lock(key->local);
114
Johannes Berg89c91ca2012-01-20 13:55:19 +0100115 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100116
Johannes Berge31b8212010-10-05 19:39:30 +0200117 /*
118 * If this is a per-STA GTK, check if it
119 * is supported; if not, return.
120 */
121 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
122 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
123 goto out_unsupported;
124
Johannes Berg89c91ca2012-01-20 13:55:19 +0100125 if (sta && !sta->uploaded)
126 goto out_unsupported;
127
Johannes Bergdc822b52008-12-29 12:55:09 +0100128 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +0100129 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
130 /*
131 * The driver doesn't know anything about VLAN interfaces.
132 * Hence, don't send GTKs for VLAN interfaces to the driver.
133 */
134 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
135 goto out_unsupported;
Helmut Schaa18890d42010-11-19 08:11:01 +0100136 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400137
Johannes Berg89c91ca2012-01-20 13:55:19 +0100138 ret = drv_set_key(key->local, SET_KEY, sdata,
139 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400140
Johannes Berge31b8212010-10-05 19:39:30 +0200141 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400142 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530143
Johannes Berg1e359a52015-01-05 10:28:49 +0100144 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500145 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530146 sdata->crypto_tx_tailroom_needed_cnt--;
147
Arik Nemtsov077a9152011-10-23 08:21:41 +0200148 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
149 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
150
Johannes Berge31b8212010-10-05 19:39:30 +0200151 return 0;
152 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400153
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100154 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200155 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700156 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100157 key->conf.keyidx,
158 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300159
Johannes Berge31b8212010-10-05 19:39:30 +0200160 out_unsupported:
161 switch (key->conf.cipher) {
162 case WLAN_CIPHER_SUITE_WEP40:
163 case WLAN_CIPHER_SUITE_WEP104:
164 case WLAN_CIPHER_SUITE_TKIP:
165 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200166 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200167 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200168 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200169 case WLAN_CIPHER_SUITE_GCMP:
170 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100171 /* all of these we can do in software - if driver can */
172 if (ret == 1)
173 return 0;
174 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
175 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200176 return 0;
177 default:
178 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300179 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400180}
181
182static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
183{
Johannes Bergdc822b52008-12-29 12:55:09 +0100184 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100185 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400186 int ret;
187
Johannes Berg3b967662008-04-08 17:56:52 +0200188 might_sleep();
189
Johannes Bergdb4d1162008-02-25 16:27:45 +0100190 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400191 return;
192
Johannes Bergad0e2b52010-06-01 10:19:19 +0200193 assert_key_lock(key->local);
194
195 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400196 return;
197
Johannes Berg89c91ca2012-01-20 13:55:19 +0100198 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100199 sdata = key->sdata;
200
Johannes Berg1e359a52015-01-05 10:28:49 +0100201 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500202 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530203 increment_tailroom_need_count(sdata);
204
Johannes Berg12375ef2009-11-25 20:30:31 +0100205 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100206 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400207
208 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200209 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700210 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100211 key->conf.keyidx,
212 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400213
Johannes Berg3b967662008-04-08 17:56:52 +0200214 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200215}
216
217static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100218 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200219{
220 struct ieee80211_key *key = NULL;
221
Johannes Bergad0e2b52010-06-01 10:19:19 +0200222 assert_key_lock(sdata->local);
223
Johannes Berg3b967662008-04-08 17:56:52 +0200224 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200225 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200226
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300227 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100228 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300229 drv_set_default_unicast_key(sdata->local, sdata, idx);
230 }
231
Johannes Bergf7e01042010-12-09 19:49:02 +0100232 if (multi)
233 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200234
Johannes Bergf7e01042010-12-09 19:49:02 +0100235 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200236}
237
Johannes Bergf7e01042010-12-09 19:49:02 +0100238void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
239 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200240{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200241 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100242 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200243 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200244}
245
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200246static void
247__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
248{
249 struct ieee80211_key *key = NULL;
250
Johannes Bergad0e2b52010-06-01 10:19:19 +0200251 assert_key_lock(sdata->local);
252
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200253 if (idx >= NUM_DEFAULT_KEYS &&
254 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200255 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200256
257 rcu_assign_pointer(sdata->default_mgmt_key, key);
258
Johannes Bergf7e01042010-12-09 19:49:02 +0100259 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200260}
261
262void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
263 int idx)
264{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200265 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200266 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200267 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200268}
269
Johannes Berg3b967662008-04-08 17:56:52 +0200270
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100271static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
272 struct sta_info *sta,
273 bool pairwise,
274 struct ieee80211_key *old,
275 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200276{
Johannes Bergf7e01042010-12-09 19:49:02 +0100277 int idx;
278 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200279
Johannes Berg5282c3b2013-10-29 10:00:08 +0100280 /* caller must provide at least one old/new */
281 if (WARN_ON(!new && !old))
282 return;
283
Johannes Berg3b967662008-04-08 17:56:52 +0200284 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200285 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200286
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200287 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
288
289 if (old)
290 idx = old->conf.keyidx;
291 else
292 idx = new->conf.keyidx;
293
294 if (sta) {
295 if (pairwise) {
296 rcu_assign_pointer(sta->ptk[idx], new);
297 sta->ptk_idx = idx;
298 } else {
299 rcu_assign_pointer(sta->gtk[idx], new);
300 sta->gtk_idx = idx;
301 }
Johannes Berg3b967662008-04-08 17:56:52 +0200302 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200303 defunikey = old &&
304 old == key_mtx_dereference(sdata->local,
305 sdata->default_unicast_key);
306 defmultikey = old &&
307 old == key_mtx_dereference(sdata->local,
308 sdata->default_multicast_key);
309 defmgmtkey = old &&
310 old == key_mtx_dereference(sdata->local,
311 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200312
Johannes Bergf7e01042010-12-09 19:49:02 +0100313 if (defunikey && !new)
314 __ieee80211_set_default_key(sdata, -1, true, false);
315 if (defmultikey && !new)
316 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200317 if (defmgmtkey && !new)
318 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200319
320 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100321 if (defunikey && new)
322 __ieee80211_set_default_key(sdata, new->conf.keyidx,
323 true, false);
324 if (defmultikey && new)
325 __ieee80211_set_default_key(sdata, new->conf.keyidx,
326 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200327 if (defmgmtkey && new)
328 __ieee80211_set_default_mgmt_key(sdata,
329 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200330 }
331
Johannes Bergb5c34f62011-01-03 19:51:09 +0100332 if (old)
333 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400334}
335
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200336struct ieee80211_key *
337ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
338 const u8 *key_data,
339 size_t seq_len, const u8 *seq,
340 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200341{
342 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100343 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200344
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200345 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
346 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400347
348 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200349 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100350 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400351
352 /*
353 * Default to software encryption; we'll later upload the
354 * key to the hardware if possible.
355 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400356 key->conf.flags = 0;
357 key->flags = 0;
358
Johannes Berg97359d12010-08-10 09:46:38 +0200359 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400360 key->conf.keyidx = idx;
361 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200362 switch (cipher) {
363 case WLAN_CIPHER_SUITE_WEP40:
364 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200365 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
366 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200367 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200368 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200369 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
370 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300371 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100372 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300373 key->u.tkip.rx[i].iv32 =
374 get_unaligned_le32(&seq[2]);
375 key->u.tkip.rx[i].iv16 =
376 get_unaligned_le16(seq);
377 }
378 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200379 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200380 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200381 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200382 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
383 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300384 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100385 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200386 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300387 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200388 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300389 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400390 /*
391 * Initialize AES key state here as an optimization so that
392 * it does not need to be initialized for every packet.
393 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200394 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
395 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
396 if (IS_ERR(key->u.ccmp.tfm)) {
397 err = PTR_ERR(key->u.ccmp.tfm);
398 kfree(key);
399 return ERR_PTR(err);
400 }
401 break;
402 case WLAN_CIPHER_SUITE_CCMP_256:
403 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
404 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
405 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
406 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
407 key->u.ccmp.rx_pn[i][j] =
408 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
409 /* Initialize AES key state here as an optimization so that
410 * it does not need to be initialized for every packet.
411 */
412 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
413 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100414 if (IS_ERR(key->u.ccmp.tfm)) {
415 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200416 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200417 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400418 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200419 break;
420 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200421 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200422 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200423 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
424 key->conf.icv_len = sizeof(struct ieee80211_mmie);
425 else
426 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200427 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200428 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100429 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200430 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200431 /*
432 * Initialize AES key state here as an optimization so that
433 * it does not need to be initialized for every packet.
434 */
435 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200436 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100437 if (IS_ERR(key->u.aes_cmac.tfm)) {
438 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200439 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200440 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200441 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200442 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200443 case WLAN_CIPHER_SUITE_GCMP:
444 case WLAN_CIPHER_SUITE_GCMP_256:
445 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
446 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
447 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
448 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
449 key->u.gcmp.rx_pn[i][j] =
450 seq[IEEE80211_GCMP_PN_LEN - j - 1];
451 /* Initialize AES key state here as an optimization so that
452 * it does not need to be initialized for every packet.
453 */
454 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
455 key_len);
456 if (IS_ERR(key->u.gcmp.tfm)) {
457 err = PTR_ERR(key->u.gcmp.tfm);
458 kfree(key);
459 return ERR_PTR(err);
460 }
461 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200462 default:
463 if (cs) {
464 size_t len = (seq_len > MAX_PN_LEN) ?
465 MAX_PN_LEN : seq_len;
466
467 key->conf.iv_len = cs->hdr_len;
468 key->conf.icv_len = cs->mic_len;
469 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
470 for (j = 0; j < len; j++)
471 key->u.gen.rx_pn[i][j] =
472 seq[len - j - 1];
473 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200474 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200475 memcpy(key->conf.key, key_data, key_len);
476 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200477
Johannes Bergdb4d1162008-02-25 16:27:45 +0100478 return key;
479}
Johannes Berg11a843b2007-08-28 17:01:55 -0400480
Johannes Berg79cf2df2013-03-06 22:53:52 +0100481static void ieee80211_key_free_common(struct ieee80211_key *key)
482{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200483 switch (key->conf.cipher) {
484 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200485 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100486 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200487 break;
488 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200489 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100490 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200491 break;
492 case WLAN_CIPHER_SUITE_GCMP:
493 case WLAN_CIPHER_SUITE_GCMP_256:
494 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
495 break;
496 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300497 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100498}
499
Johannes Berg6d10e462013-03-06 23:09:11 +0100500static void __ieee80211_key_destroy(struct ieee80211_key *key,
501 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200502{
Jouni Malinen32162a42010-07-26 15:52:03 -0700503 if (key->local)
504 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200505
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530506 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100507 struct ieee80211_sub_if_data *sdata = key->sdata;
508
Jouni Malinen32162a42010-07-26 15:52:03 -0700509 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100510
511 if (delay_tailroom) {
512 /* see ieee80211_delayed_tailroom_dec */
513 sdata->crypto_tx_tailroom_pending_dec++;
514 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
515 HZ/2);
516 } else {
517 sdata->crypto_tx_tailroom_needed_cnt--;
518 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530519 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200520
Johannes Berg79cf2df2013-03-06 22:53:52 +0100521 ieee80211_key_free_common(key);
522}
523
Johannes Berg6d10e462013-03-06 23:09:11 +0100524static void ieee80211_key_destroy(struct ieee80211_key *key,
525 bool delay_tailroom)
526{
527 if (!key)
528 return;
529
530 /*
531 * Synchronize so the TX path can no longer be using
532 * this key before we free/remove it.
533 */
534 synchronize_net();
535
536 __ieee80211_key_destroy(key, delay_tailroom);
537}
538
Johannes Berg79cf2df2013-03-06 22:53:52 +0100539void ieee80211_key_free_unused(struct ieee80211_key *key)
540{
541 WARN_ON(key->sdata || key->local);
542 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200543}
544
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300545int ieee80211_key_link(struct ieee80211_key *key,
546 struct ieee80211_sub_if_data *sdata,
547 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100548{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200549 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100550 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300551 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100552 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100553
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100554 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100555 idx = key->conf.keyidx;
556 key->local = sdata->local;
557 key->sdata = sdata;
558 key->sta = sta;
559
Johannes Bergad0e2b52010-06-01 10:19:19 +0200560 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200561
Johannes Berge31b8212010-10-05 19:39:30 +0200562 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200563 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200564 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200565 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400566 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200567 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100568
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530569 increment_tailroom_need_count(sdata);
570
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100571 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
572 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400573
Johannes Bergad0e2b52010-06-01 10:19:19 +0200574 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200575
Johannes Berg27b3eb92013-08-07 20:11:55 +0200576 if (!local->wowlan) {
577 ret = ieee80211_key_enable_hw_accel(key);
578 if (ret)
579 ieee80211_key_free(key, true);
580 } else {
581 ret = 0;
582 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100583
Johannes Bergad0e2b52010-06-01 10:19:19 +0200584 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300585
586 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200587}
588
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100589void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200590{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200591 if (!key)
592 return;
593
Johannes Berg3a245762008-04-09 16:45:37 +0200594 /*
595 * Replace key with nothingness if it was ever used.
596 */
597 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100598 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200599 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
600 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100601 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200602}
603
Johannes Berg3b967662008-04-08 17:56:52 +0200604void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
605{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200606 struct ieee80211_key *key;
607
Johannes Berg3a245762008-04-09 16:45:37 +0200608 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200609
Johannes Berg9607e6b662009-12-23 13:15:31 +0100610 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200611 return;
612
Johannes Bergad0e2b52010-06-01 10:19:19 +0200613 mutex_lock(&sdata->local->key_mtx);
614
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530615 sdata->crypto_tx_tailroom_needed_cnt = 0;
616
617 list_for_each_entry(key, &sdata->key_list, list) {
618 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200619 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530620 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200621
622 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200623}
624
Johannes Berg830af022011-07-05 16:35:39 +0200625void ieee80211_iter_keys(struct ieee80211_hw *hw,
626 struct ieee80211_vif *vif,
627 void (*iter)(struct ieee80211_hw *hw,
628 struct ieee80211_vif *vif,
629 struct ieee80211_sta *sta,
630 struct ieee80211_key_conf *key,
631 void *data),
632 void *iter_data)
633{
634 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200635 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200636 struct ieee80211_sub_if_data *sdata;
637
638 ASSERT_RTNL();
639
640 mutex_lock(&local->key_mtx);
641 if (vif) {
642 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200643 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200644 iter(hw, &sdata->vif,
645 key->sta ? &key->sta->sta : NULL,
646 &key->conf, iter_data);
647 } else {
648 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200649 list_for_each_entry_safe(key, tmp,
650 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200651 iter(hw, &sdata->vif,
652 key->sta ? &key->sta->sta : NULL,
653 &key->conf, iter_data);
654 }
655 mutex_unlock(&local->key_mtx);
656}
657EXPORT_SYMBOL(ieee80211_iter_keys);
658
Johannes Berg7907c7d2013-12-04 23:47:09 +0100659static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
660 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400661{
662 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200663
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100664 sdata->crypto_tx_tailroom_needed_cnt -=
665 sdata->crypto_tx_tailroom_pending_dec;
666 sdata->crypto_tx_tailroom_pending_dec = 0;
667
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200668 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400669
Johannes Berg6d10e462013-03-06 23:09:11 +0100670 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
671 ieee80211_key_replace(key->sdata, key->sta,
672 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
673 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100674 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100675 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400676
Johannes Bergf7e01042010-12-09 19:49:02 +0100677 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100678}
Johannes Bergf7e01042010-12-09 19:49:02 +0100679
Johannes Berg7907c7d2013-12-04 23:47:09 +0100680void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
681 bool force_synchronize)
682{
683 struct ieee80211_local *local = sdata->local;
684 struct ieee80211_sub_if_data *vlan;
685 struct ieee80211_key *key, *tmp;
686 LIST_HEAD(keys);
687
688 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
689
690 mutex_lock(&local->key_mtx);
691
692 ieee80211_free_keys_iface(sdata, &keys);
693
694 if (sdata->vif.type == NL80211_IFTYPE_AP) {
695 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
696 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100697 }
698
Johannes Berg7907c7d2013-12-04 23:47:09 +0100699 if (!list_empty(&keys) || force_synchronize)
700 synchronize_net();
701 list_for_each_entry_safe(key, tmp, &keys, list)
702 __ieee80211_key_destroy(key, false);
703
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100704 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
705 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100706 if (sdata->vif.type == NL80211_IFTYPE_AP) {
707 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
708 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
709 vlan->crypto_tx_tailroom_pending_dec);
710 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100711
Johannes Berg7907c7d2013-12-04 23:47:09 +0100712 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400713}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200714
Johannes Berg6d10e462013-03-06 23:09:11 +0100715void ieee80211_free_sta_keys(struct ieee80211_local *local,
716 struct sta_info *sta)
717{
Johannes Bergc8782072013-12-04 23:05:45 +0100718 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100719 int i;
720
721 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100722 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100723 key = key_mtx_dereference(local, sta->gtk[i]);
724 if (!key)
725 continue;
726 ieee80211_key_replace(key->sdata, key->sta,
727 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
728 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100729 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100730 }
731
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200732 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
733 key = key_mtx_dereference(local, sta->ptk[i]);
734 if (!key)
735 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100736 ieee80211_key_replace(key->sdata, key->sta,
737 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
738 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100739 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100740 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100741
742 mutex_unlock(&local->key_mtx);
743}
744
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100745void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
746{
747 struct ieee80211_sub_if_data *sdata;
748
749 sdata = container_of(wk, struct ieee80211_sub_if_data,
750 dec_tailroom_needed_wk.work);
751
752 /*
753 * The reason for the delayed tailroom needed decrementing is to
754 * make roaming faster: during roaming, all keys are first deleted
755 * and then new keys are installed. The first new key causes the
756 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
757 * the cost of synchronize_net() (which can be slow). Avoid this
758 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
759 * key removal for a while, so if we roam the value is larger than
760 * zero and no 0->1 transition happens.
761 *
762 * The cost is that if the AP switching was from an AP with keys
763 * to one without, we still allocate tailroom while it would no
764 * longer be needed. However, in the typical (fast) roaming case
765 * within an ESS this usually won't happen.
766 */
767
768 mutex_lock(&sdata->local->key_mtx);
769 sdata->crypto_tx_tailroom_needed_cnt -=
770 sdata->crypto_tx_tailroom_pending_dec;
771 sdata->crypto_tx_tailroom_pending_dec = 0;
772 mutex_unlock(&sdata->local->key_mtx);
773}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200774
775void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
776 const u8 *replay_ctr, gfp_t gfp)
777{
778 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
779
780 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
781
782 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
783}
784EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200785
786void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
787 struct ieee80211_key_seq *seq)
788{
789 struct ieee80211_key *key;
790 u64 pn64;
791
792 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
793 return;
794
795 key = container_of(keyconf, struct ieee80211_key, conf);
796
797 switch (key->conf.cipher) {
798 case WLAN_CIPHER_SUITE_TKIP:
799 seq->tkip.iv32 = key->u.tkip.tx.iv32;
800 seq->tkip.iv16 = key->u.tkip.tx.iv16;
801 break;
802 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200803 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200804 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
805 seq->ccmp.pn[5] = pn64;
806 seq->ccmp.pn[4] = pn64 >> 8;
807 seq->ccmp.pn[3] = pn64 >> 16;
808 seq->ccmp.pn[2] = pn64 >> 24;
809 seq->ccmp.pn[1] = pn64 >> 32;
810 seq->ccmp.pn[0] = pn64 >> 40;
811 break;
812 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200813 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200814 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
815 seq->ccmp.pn[5] = pn64;
816 seq->ccmp.pn[4] = pn64 >> 8;
817 seq->ccmp.pn[3] = pn64 >> 16;
818 seq->ccmp.pn[2] = pn64 >> 24;
819 seq->ccmp.pn[1] = pn64 >> 32;
820 seq->ccmp.pn[0] = pn64 >> 40;
821 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200822 case WLAN_CIPHER_SUITE_GCMP:
823 case WLAN_CIPHER_SUITE_GCMP_256:
824 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
825 seq->gcmp.pn[5] = pn64;
826 seq->gcmp.pn[4] = pn64 >> 8;
827 seq->gcmp.pn[3] = pn64 >> 16;
828 seq->gcmp.pn[2] = pn64 >> 24;
829 seq->gcmp.pn[1] = pn64 >> 32;
830 seq->gcmp.pn[0] = pn64 >> 40;
831 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200832 default:
833 WARN_ON(1);
834 }
835}
836EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
837
838void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
839 int tid, struct ieee80211_key_seq *seq)
840{
841 struct ieee80211_key *key;
842 const u8 *pn;
843
844 key = container_of(keyconf, struct ieee80211_key, conf);
845
846 switch (key->conf.cipher) {
847 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100848 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200849 return;
850 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
851 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
852 break;
853 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200854 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +0100855 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200856 return;
857 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100858 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200859 else
860 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200861 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200862 break;
863 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200864 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200865 if (WARN_ON(tid != 0))
866 return;
867 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200868 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200869 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200870 case WLAN_CIPHER_SUITE_GCMP:
871 case WLAN_CIPHER_SUITE_GCMP_256:
872 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
873 return;
874 if (tid < 0)
875 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
876 else
877 pn = key->u.gcmp.rx_pn[tid];
878 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
879 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200880 }
881}
882EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200883
884void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
885 struct ieee80211_key_seq *seq)
886{
887 struct ieee80211_key *key;
888 u64 pn64;
889
890 key = container_of(keyconf, struct ieee80211_key, conf);
891
892 switch (key->conf.cipher) {
893 case WLAN_CIPHER_SUITE_TKIP:
894 key->u.tkip.tx.iv32 = seq->tkip.iv32;
895 key->u.tkip.tx.iv16 = seq->tkip.iv16;
896 break;
897 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200898 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200899 pn64 = (u64)seq->ccmp.pn[5] |
900 ((u64)seq->ccmp.pn[4] << 8) |
901 ((u64)seq->ccmp.pn[3] << 16) |
902 ((u64)seq->ccmp.pn[2] << 24) |
903 ((u64)seq->ccmp.pn[1] << 32) |
904 ((u64)seq->ccmp.pn[0] << 40);
905 atomic64_set(&key->u.ccmp.tx_pn, pn64);
906 break;
907 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200908 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200909 pn64 = (u64)seq->aes_cmac.pn[5] |
910 ((u64)seq->aes_cmac.pn[4] << 8) |
911 ((u64)seq->aes_cmac.pn[3] << 16) |
912 ((u64)seq->aes_cmac.pn[2] << 24) |
913 ((u64)seq->aes_cmac.pn[1] << 32) |
914 ((u64)seq->aes_cmac.pn[0] << 40);
915 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
916 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200917 case WLAN_CIPHER_SUITE_GCMP:
918 case WLAN_CIPHER_SUITE_GCMP_256:
919 pn64 = (u64)seq->gcmp.pn[5] |
920 ((u64)seq->gcmp.pn[4] << 8) |
921 ((u64)seq->gcmp.pn[3] << 16) |
922 ((u64)seq->gcmp.pn[2] << 24) |
923 ((u64)seq->gcmp.pn[1] << 32) |
924 ((u64)seq->gcmp.pn[0] << 40);
925 atomic64_set(&key->u.gcmp.tx_pn, pn64);
926 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200927 default:
928 WARN_ON(1);
929 break;
930 }
931}
932EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
933
934void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
935 int tid, struct ieee80211_key_seq *seq)
936{
937 struct ieee80211_key *key;
938 u8 *pn;
939
940 key = container_of(keyconf, struct ieee80211_key, conf);
941
942 switch (key->conf.cipher) {
943 case WLAN_CIPHER_SUITE_TKIP:
944 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
945 return;
946 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
947 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
948 break;
949 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200950 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200951 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
952 return;
953 if (tid < 0)
954 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
955 else
956 pn = key->u.ccmp.rx_pn[tid];
957 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
958 break;
959 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200960 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200961 if (WARN_ON(tid != 0))
962 return;
963 pn = key->u.aes_cmac.rx_pn;
964 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
965 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200966 case WLAN_CIPHER_SUITE_GCMP:
967 case WLAN_CIPHER_SUITE_GCMP_256:
968 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
969 return;
970 if (tid < 0)
971 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
972 else
973 pn = key->u.gcmp.rx_pn[tid];
974 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
975 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200976 default:
977 WARN_ON(1);
978 break;
979 }
980}
981EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
982
983void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
984{
985 struct ieee80211_key *key;
986
987 key = container_of(keyconf, struct ieee80211_key, conf);
988
989 assert_key_lock(key->local);
990
991 /*
992 * if key was uploaded, we assume the driver will/has remove(d)
993 * it, so adjust bookkeeping accordingly
994 */
995 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
996 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
997
Johannes Berg1e359a52015-01-05 10:28:49 +0100998 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500999 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001000 increment_tailroom_need_count(key->sdata);
1001 }
1002
1003 ieee80211_key_free(key, false);
1004}
1005EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1006
1007struct ieee80211_key_conf *
1008ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1009 struct ieee80211_key_conf *keyconf)
1010{
1011 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1012 struct ieee80211_local *local = sdata->local;
1013 struct ieee80211_key *key;
1014 int err;
1015
1016 if (WARN_ON(!local->wowlan))
1017 return ERR_PTR(-EINVAL);
1018
1019 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1020 return ERR_PTR(-EINVAL);
1021
1022 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1023 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001024 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001025 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001026 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001027
1028 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1029 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1030
1031 err = ieee80211_key_link(key, sdata, NULL);
1032 if (err)
1033 return ERR_PTR(err);
1034
1035 return &key->conf;
1036}
1037EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);