blob: 83c61085c3f03d92024471671ad7c341a1887c54 [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 Malinen00b9cfa2015-01-24 19:52:06 +0200168 case WLAN_CIPHER_SUITE_GCMP:
169 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100170 /* all of these we can do in software - if driver can */
171 if (ret == 1)
172 return 0;
173 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
174 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200175 return 0;
176 default:
177 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300178 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400179}
180
181static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
182{
Johannes Bergdc822b52008-12-29 12:55:09 +0100183 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100184 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400185 int ret;
186
Johannes Berg3b967662008-04-08 17:56:52 +0200187 might_sleep();
188
Johannes Bergdb4d1162008-02-25 16:27:45 +0100189 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400190 return;
191
Johannes Bergad0e2b52010-06-01 10:19:19 +0200192 assert_key_lock(key->local);
193
194 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400195 return;
196
Johannes Berg89c91ca2012-01-20 13:55:19 +0100197 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100198 sdata = key->sdata;
199
Johannes Berg1e359a52015-01-05 10:28:49 +0100200 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500201 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530202 increment_tailroom_need_count(sdata);
203
Johannes Berg12375ef2009-11-25 20:30:31 +0100204 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100205 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400206
207 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200208 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700209 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100210 key->conf.keyidx,
211 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400212
Johannes Berg3b967662008-04-08 17:56:52 +0200213 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200214}
215
216static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100217 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200218{
219 struct ieee80211_key *key = NULL;
220
Johannes Bergad0e2b52010-06-01 10:19:19 +0200221 assert_key_lock(sdata->local);
222
Johannes Berg3b967662008-04-08 17:56:52 +0200223 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200224 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200225
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300226 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100227 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300228 drv_set_default_unicast_key(sdata->local, sdata, idx);
229 }
230
Johannes Bergf7e01042010-12-09 19:49:02 +0100231 if (multi)
232 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200233
Johannes Bergf7e01042010-12-09 19:49:02 +0100234 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200235}
236
Johannes Bergf7e01042010-12-09 19:49:02 +0100237void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
238 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200239{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200240 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100241 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200242 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200243}
244
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200245static void
246__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
247{
248 struct ieee80211_key *key = NULL;
249
Johannes Bergad0e2b52010-06-01 10:19:19 +0200250 assert_key_lock(sdata->local);
251
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200252 if (idx >= NUM_DEFAULT_KEYS &&
253 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200254 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200255
256 rcu_assign_pointer(sdata->default_mgmt_key, key);
257
Johannes Bergf7e01042010-12-09 19:49:02 +0100258 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200259}
260
261void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
262 int idx)
263{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200264 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200265 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200266 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200267}
268
Johannes Berg3b967662008-04-08 17:56:52 +0200269
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100270static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
271 struct sta_info *sta,
272 bool pairwise,
273 struct ieee80211_key *old,
274 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200275{
Johannes Bergf7e01042010-12-09 19:49:02 +0100276 int idx;
277 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200278
Johannes Berg5282c3b2013-10-29 10:00:08 +0100279 /* caller must provide at least one old/new */
280 if (WARN_ON(!new && !old))
281 return;
282
Johannes Berg3b967662008-04-08 17:56:52 +0200283 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200284 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200285
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200286 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
287
288 if (old)
289 idx = old->conf.keyidx;
290 else
291 idx = new->conf.keyidx;
292
293 if (sta) {
294 if (pairwise) {
295 rcu_assign_pointer(sta->ptk[idx], new);
296 sta->ptk_idx = idx;
297 } else {
298 rcu_assign_pointer(sta->gtk[idx], new);
299 sta->gtk_idx = idx;
300 }
Johannes Berg3b967662008-04-08 17:56:52 +0200301 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200302 defunikey = old &&
303 old == key_mtx_dereference(sdata->local,
304 sdata->default_unicast_key);
305 defmultikey = old &&
306 old == key_mtx_dereference(sdata->local,
307 sdata->default_multicast_key);
308 defmgmtkey = old &&
309 old == key_mtx_dereference(sdata->local,
310 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200311
Johannes Bergf7e01042010-12-09 19:49:02 +0100312 if (defunikey && !new)
313 __ieee80211_set_default_key(sdata, -1, true, false);
314 if (defmultikey && !new)
315 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200316 if (defmgmtkey && !new)
317 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200318
319 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100320 if (defunikey && new)
321 __ieee80211_set_default_key(sdata, new->conf.keyidx,
322 true, false);
323 if (defmultikey && new)
324 __ieee80211_set_default_key(sdata, new->conf.keyidx,
325 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200326 if (defmgmtkey && new)
327 __ieee80211_set_default_mgmt_key(sdata,
328 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200329 }
330
Johannes Bergb5c34f62011-01-03 19:51:09 +0100331 if (old)
332 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400333}
334
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200335struct ieee80211_key *
336ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
337 const u8 *key_data,
338 size_t seq_len, const u8 *seq,
339 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200340{
341 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100342 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200343
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200344 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
345 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400346
347 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200348 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100349 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400350
351 /*
352 * Default to software encryption; we'll later upload the
353 * key to the hardware if possible.
354 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400355 key->conf.flags = 0;
356 key->flags = 0;
357
Johannes Berg97359d12010-08-10 09:46:38 +0200358 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400359 key->conf.keyidx = idx;
360 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200361 switch (cipher) {
362 case WLAN_CIPHER_SUITE_WEP40:
363 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200364 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
365 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200366 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200367 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200368 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
369 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300370 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100371 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300372 key->u.tkip.rx[i].iv32 =
373 get_unaligned_le32(&seq[2]);
374 key->u.tkip.rx[i].iv16 =
375 get_unaligned_le16(seq);
376 }
377 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200378 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200379 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200380 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200381 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
382 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300383 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100384 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200385 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300386 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200387 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300388 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400389 /*
390 * Initialize AES key state here as an optimization so that
391 * it does not need to be initialized for every packet.
392 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200393 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
394 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
395 if (IS_ERR(key->u.ccmp.tfm)) {
396 err = PTR_ERR(key->u.ccmp.tfm);
397 kfree(key);
398 return ERR_PTR(err);
399 }
400 break;
401 case WLAN_CIPHER_SUITE_CCMP_256:
402 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
403 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
404 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
405 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
406 key->u.ccmp.rx_pn[i][j] =
407 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
408 /* Initialize AES key state here as an optimization so that
409 * it does not need to be initialized for every packet.
410 */
411 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
412 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100413 if (IS_ERR(key->u.ccmp.tfm)) {
414 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200415 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200416 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400417 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200418 break;
419 case WLAN_CIPHER_SUITE_AES_CMAC:
420 key->conf.iv_len = 0;
421 key->conf.icv_len = sizeof(struct ieee80211_mmie);
422 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200423 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100424 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200425 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200426 /*
427 * Initialize AES key state here as an optimization so that
428 * it does not need to be initialized for every packet.
429 */
430 key->u.aes_cmac.tfm =
431 ieee80211_aes_cmac_key_setup(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100432 if (IS_ERR(key->u.aes_cmac.tfm)) {
433 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200434 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200435 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200436 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200437 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200438 case WLAN_CIPHER_SUITE_GCMP:
439 case WLAN_CIPHER_SUITE_GCMP_256:
440 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
441 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
442 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
443 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
444 key->u.gcmp.rx_pn[i][j] =
445 seq[IEEE80211_GCMP_PN_LEN - j - 1];
446 /* Initialize AES key state here as an optimization so that
447 * it does not need to be initialized for every packet.
448 */
449 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
450 key_len);
451 if (IS_ERR(key->u.gcmp.tfm)) {
452 err = PTR_ERR(key->u.gcmp.tfm);
453 kfree(key);
454 return ERR_PTR(err);
455 }
456 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200457 default:
458 if (cs) {
459 size_t len = (seq_len > MAX_PN_LEN) ?
460 MAX_PN_LEN : seq_len;
461
462 key->conf.iv_len = cs->hdr_len;
463 key->conf.icv_len = cs->mic_len;
464 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
465 for (j = 0; j < len; j++)
466 key->u.gen.rx_pn[i][j] =
467 seq[len - j - 1];
468 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200469 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200470 memcpy(key->conf.key, key_data, key_len);
471 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200472
Johannes Bergdb4d1162008-02-25 16:27:45 +0100473 return key;
474}
Johannes Berg11a843b2007-08-28 17:01:55 -0400475
Johannes Berg79cf2df2013-03-06 22:53:52 +0100476static void ieee80211_key_free_common(struct ieee80211_key *key)
477{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200478 switch (key->conf.cipher) {
479 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200480 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100481 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200482 break;
483 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100484 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200485 break;
486 case WLAN_CIPHER_SUITE_GCMP:
487 case WLAN_CIPHER_SUITE_GCMP_256:
488 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
489 break;
490 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300491 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100492}
493
Johannes Berg6d10e462013-03-06 23:09:11 +0100494static void __ieee80211_key_destroy(struct ieee80211_key *key,
495 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200496{
Jouni Malinen32162a42010-07-26 15:52:03 -0700497 if (key->local)
498 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200499
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530500 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100501 struct ieee80211_sub_if_data *sdata = key->sdata;
502
Jouni Malinen32162a42010-07-26 15:52:03 -0700503 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100504
505 if (delay_tailroom) {
506 /* see ieee80211_delayed_tailroom_dec */
507 sdata->crypto_tx_tailroom_pending_dec++;
508 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
509 HZ/2);
510 } else {
511 sdata->crypto_tx_tailroom_needed_cnt--;
512 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530513 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200514
Johannes Berg79cf2df2013-03-06 22:53:52 +0100515 ieee80211_key_free_common(key);
516}
517
Johannes Berg6d10e462013-03-06 23:09:11 +0100518static void ieee80211_key_destroy(struct ieee80211_key *key,
519 bool delay_tailroom)
520{
521 if (!key)
522 return;
523
524 /*
525 * Synchronize so the TX path can no longer be using
526 * this key before we free/remove it.
527 */
528 synchronize_net();
529
530 __ieee80211_key_destroy(key, delay_tailroom);
531}
532
Johannes Berg79cf2df2013-03-06 22:53:52 +0100533void ieee80211_key_free_unused(struct ieee80211_key *key)
534{
535 WARN_ON(key->sdata || key->local);
536 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200537}
538
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300539int ieee80211_key_link(struct ieee80211_key *key,
540 struct ieee80211_sub_if_data *sdata,
541 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100542{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200543 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100544 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300545 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100546 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100547
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100548 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100549 idx = key->conf.keyidx;
550 key->local = sdata->local;
551 key->sdata = sdata;
552 key->sta = sta;
553
Johannes Bergad0e2b52010-06-01 10:19:19 +0200554 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200555
Johannes Berge31b8212010-10-05 19:39:30 +0200556 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200557 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200558 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200559 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400560 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200561 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100562
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530563 increment_tailroom_need_count(sdata);
564
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100565 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
566 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400567
Johannes Bergad0e2b52010-06-01 10:19:19 +0200568 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200569
Johannes Berg27b3eb92013-08-07 20:11:55 +0200570 if (!local->wowlan) {
571 ret = ieee80211_key_enable_hw_accel(key);
572 if (ret)
573 ieee80211_key_free(key, true);
574 } else {
575 ret = 0;
576 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100577
Johannes Bergad0e2b52010-06-01 10:19:19 +0200578 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300579
580 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200581}
582
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100583void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200584{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200585 if (!key)
586 return;
587
Johannes Berg3a245762008-04-09 16:45:37 +0200588 /*
589 * Replace key with nothingness if it was ever used.
590 */
591 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100592 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200593 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
594 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100595 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200596}
597
Johannes Berg3b967662008-04-08 17:56:52 +0200598void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
599{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200600 struct ieee80211_key *key;
601
Johannes Berg3a245762008-04-09 16:45:37 +0200602 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200603
Johannes Berg9607e6b662009-12-23 13:15:31 +0100604 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200605 return;
606
Johannes Bergad0e2b52010-06-01 10:19:19 +0200607 mutex_lock(&sdata->local->key_mtx);
608
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530609 sdata->crypto_tx_tailroom_needed_cnt = 0;
610
611 list_for_each_entry(key, &sdata->key_list, list) {
612 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200613 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530614 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200615
616 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200617}
618
Johannes Berg830af022011-07-05 16:35:39 +0200619void ieee80211_iter_keys(struct ieee80211_hw *hw,
620 struct ieee80211_vif *vif,
621 void (*iter)(struct ieee80211_hw *hw,
622 struct ieee80211_vif *vif,
623 struct ieee80211_sta *sta,
624 struct ieee80211_key_conf *key,
625 void *data),
626 void *iter_data)
627{
628 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200629 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200630 struct ieee80211_sub_if_data *sdata;
631
632 ASSERT_RTNL();
633
634 mutex_lock(&local->key_mtx);
635 if (vif) {
636 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200637 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200638 iter(hw, &sdata->vif,
639 key->sta ? &key->sta->sta : NULL,
640 &key->conf, iter_data);
641 } else {
642 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200643 list_for_each_entry_safe(key, tmp,
644 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200645 iter(hw, &sdata->vif,
646 key->sta ? &key->sta->sta : NULL,
647 &key->conf, iter_data);
648 }
649 mutex_unlock(&local->key_mtx);
650}
651EXPORT_SYMBOL(ieee80211_iter_keys);
652
Johannes Berg7907c7d2013-12-04 23:47:09 +0100653static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
654 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400655{
656 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200657
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100658 sdata->crypto_tx_tailroom_needed_cnt -=
659 sdata->crypto_tx_tailroom_pending_dec;
660 sdata->crypto_tx_tailroom_pending_dec = 0;
661
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200662 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400663
Johannes Berg6d10e462013-03-06 23:09:11 +0100664 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
665 ieee80211_key_replace(key->sdata, key->sta,
666 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
667 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100668 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100669 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400670
Johannes Bergf7e01042010-12-09 19:49:02 +0100671 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100672}
Johannes Bergf7e01042010-12-09 19:49:02 +0100673
Johannes Berg7907c7d2013-12-04 23:47:09 +0100674void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
675 bool force_synchronize)
676{
677 struct ieee80211_local *local = sdata->local;
678 struct ieee80211_sub_if_data *vlan;
679 struct ieee80211_key *key, *tmp;
680 LIST_HEAD(keys);
681
682 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
683
684 mutex_lock(&local->key_mtx);
685
686 ieee80211_free_keys_iface(sdata, &keys);
687
688 if (sdata->vif.type == NL80211_IFTYPE_AP) {
689 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
690 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100691 }
692
Johannes Berg7907c7d2013-12-04 23:47:09 +0100693 if (!list_empty(&keys) || force_synchronize)
694 synchronize_net();
695 list_for_each_entry_safe(key, tmp, &keys, list)
696 __ieee80211_key_destroy(key, false);
697
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100698 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
699 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100700 if (sdata->vif.type == NL80211_IFTYPE_AP) {
701 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
702 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
703 vlan->crypto_tx_tailroom_pending_dec);
704 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100705
Johannes Berg7907c7d2013-12-04 23:47:09 +0100706 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400707}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200708
Johannes Berg6d10e462013-03-06 23:09:11 +0100709void ieee80211_free_sta_keys(struct ieee80211_local *local,
710 struct sta_info *sta)
711{
Johannes Bergc8782072013-12-04 23:05:45 +0100712 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100713 int i;
714
715 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100716 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100717 key = key_mtx_dereference(local, sta->gtk[i]);
718 if (!key)
719 continue;
720 ieee80211_key_replace(key->sdata, key->sta,
721 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
722 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100723 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100724 }
725
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200726 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
727 key = key_mtx_dereference(local, sta->ptk[i]);
728 if (!key)
729 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100730 ieee80211_key_replace(key->sdata, key->sta,
731 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
732 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100733 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100734 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100735
736 mutex_unlock(&local->key_mtx);
737}
738
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100739void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
740{
741 struct ieee80211_sub_if_data *sdata;
742
743 sdata = container_of(wk, struct ieee80211_sub_if_data,
744 dec_tailroom_needed_wk.work);
745
746 /*
747 * The reason for the delayed tailroom needed decrementing is to
748 * make roaming faster: during roaming, all keys are first deleted
749 * and then new keys are installed. The first new key causes the
750 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
751 * the cost of synchronize_net() (which can be slow). Avoid this
752 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
753 * key removal for a while, so if we roam the value is larger than
754 * zero and no 0->1 transition happens.
755 *
756 * The cost is that if the AP switching was from an AP with keys
757 * to one without, we still allocate tailroom while it would no
758 * longer be needed. However, in the typical (fast) roaming case
759 * within an ESS this usually won't happen.
760 */
761
762 mutex_lock(&sdata->local->key_mtx);
763 sdata->crypto_tx_tailroom_needed_cnt -=
764 sdata->crypto_tx_tailroom_pending_dec;
765 sdata->crypto_tx_tailroom_pending_dec = 0;
766 mutex_unlock(&sdata->local->key_mtx);
767}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200768
769void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
770 const u8 *replay_ctr, gfp_t gfp)
771{
772 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
773
774 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
775
776 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
777}
778EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200779
780void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
781 struct ieee80211_key_seq *seq)
782{
783 struct ieee80211_key *key;
784 u64 pn64;
785
786 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
787 return;
788
789 key = container_of(keyconf, struct ieee80211_key, conf);
790
791 switch (key->conf.cipher) {
792 case WLAN_CIPHER_SUITE_TKIP:
793 seq->tkip.iv32 = key->u.tkip.tx.iv32;
794 seq->tkip.iv16 = key->u.tkip.tx.iv16;
795 break;
796 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200797 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200798 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
799 seq->ccmp.pn[5] = pn64;
800 seq->ccmp.pn[4] = pn64 >> 8;
801 seq->ccmp.pn[3] = pn64 >> 16;
802 seq->ccmp.pn[2] = pn64 >> 24;
803 seq->ccmp.pn[1] = pn64 >> 32;
804 seq->ccmp.pn[0] = pn64 >> 40;
805 break;
806 case WLAN_CIPHER_SUITE_AES_CMAC:
807 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
808 seq->ccmp.pn[5] = pn64;
809 seq->ccmp.pn[4] = pn64 >> 8;
810 seq->ccmp.pn[3] = pn64 >> 16;
811 seq->ccmp.pn[2] = pn64 >> 24;
812 seq->ccmp.pn[1] = pn64 >> 32;
813 seq->ccmp.pn[0] = pn64 >> 40;
814 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200815 case WLAN_CIPHER_SUITE_GCMP:
816 case WLAN_CIPHER_SUITE_GCMP_256:
817 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
818 seq->gcmp.pn[5] = pn64;
819 seq->gcmp.pn[4] = pn64 >> 8;
820 seq->gcmp.pn[3] = pn64 >> 16;
821 seq->gcmp.pn[2] = pn64 >> 24;
822 seq->gcmp.pn[1] = pn64 >> 32;
823 seq->gcmp.pn[0] = pn64 >> 40;
824 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200825 default:
826 WARN_ON(1);
827 }
828}
829EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
830
831void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
832 int tid, struct ieee80211_key_seq *seq)
833{
834 struct ieee80211_key *key;
835 const u8 *pn;
836
837 key = container_of(keyconf, struct ieee80211_key, conf);
838
839 switch (key->conf.cipher) {
840 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100841 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200842 return;
843 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
844 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
845 break;
846 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200847 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +0100848 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200849 return;
850 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100851 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200852 else
853 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200854 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200855 break;
856 case WLAN_CIPHER_SUITE_AES_CMAC:
857 if (WARN_ON(tid != 0))
858 return;
859 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200860 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200861 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200862 case WLAN_CIPHER_SUITE_GCMP:
863 case WLAN_CIPHER_SUITE_GCMP_256:
864 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
865 return;
866 if (tid < 0)
867 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
868 else
869 pn = key->u.gcmp.rx_pn[tid];
870 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
871 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200872 }
873}
874EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200875
876void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
877 struct ieee80211_key_seq *seq)
878{
879 struct ieee80211_key *key;
880 u64 pn64;
881
882 key = container_of(keyconf, struct ieee80211_key, conf);
883
884 switch (key->conf.cipher) {
885 case WLAN_CIPHER_SUITE_TKIP:
886 key->u.tkip.tx.iv32 = seq->tkip.iv32;
887 key->u.tkip.tx.iv16 = seq->tkip.iv16;
888 break;
889 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200890 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200891 pn64 = (u64)seq->ccmp.pn[5] |
892 ((u64)seq->ccmp.pn[4] << 8) |
893 ((u64)seq->ccmp.pn[3] << 16) |
894 ((u64)seq->ccmp.pn[2] << 24) |
895 ((u64)seq->ccmp.pn[1] << 32) |
896 ((u64)seq->ccmp.pn[0] << 40);
897 atomic64_set(&key->u.ccmp.tx_pn, pn64);
898 break;
899 case WLAN_CIPHER_SUITE_AES_CMAC:
900 pn64 = (u64)seq->aes_cmac.pn[5] |
901 ((u64)seq->aes_cmac.pn[4] << 8) |
902 ((u64)seq->aes_cmac.pn[3] << 16) |
903 ((u64)seq->aes_cmac.pn[2] << 24) |
904 ((u64)seq->aes_cmac.pn[1] << 32) |
905 ((u64)seq->aes_cmac.pn[0] << 40);
906 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
907 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200908 case WLAN_CIPHER_SUITE_GCMP:
909 case WLAN_CIPHER_SUITE_GCMP_256:
910 pn64 = (u64)seq->gcmp.pn[5] |
911 ((u64)seq->gcmp.pn[4] << 8) |
912 ((u64)seq->gcmp.pn[3] << 16) |
913 ((u64)seq->gcmp.pn[2] << 24) |
914 ((u64)seq->gcmp.pn[1] << 32) |
915 ((u64)seq->gcmp.pn[0] << 40);
916 atomic64_set(&key->u.gcmp.tx_pn, pn64);
917 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200918 default:
919 WARN_ON(1);
920 break;
921 }
922}
923EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
924
925void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
926 int tid, struct ieee80211_key_seq *seq)
927{
928 struct ieee80211_key *key;
929 u8 *pn;
930
931 key = container_of(keyconf, struct ieee80211_key, conf);
932
933 switch (key->conf.cipher) {
934 case WLAN_CIPHER_SUITE_TKIP:
935 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
936 return;
937 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
938 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
939 break;
940 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200941 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200942 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
943 return;
944 if (tid < 0)
945 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
946 else
947 pn = key->u.ccmp.rx_pn[tid];
948 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
949 break;
950 case WLAN_CIPHER_SUITE_AES_CMAC:
951 if (WARN_ON(tid != 0))
952 return;
953 pn = key->u.aes_cmac.rx_pn;
954 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
955 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200956 case WLAN_CIPHER_SUITE_GCMP:
957 case WLAN_CIPHER_SUITE_GCMP_256:
958 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
959 return;
960 if (tid < 0)
961 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
962 else
963 pn = key->u.gcmp.rx_pn[tid];
964 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
965 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200966 default:
967 WARN_ON(1);
968 break;
969 }
970}
971EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
972
973void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
974{
975 struct ieee80211_key *key;
976
977 key = container_of(keyconf, struct ieee80211_key, conf);
978
979 assert_key_lock(key->local);
980
981 /*
982 * if key was uploaded, we assume the driver will/has remove(d)
983 * it, so adjust bookkeeping accordingly
984 */
985 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
986 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
987
Johannes Berg1e359a52015-01-05 10:28:49 +0100988 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500989 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +0200990 increment_tailroom_need_count(key->sdata);
991 }
992
993 ieee80211_key_free(key, false);
994}
995EXPORT_SYMBOL_GPL(ieee80211_remove_key);
996
997struct ieee80211_key_conf *
998ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
999 struct ieee80211_key_conf *keyconf)
1000{
1001 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1002 struct ieee80211_local *local = sdata->local;
1003 struct ieee80211_key *key;
1004 int err;
1005
1006 if (WARN_ON(!local->wowlan))
1007 return ERR_PTR(-EINVAL);
1008
1009 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1010 return ERR_PTR(-EINVAL);
1011
1012 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1013 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001014 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001015 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001016 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001017
1018 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1019 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1020
1021 err = ieee80211_key_link(key, sdata, NULL);
1022 if (err)
1023 return ERR_PTR(err);
1024
1025 return &key->conf;
1026}
1027EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);