blob: cbee2f5180cef7f5b8d5e4959e1ea4f02996b15c [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:
166 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200167 case WLAN_CIPHER_SUITE_GCMP:
168 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100169 /* all of these we can do in software - if driver can */
170 if (ret == 1)
171 return 0;
172 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
173 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200174 return 0;
175 default:
176 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300177 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400178}
179
180static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
181{
Johannes Bergdc822b52008-12-29 12:55:09 +0100182 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100183 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400184 int ret;
185
Johannes Berg3b967662008-04-08 17:56:52 +0200186 might_sleep();
187
Johannes Bergdb4d1162008-02-25 16:27:45 +0100188 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400189 return;
190
Johannes Bergad0e2b52010-06-01 10:19:19 +0200191 assert_key_lock(key->local);
192
193 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400194 return;
195
Johannes Berg89c91ca2012-01-20 13:55:19 +0100196 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100197 sdata = key->sdata;
198
Johannes Berg1e359a52015-01-05 10:28:49 +0100199 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500200 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530201 increment_tailroom_need_count(sdata);
202
Johannes Berg12375ef2009-11-25 20:30:31 +0100203 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100204 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400205
206 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200207 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700208 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100209 key->conf.keyidx,
210 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400211
Johannes Berg3b967662008-04-08 17:56:52 +0200212 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200213}
214
215static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100216 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200217{
218 struct ieee80211_key *key = NULL;
219
Johannes Bergad0e2b52010-06-01 10:19:19 +0200220 assert_key_lock(sdata->local);
221
Johannes Berg3b967662008-04-08 17:56:52 +0200222 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200223 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200224
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300225 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100226 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300227 drv_set_default_unicast_key(sdata->local, sdata, idx);
228 }
229
Johannes Bergf7e01042010-12-09 19:49:02 +0100230 if (multi)
231 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200232
Johannes Bergf7e01042010-12-09 19:49:02 +0100233 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200234}
235
Johannes Bergf7e01042010-12-09 19:49:02 +0100236void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
237 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200238{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200239 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100240 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200241 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200242}
243
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200244static void
245__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
246{
247 struct ieee80211_key *key = NULL;
248
Johannes Bergad0e2b52010-06-01 10:19:19 +0200249 assert_key_lock(sdata->local);
250
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200251 if (idx >= NUM_DEFAULT_KEYS &&
252 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200253 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200254
255 rcu_assign_pointer(sdata->default_mgmt_key, key);
256
Johannes Bergf7e01042010-12-09 19:49:02 +0100257 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200258}
259
260void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
261 int idx)
262{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200263 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200264 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200265 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200266}
267
Johannes Berg3b967662008-04-08 17:56:52 +0200268
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100269static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
270 struct sta_info *sta,
271 bool pairwise,
272 struct ieee80211_key *old,
273 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200274{
Johannes Bergf7e01042010-12-09 19:49:02 +0100275 int idx;
276 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200277
Johannes Berg5282c3b2013-10-29 10:00:08 +0100278 /* caller must provide at least one old/new */
279 if (WARN_ON(!new && !old))
280 return;
281
Johannes Berg3b967662008-04-08 17:56:52 +0200282 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200283 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200284
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200285 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
286
287 if (old)
288 idx = old->conf.keyidx;
289 else
290 idx = new->conf.keyidx;
291
292 if (sta) {
293 if (pairwise) {
294 rcu_assign_pointer(sta->ptk[idx], new);
295 sta->ptk_idx = idx;
296 } else {
297 rcu_assign_pointer(sta->gtk[idx], new);
298 sta->gtk_idx = idx;
299 }
Johannes Berg3b967662008-04-08 17:56:52 +0200300 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200301 defunikey = old &&
302 old == key_mtx_dereference(sdata->local,
303 sdata->default_unicast_key);
304 defmultikey = old &&
305 old == key_mtx_dereference(sdata->local,
306 sdata->default_multicast_key);
307 defmgmtkey = old &&
308 old == key_mtx_dereference(sdata->local,
309 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200310
Johannes Bergf7e01042010-12-09 19:49:02 +0100311 if (defunikey && !new)
312 __ieee80211_set_default_key(sdata, -1, true, false);
313 if (defmultikey && !new)
314 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200315 if (defmgmtkey && !new)
316 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200317
318 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100319 if (defunikey && new)
320 __ieee80211_set_default_key(sdata, new->conf.keyidx,
321 true, false);
322 if (defmultikey && new)
323 __ieee80211_set_default_key(sdata, new->conf.keyidx,
324 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200325 if (defmgmtkey && new)
326 __ieee80211_set_default_mgmt_key(sdata,
327 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200328 }
329
Johannes Bergb5c34f62011-01-03 19:51:09 +0100330 if (old)
331 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400332}
333
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200334struct ieee80211_key *
335ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
336 const u8 *key_data,
337 size_t seq_len, const u8 *seq,
338 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200339{
340 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100341 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200342
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200343 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
344 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400345
346 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200347 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100348 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400349
350 /*
351 * Default to software encryption; we'll later upload the
352 * key to the hardware if possible.
353 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400354 key->conf.flags = 0;
355 key->flags = 0;
356
Johannes Berg97359d12010-08-10 09:46:38 +0200357 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400358 key->conf.keyidx = idx;
359 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200360 switch (cipher) {
361 case WLAN_CIPHER_SUITE_WEP40:
362 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200363 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
364 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200365 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200366 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200367 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
368 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300369 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100370 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300371 key->u.tkip.rx[i].iv32 =
372 get_unaligned_le32(&seq[2]);
373 key->u.tkip.rx[i].iv16 =
374 get_unaligned_le16(seq);
375 }
376 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200377 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200378 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200379 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200380 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
381 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300382 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100383 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200384 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300385 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200386 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300387 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400388 /*
389 * Initialize AES key state here as an optimization so that
390 * it does not need to be initialized for every packet.
391 */
392 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100393 if (IS_ERR(key->u.ccmp.tfm)) {
394 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200395 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200396 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400397 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200398 break;
399 case WLAN_CIPHER_SUITE_AES_CMAC:
400 key->conf.iv_len = 0;
401 key->conf.icv_len = sizeof(struct ieee80211_mmie);
402 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200403 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100404 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200405 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200406 /*
407 * Initialize AES key state here as an optimization so that
408 * it does not need to be initialized for every packet.
409 */
410 key->u.aes_cmac.tfm =
411 ieee80211_aes_cmac_key_setup(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100412 if (IS_ERR(key->u.aes_cmac.tfm)) {
413 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200414 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200415 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200416 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200417 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200418 case WLAN_CIPHER_SUITE_GCMP:
419 case WLAN_CIPHER_SUITE_GCMP_256:
420 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
421 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
422 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
423 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
424 key->u.gcmp.rx_pn[i][j] =
425 seq[IEEE80211_GCMP_PN_LEN - j - 1];
426 /* Initialize AES key state here as an optimization so that
427 * it does not need to be initialized for every packet.
428 */
429 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
430 key_len);
431 if (IS_ERR(key->u.gcmp.tfm)) {
432 err = PTR_ERR(key->u.gcmp.tfm);
433 kfree(key);
434 return ERR_PTR(err);
435 }
436 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200437 default:
438 if (cs) {
439 size_t len = (seq_len > MAX_PN_LEN) ?
440 MAX_PN_LEN : seq_len;
441
442 key->conf.iv_len = cs->hdr_len;
443 key->conf.icv_len = cs->mic_len;
444 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
445 for (j = 0; j < len; j++)
446 key->u.gen.rx_pn[i][j] =
447 seq[len - j - 1];
448 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200449 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200450 memcpy(key->conf.key, key_data, key_len);
451 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200452
Johannes Bergdb4d1162008-02-25 16:27:45 +0100453 return key;
454}
Johannes Berg11a843b2007-08-28 17:01:55 -0400455
Johannes Berg79cf2df2013-03-06 22:53:52 +0100456static void ieee80211_key_free_common(struct ieee80211_key *key)
457{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200458 switch (key->conf.cipher) {
459 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100460 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200461 break;
462 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100463 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200464 break;
465 case WLAN_CIPHER_SUITE_GCMP:
466 case WLAN_CIPHER_SUITE_GCMP_256:
467 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
468 break;
469 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300470 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100471}
472
Johannes Berg6d10e462013-03-06 23:09:11 +0100473static void __ieee80211_key_destroy(struct ieee80211_key *key,
474 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200475{
Jouni Malinen32162a42010-07-26 15:52:03 -0700476 if (key->local)
477 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200478
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530479 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100480 struct ieee80211_sub_if_data *sdata = key->sdata;
481
Jouni Malinen32162a42010-07-26 15:52:03 -0700482 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100483
484 if (delay_tailroom) {
485 /* see ieee80211_delayed_tailroom_dec */
486 sdata->crypto_tx_tailroom_pending_dec++;
487 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
488 HZ/2);
489 } else {
490 sdata->crypto_tx_tailroom_needed_cnt--;
491 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530492 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200493
Johannes Berg79cf2df2013-03-06 22:53:52 +0100494 ieee80211_key_free_common(key);
495}
496
Johannes Berg6d10e462013-03-06 23:09:11 +0100497static void ieee80211_key_destroy(struct ieee80211_key *key,
498 bool delay_tailroom)
499{
500 if (!key)
501 return;
502
503 /*
504 * Synchronize so the TX path can no longer be using
505 * this key before we free/remove it.
506 */
507 synchronize_net();
508
509 __ieee80211_key_destroy(key, delay_tailroom);
510}
511
Johannes Berg79cf2df2013-03-06 22:53:52 +0100512void ieee80211_key_free_unused(struct ieee80211_key *key)
513{
514 WARN_ON(key->sdata || key->local);
515 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200516}
517
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300518int ieee80211_key_link(struct ieee80211_key *key,
519 struct ieee80211_sub_if_data *sdata,
520 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100521{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200522 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100523 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300524 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100525 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100526
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100527 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100528 idx = key->conf.keyidx;
529 key->local = sdata->local;
530 key->sdata = sdata;
531 key->sta = sta;
532
Johannes Bergad0e2b52010-06-01 10:19:19 +0200533 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200534
Johannes Berge31b8212010-10-05 19:39:30 +0200535 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200536 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200537 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200538 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400539 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200540 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100541
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530542 increment_tailroom_need_count(sdata);
543
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100544 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
545 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400546
Johannes Bergad0e2b52010-06-01 10:19:19 +0200547 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200548
Johannes Berg27b3eb92013-08-07 20:11:55 +0200549 if (!local->wowlan) {
550 ret = ieee80211_key_enable_hw_accel(key);
551 if (ret)
552 ieee80211_key_free(key, true);
553 } else {
554 ret = 0;
555 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100556
Johannes Bergad0e2b52010-06-01 10:19:19 +0200557 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300558
559 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200560}
561
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100562void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200563{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200564 if (!key)
565 return;
566
Johannes Berg3a245762008-04-09 16:45:37 +0200567 /*
568 * Replace key with nothingness if it was ever used.
569 */
570 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100571 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200572 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
573 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100574 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200575}
576
Johannes Berg3b967662008-04-08 17:56:52 +0200577void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
578{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200579 struct ieee80211_key *key;
580
Johannes Berg3a245762008-04-09 16:45:37 +0200581 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200582
Johannes Berg9607e6b662009-12-23 13:15:31 +0100583 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200584 return;
585
Johannes Bergad0e2b52010-06-01 10:19:19 +0200586 mutex_lock(&sdata->local->key_mtx);
587
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530588 sdata->crypto_tx_tailroom_needed_cnt = 0;
589
590 list_for_each_entry(key, &sdata->key_list, list) {
591 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200592 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530593 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200594
595 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200596}
597
Johannes Berg830af022011-07-05 16:35:39 +0200598void ieee80211_iter_keys(struct ieee80211_hw *hw,
599 struct ieee80211_vif *vif,
600 void (*iter)(struct ieee80211_hw *hw,
601 struct ieee80211_vif *vif,
602 struct ieee80211_sta *sta,
603 struct ieee80211_key_conf *key,
604 void *data),
605 void *iter_data)
606{
607 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200608 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200609 struct ieee80211_sub_if_data *sdata;
610
611 ASSERT_RTNL();
612
613 mutex_lock(&local->key_mtx);
614 if (vif) {
615 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200616 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200617 iter(hw, &sdata->vif,
618 key->sta ? &key->sta->sta : NULL,
619 &key->conf, iter_data);
620 } else {
621 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200622 list_for_each_entry_safe(key, tmp,
623 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200624 iter(hw, &sdata->vif,
625 key->sta ? &key->sta->sta : NULL,
626 &key->conf, iter_data);
627 }
628 mutex_unlock(&local->key_mtx);
629}
630EXPORT_SYMBOL(ieee80211_iter_keys);
631
Johannes Berg7907c7d2013-12-04 23:47:09 +0100632static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
633 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400634{
635 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200636
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100637 sdata->crypto_tx_tailroom_needed_cnt -=
638 sdata->crypto_tx_tailroom_pending_dec;
639 sdata->crypto_tx_tailroom_pending_dec = 0;
640
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200641 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400642
Johannes Berg6d10e462013-03-06 23:09:11 +0100643 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
644 ieee80211_key_replace(key->sdata, key->sta,
645 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
646 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100647 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100648 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400649
Johannes Bergf7e01042010-12-09 19:49:02 +0100650 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100651}
Johannes Bergf7e01042010-12-09 19:49:02 +0100652
Johannes Berg7907c7d2013-12-04 23:47:09 +0100653void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
654 bool force_synchronize)
655{
656 struct ieee80211_local *local = sdata->local;
657 struct ieee80211_sub_if_data *vlan;
658 struct ieee80211_key *key, *tmp;
659 LIST_HEAD(keys);
660
661 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
662
663 mutex_lock(&local->key_mtx);
664
665 ieee80211_free_keys_iface(sdata, &keys);
666
667 if (sdata->vif.type == NL80211_IFTYPE_AP) {
668 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
669 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100670 }
671
Johannes Berg7907c7d2013-12-04 23:47:09 +0100672 if (!list_empty(&keys) || force_synchronize)
673 synchronize_net();
674 list_for_each_entry_safe(key, tmp, &keys, list)
675 __ieee80211_key_destroy(key, false);
676
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100677 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
678 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100679 if (sdata->vif.type == NL80211_IFTYPE_AP) {
680 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
681 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
682 vlan->crypto_tx_tailroom_pending_dec);
683 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100684
Johannes Berg7907c7d2013-12-04 23:47:09 +0100685 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400686}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200687
Johannes Berg6d10e462013-03-06 23:09:11 +0100688void ieee80211_free_sta_keys(struct ieee80211_local *local,
689 struct sta_info *sta)
690{
Johannes Bergc8782072013-12-04 23:05:45 +0100691 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100692 int i;
693
694 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100695 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100696 key = key_mtx_dereference(local, sta->gtk[i]);
697 if (!key)
698 continue;
699 ieee80211_key_replace(key->sdata, key->sta,
700 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
701 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100702 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100703 }
704
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200705 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
706 key = key_mtx_dereference(local, sta->ptk[i]);
707 if (!key)
708 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100709 ieee80211_key_replace(key->sdata, key->sta,
710 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
711 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100712 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100713 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100714
715 mutex_unlock(&local->key_mtx);
716}
717
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100718void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
719{
720 struct ieee80211_sub_if_data *sdata;
721
722 sdata = container_of(wk, struct ieee80211_sub_if_data,
723 dec_tailroom_needed_wk.work);
724
725 /*
726 * The reason for the delayed tailroom needed decrementing is to
727 * make roaming faster: during roaming, all keys are first deleted
728 * and then new keys are installed. The first new key causes the
729 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
730 * the cost of synchronize_net() (which can be slow). Avoid this
731 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
732 * key removal for a while, so if we roam the value is larger than
733 * zero and no 0->1 transition happens.
734 *
735 * The cost is that if the AP switching was from an AP with keys
736 * to one without, we still allocate tailroom while it would no
737 * longer be needed. However, in the typical (fast) roaming case
738 * within an ESS this usually won't happen.
739 */
740
741 mutex_lock(&sdata->local->key_mtx);
742 sdata->crypto_tx_tailroom_needed_cnt -=
743 sdata->crypto_tx_tailroom_pending_dec;
744 sdata->crypto_tx_tailroom_pending_dec = 0;
745 mutex_unlock(&sdata->local->key_mtx);
746}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200747
748void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
749 const u8 *replay_ctr, gfp_t gfp)
750{
751 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
752
753 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
754
755 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
756}
757EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200758
759void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
760 struct ieee80211_key_seq *seq)
761{
762 struct ieee80211_key *key;
763 u64 pn64;
764
765 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
766 return;
767
768 key = container_of(keyconf, struct ieee80211_key, conf);
769
770 switch (key->conf.cipher) {
771 case WLAN_CIPHER_SUITE_TKIP:
772 seq->tkip.iv32 = key->u.tkip.tx.iv32;
773 seq->tkip.iv16 = key->u.tkip.tx.iv16;
774 break;
775 case WLAN_CIPHER_SUITE_CCMP:
776 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
777 seq->ccmp.pn[5] = pn64;
778 seq->ccmp.pn[4] = pn64 >> 8;
779 seq->ccmp.pn[3] = pn64 >> 16;
780 seq->ccmp.pn[2] = pn64 >> 24;
781 seq->ccmp.pn[1] = pn64 >> 32;
782 seq->ccmp.pn[0] = pn64 >> 40;
783 break;
784 case WLAN_CIPHER_SUITE_AES_CMAC:
785 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
786 seq->ccmp.pn[5] = pn64;
787 seq->ccmp.pn[4] = pn64 >> 8;
788 seq->ccmp.pn[3] = pn64 >> 16;
789 seq->ccmp.pn[2] = pn64 >> 24;
790 seq->ccmp.pn[1] = pn64 >> 32;
791 seq->ccmp.pn[0] = pn64 >> 40;
792 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200793 case WLAN_CIPHER_SUITE_GCMP:
794 case WLAN_CIPHER_SUITE_GCMP_256:
795 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
796 seq->gcmp.pn[5] = pn64;
797 seq->gcmp.pn[4] = pn64 >> 8;
798 seq->gcmp.pn[3] = pn64 >> 16;
799 seq->gcmp.pn[2] = pn64 >> 24;
800 seq->gcmp.pn[1] = pn64 >> 32;
801 seq->gcmp.pn[0] = pn64 >> 40;
802 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200803 default:
804 WARN_ON(1);
805 }
806}
807EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
808
809void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
810 int tid, struct ieee80211_key_seq *seq)
811{
812 struct ieee80211_key *key;
813 const u8 *pn;
814
815 key = container_of(keyconf, struct ieee80211_key, conf);
816
817 switch (key->conf.cipher) {
818 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100819 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200820 return;
821 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
822 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
823 break;
824 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100825 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200826 return;
827 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100828 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200829 else
830 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200831 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200832 break;
833 case WLAN_CIPHER_SUITE_AES_CMAC:
834 if (WARN_ON(tid != 0))
835 return;
836 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200837 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200838 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200839 case WLAN_CIPHER_SUITE_GCMP:
840 case WLAN_CIPHER_SUITE_GCMP_256:
841 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
842 return;
843 if (tid < 0)
844 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
845 else
846 pn = key->u.gcmp.rx_pn[tid];
847 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
848 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200849 }
850}
851EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200852
853void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
854 struct ieee80211_key_seq *seq)
855{
856 struct ieee80211_key *key;
857 u64 pn64;
858
859 key = container_of(keyconf, struct ieee80211_key, conf);
860
861 switch (key->conf.cipher) {
862 case WLAN_CIPHER_SUITE_TKIP:
863 key->u.tkip.tx.iv32 = seq->tkip.iv32;
864 key->u.tkip.tx.iv16 = seq->tkip.iv16;
865 break;
866 case WLAN_CIPHER_SUITE_CCMP:
867 pn64 = (u64)seq->ccmp.pn[5] |
868 ((u64)seq->ccmp.pn[4] << 8) |
869 ((u64)seq->ccmp.pn[3] << 16) |
870 ((u64)seq->ccmp.pn[2] << 24) |
871 ((u64)seq->ccmp.pn[1] << 32) |
872 ((u64)seq->ccmp.pn[0] << 40);
873 atomic64_set(&key->u.ccmp.tx_pn, pn64);
874 break;
875 case WLAN_CIPHER_SUITE_AES_CMAC:
876 pn64 = (u64)seq->aes_cmac.pn[5] |
877 ((u64)seq->aes_cmac.pn[4] << 8) |
878 ((u64)seq->aes_cmac.pn[3] << 16) |
879 ((u64)seq->aes_cmac.pn[2] << 24) |
880 ((u64)seq->aes_cmac.pn[1] << 32) |
881 ((u64)seq->aes_cmac.pn[0] << 40);
882 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
883 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200884 case WLAN_CIPHER_SUITE_GCMP:
885 case WLAN_CIPHER_SUITE_GCMP_256:
886 pn64 = (u64)seq->gcmp.pn[5] |
887 ((u64)seq->gcmp.pn[4] << 8) |
888 ((u64)seq->gcmp.pn[3] << 16) |
889 ((u64)seq->gcmp.pn[2] << 24) |
890 ((u64)seq->gcmp.pn[1] << 32) |
891 ((u64)seq->gcmp.pn[0] << 40);
892 atomic64_set(&key->u.gcmp.tx_pn, pn64);
893 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200894 default:
895 WARN_ON(1);
896 break;
897 }
898}
899EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
900
901void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
902 int tid, struct ieee80211_key_seq *seq)
903{
904 struct ieee80211_key *key;
905 u8 *pn;
906
907 key = container_of(keyconf, struct ieee80211_key, conf);
908
909 switch (key->conf.cipher) {
910 case WLAN_CIPHER_SUITE_TKIP:
911 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
912 return;
913 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
914 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
915 break;
916 case WLAN_CIPHER_SUITE_CCMP:
917 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
918 return;
919 if (tid < 0)
920 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
921 else
922 pn = key->u.ccmp.rx_pn[tid];
923 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
924 break;
925 case WLAN_CIPHER_SUITE_AES_CMAC:
926 if (WARN_ON(tid != 0))
927 return;
928 pn = key->u.aes_cmac.rx_pn;
929 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
930 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200931 case WLAN_CIPHER_SUITE_GCMP:
932 case WLAN_CIPHER_SUITE_GCMP_256:
933 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
934 return;
935 if (tid < 0)
936 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
937 else
938 pn = key->u.gcmp.rx_pn[tid];
939 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
940 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200941 default:
942 WARN_ON(1);
943 break;
944 }
945}
946EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
947
948void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
949{
950 struct ieee80211_key *key;
951
952 key = container_of(keyconf, struct ieee80211_key, conf);
953
954 assert_key_lock(key->local);
955
956 /*
957 * if key was uploaded, we assume the driver will/has remove(d)
958 * it, so adjust bookkeeping accordingly
959 */
960 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
961 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
962
Johannes Berg1e359a52015-01-05 10:28:49 +0100963 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500964 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +0200965 increment_tailroom_need_count(key->sdata);
966 }
967
968 ieee80211_key_free(key, false);
969}
970EXPORT_SYMBOL_GPL(ieee80211_remove_key);
971
972struct ieee80211_key_conf *
973ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
974 struct ieee80211_key_conf *keyconf)
975{
976 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
977 struct ieee80211_local *local = sdata->local;
978 struct ieee80211_key *key;
979 int err;
980
981 if (WARN_ON(!local->wowlan))
982 return ERR_PTR(-EINVAL);
983
984 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
985 return ERR_PTR(-EINVAL);
986
987 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
988 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200989 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200990 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +0200991 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200992
993 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
994 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
995
996 err = ieee80211_key_link(key, sdata, NULL);
997 if (err)
998 return ERR_PTR(err);
999
1000 return &key->conf;
1001}
1002EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);