blob: 2291cd7300911514db84c0135369b807e93a9d06 [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 Malinen8ade5382015-01-24 19:52:09 +020027#include "aes_gmac.h"
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020028#include "aes_gcm.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020029
Johannes Berg11a843b2007-08-28 17:01:55 -040030
Johannes Bergdbbea672008-02-26 14:34:06 +010031/**
32 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040033 *
34 * Key handling in mac80211 is done based on per-interface (sub_if_data)
35 * keys and per-station keys. Since each station belongs to an interface,
36 * each station key also belongs to that interface.
37 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010038 * Hardware acceleration is done on a best-effort basis for algorithms
39 * that are implemented in software, for each key the hardware is asked
40 * to enable that key for offloading but if it cannot do that the key is
41 * simply kept for software encryption (unless it is for an algorithm
42 * that isn't implemented in software).
43 * There is currently no way of knowing whether a key is handled in SW
44 * or HW except by looking into debugfs.
Johannes Berg11a843b2007-08-28 17:01:55 -040045 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010046 * All key management is internally protected by a mutex. Within all
47 * other parts of mac80211, key references are, just as STA structure
48 * references, protected by RCU. Note, however, that some things are
49 * unprotected, namely the key->sta dereferences within the hardware
50 * acceleration functions. This means that sta_info_destroy() must
51 * remove the key which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040052 */
53
54static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040055
Johannes Bergad0e2b52010-06-01 10:19:19 +020056static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020057{
Johannes Berg46a5eba2010-09-15 13:28:15 +020058 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020059}
60
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053061static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
62{
63 /*
64 * When this count is zero, SKB resizing for allocating tailroom
65 * for IV or MMIC is skipped. But, this check has created two race
66 * cases in xmit path while transiting from zero count to one:
67 *
68 * 1. SKB resize was skipped because no key was added but just before
69 * the xmit key is added and SW encryption kicks off.
70 *
71 * 2. SKB resize was skipped because all the keys were hw planted but
72 * just before xmit one of the key is deleted and SW encryption kicks
73 * off.
74 *
75 * In both the above case SW encryption will find not enough space for
76 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
77 *
78 * Solution has been explained at
79 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
80 */
81
82 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
83 /*
84 * Flush all XMIT packets currently using HW encryption or no
85 * encryption at all if the count transition is from 0 -> 1.
86 */
87 synchronize_net();
88 }
89}
90
Johannes Berg3ffc2a92010-08-27 14:26:52 +030091static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040092{
Johannes Bergdc822b52008-12-29 12:55:09 +010093 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +010094 struct sta_info *sta;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +010095 int ret = -EOPNOTSUPP;
Johannes Berg11a843b2007-08-28 17:01:55 -040096
Johannes Berg3b967662008-04-08 17:56:52 +020097 might_sleep();
98
Johannes Berg46191942014-10-13 13:43:29 +020099 if (key->flags & KEY_FLAG_TAINTED) {
100 /* If we get here, it's during resume and the key is
101 * tainted so shouldn't be used/programmed any more.
102 * However, its flags may still indicate that it was
103 * programmed into the device (since we're in resume)
104 * so clear that flag now to avoid trying to remove
105 * it again later.
106 */
107 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200108 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200109 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200110
Johannes Berge31b8212010-10-05 19:39:30 +0200111 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300112 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400113
Johannes Bergad0e2b52010-06-01 10:19:19 +0200114 assert_key_lock(key->local);
115
Johannes Berg89c91ca2012-01-20 13:55:19 +0100116 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100117
Johannes Berge31b8212010-10-05 19:39:30 +0200118 /*
119 * If this is a per-STA GTK, check if it
120 * is supported; if not, return.
121 */
122 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
123 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
124 goto out_unsupported;
125
Johannes Berg89c91ca2012-01-20 13:55:19 +0100126 if (sta && !sta->uploaded)
127 goto out_unsupported;
128
Johannes Bergdc822b52008-12-29 12:55:09 +0100129 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +0100130 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
131 /*
132 * The driver doesn't know anything about VLAN interfaces.
133 * Hence, don't send GTKs for VLAN interfaces to the driver.
134 */
135 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
136 goto out_unsupported;
Helmut Schaa18890d42010-11-19 08:11:01 +0100137 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400138
Johannes Berg89c91ca2012-01-20 13:55:19 +0100139 ret = drv_set_key(key->local, SET_KEY, sdata,
140 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400141
Johannes Berge31b8212010-10-05 19:39:30 +0200142 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400143 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530144
Johannes Berg1e359a52015-01-05 10:28:49 +0100145 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500146 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530147 sdata->crypto_tx_tailroom_needed_cnt--;
148
Arik Nemtsov077a9152011-10-23 08:21:41 +0200149 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
150 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
151
Johannes Berge31b8212010-10-05 19:39:30 +0200152 return 0;
153 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400154
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100155 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200156 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700157 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100158 key->conf.keyidx,
159 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300160
Johannes Berge31b8212010-10-05 19:39:30 +0200161 out_unsupported:
162 switch (key->conf.cipher) {
163 case WLAN_CIPHER_SUITE_WEP40:
164 case WLAN_CIPHER_SUITE_WEP104:
165 case WLAN_CIPHER_SUITE_TKIP:
166 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200167 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200168 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200169 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200170 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
171 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200172 case WLAN_CIPHER_SUITE_GCMP:
173 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100174 /* all of these we can do in software - if driver can */
175 if (ret == 1)
176 return 0;
177 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
178 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200179 return 0;
180 default:
181 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300182 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400183}
184
185static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
186{
Johannes Bergdc822b52008-12-29 12:55:09 +0100187 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100188 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400189 int ret;
190
Johannes Berg3b967662008-04-08 17:56:52 +0200191 might_sleep();
192
Johannes Bergdb4d1162008-02-25 16:27:45 +0100193 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400194 return;
195
Johannes Bergad0e2b52010-06-01 10:19:19 +0200196 assert_key_lock(key->local);
197
198 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400199 return;
200
Johannes Berg89c91ca2012-01-20 13:55:19 +0100201 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100202 sdata = key->sdata;
203
Johannes Berg1e359a52015-01-05 10:28:49 +0100204 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500205 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530206 increment_tailroom_need_count(sdata);
207
Johannes Berg12375ef2009-11-25 20:30:31 +0100208 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100209 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400210
211 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200212 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700213 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100214 key->conf.keyidx,
215 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400216
Johannes Berg3b967662008-04-08 17:56:52 +0200217 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200218}
219
220static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100221 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200222{
223 struct ieee80211_key *key = NULL;
224
Johannes Bergad0e2b52010-06-01 10:19:19 +0200225 assert_key_lock(sdata->local);
226
Johannes Berg3b967662008-04-08 17:56:52 +0200227 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200228 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200229
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300230 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100231 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300232 drv_set_default_unicast_key(sdata->local, sdata, idx);
233 }
234
Johannes Bergf7e01042010-12-09 19:49:02 +0100235 if (multi)
236 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200237
Johannes Bergf7e01042010-12-09 19:49:02 +0100238 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200239}
240
Johannes Bergf7e01042010-12-09 19:49:02 +0100241void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
242 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200243{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200244 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100245 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200246 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200247}
248
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200249static void
250__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
251{
252 struct ieee80211_key *key = NULL;
253
Johannes Bergad0e2b52010-06-01 10:19:19 +0200254 assert_key_lock(sdata->local);
255
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200256 if (idx >= NUM_DEFAULT_KEYS &&
257 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200258 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200259
260 rcu_assign_pointer(sdata->default_mgmt_key, key);
261
Johannes Bergf7e01042010-12-09 19:49:02 +0100262 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200263}
264
265void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
266 int idx)
267{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200268 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200269 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200270 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200271}
272
Johannes Berg3b967662008-04-08 17:56:52 +0200273
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100274static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
275 struct sta_info *sta,
276 bool pairwise,
277 struct ieee80211_key *old,
278 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200279{
Johannes Bergf7e01042010-12-09 19:49:02 +0100280 int idx;
281 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200282
Johannes Berg5282c3b2013-10-29 10:00:08 +0100283 /* caller must provide at least one old/new */
284 if (WARN_ON(!new && !old))
285 return;
286
Johannes Berg3b967662008-04-08 17:56:52 +0200287 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200288 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200289
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200290 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
291
292 if (old)
293 idx = old->conf.keyidx;
294 else
295 idx = new->conf.keyidx;
296
297 if (sta) {
298 if (pairwise) {
299 rcu_assign_pointer(sta->ptk[idx], new);
300 sta->ptk_idx = idx;
301 } else {
302 rcu_assign_pointer(sta->gtk[idx], new);
303 sta->gtk_idx = idx;
304 }
Johannes Berg3b967662008-04-08 17:56:52 +0200305 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200306 defunikey = old &&
307 old == key_mtx_dereference(sdata->local,
308 sdata->default_unicast_key);
309 defmultikey = old &&
310 old == key_mtx_dereference(sdata->local,
311 sdata->default_multicast_key);
312 defmgmtkey = old &&
313 old == key_mtx_dereference(sdata->local,
314 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200315
Johannes Bergf7e01042010-12-09 19:49:02 +0100316 if (defunikey && !new)
317 __ieee80211_set_default_key(sdata, -1, true, false);
318 if (defmultikey && !new)
319 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200320 if (defmgmtkey && !new)
321 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200322
323 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100324 if (defunikey && new)
325 __ieee80211_set_default_key(sdata, new->conf.keyidx,
326 true, false);
327 if (defmultikey && new)
328 __ieee80211_set_default_key(sdata, new->conf.keyidx,
329 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200330 if (defmgmtkey && new)
331 __ieee80211_set_default_mgmt_key(sdata,
332 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200333 }
334
Johannes Bergb5c34f62011-01-03 19:51:09 +0100335 if (old)
336 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400337}
338
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200339struct ieee80211_key *
340ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
341 const u8 *key_data,
342 size_t seq_len, const u8 *seq,
343 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200344{
345 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100346 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200347
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200348 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
349 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400350
351 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200352 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100353 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400354
355 /*
356 * Default to software encryption; we'll later upload the
357 * key to the hardware if possible.
358 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400359 key->conf.flags = 0;
360 key->flags = 0;
361
Johannes Berg97359d12010-08-10 09:46:38 +0200362 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400363 key->conf.keyidx = idx;
364 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200365 switch (cipher) {
366 case WLAN_CIPHER_SUITE_WEP40:
367 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200368 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
369 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200370 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200371 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200372 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
373 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300374 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100375 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300376 key->u.tkip.rx[i].iv32 =
377 get_unaligned_le32(&seq[2]);
378 key->u.tkip.rx[i].iv16 =
379 get_unaligned_le16(seq);
380 }
381 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200382 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200383 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200384 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200385 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
386 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300387 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100388 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200389 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300390 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200391 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300392 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400393 /*
394 * Initialize AES key state here as an optimization so that
395 * it does not need to be initialized for every packet.
396 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200397 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
398 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
399 if (IS_ERR(key->u.ccmp.tfm)) {
400 err = PTR_ERR(key->u.ccmp.tfm);
401 kfree(key);
402 return ERR_PTR(err);
403 }
404 break;
405 case WLAN_CIPHER_SUITE_CCMP_256:
406 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
407 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
408 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
409 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
410 key->u.ccmp.rx_pn[i][j] =
411 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
412 /* Initialize AES key state here as an optimization so that
413 * it does not need to be initialized for every packet.
414 */
415 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
416 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100417 if (IS_ERR(key->u.ccmp.tfm)) {
418 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200419 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200420 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400421 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200422 break;
423 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200424 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200425 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200426 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
427 key->conf.icv_len = sizeof(struct ieee80211_mmie);
428 else
429 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200430 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200431 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100432 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200433 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200434 /*
435 * Initialize AES key state here as an optimization so that
436 * it does not need to be initialized for every packet.
437 */
438 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200439 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100440 if (IS_ERR(key->u.aes_cmac.tfm)) {
441 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200442 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200443 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200444 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200445 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200446 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
447 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
448 key->conf.iv_len = 0;
449 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
450 if (seq)
451 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
452 key->u.aes_gmac.rx_pn[j] =
453 seq[IEEE80211_GMAC_PN_LEN - j - 1];
454 /* Initialize AES key state here as an optimization so that
455 * it does not need to be initialized for every packet.
456 */
457 key->u.aes_gmac.tfm =
458 ieee80211_aes_gmac_key_setup(key_data, key_len);
459 if (IS_ERR(key->u.aes_gmac.tfm)) {
460 err = PTR_ERR(key->u.aes_gmac.tfm);
461 kfree(key);
462 return ERR_PTR(err);
463 }
464 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200465 case WLAN_CIPHER_SUITE_GCMP:
466 case WLAN_CIPHER_SUITE_GCMP_256:
467 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
468 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
469 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
470 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
471 key->u.gcmp.rx_pn[i][j] =
472 seq[IEEE80211_GCMP_PN_LEN - j - 1];
473 /* Initialize AES key state here as an optimization so that
474 * it does not need to be initialized for every packet.
475 */
476 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
477 key_len);
478 if (IS_ERR(key->u.gcmp.tfm)) {
479 err = PTR_ERR(key->u.gcmp.tfm);
480 kfree(key);
481 return ERR_PTR(err);
482 }
483 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200484 default:
485 if (cs) {
486 size_t len = (seq_len > MAX_PN_LEN) ?
487 MAX_PN_LEN : seq_len;
488
489 key->conf.iv_len = cs->hdr_len;
490 key->conf.icv_len = cs->mic_len;
491 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
492 for (j = 0; j < len; j++)
493 key->u.gen.rx_pn[i][j] =
494 seq[len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000495 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200496 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200497 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200498 memcpy(key->conf.key, key_data, key_len);
499 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200500
Johannes Bergdb4d1162008-02-25 16:27:45 +0100501 return key;
502}
Johannes Berg11a843b2007-08-28 17:01:55 -0400503
Johannes Berg79cf2df2013-03-06 22:53:52 +0100504static void ieee80211_key_free_common(struct ieee80211_key *key)
505{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200506 switch (key->conf.cipher) {
507 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200508 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100509 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200510 break;
511 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200512 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100513 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200514 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200515 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
516 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
517 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
518 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200519 case WLAN_CIPHER_SUITE_GCMP:
520 case WLAN_CIPHER_SUITE_GCMP_256:
521 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
522 break;
523 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300524 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100525}
526
Johannes Berg6d10e462013-03-06 23:09:11 +0100527static void __ieee80211_key_destroy(struct ieee80211_key *key,
528 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200529{
Jouni Malinen32162a42010-07-26 15:52:03 -0700530 if (key->local)
531 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200532
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530533 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100534 struct ieee80211_sub_if_data *sdata = key->sdata;
535
Jouni Malinen32162a42010-07-26 15:52:03 -0700536 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100537
538 if (delay_tailroom) {
539 /* see ieee80211_delayed_tailroom_dec */
540 sdata->crypto_tx_tailroom_pending_dec++;
541 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
542 HZ/2);
543 } else {
544 sdata->crypto_tx_tailroom_needed_cnt--;
545 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530546 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200547
Johannes Berg79cf2df2013-03-06 22:53:52 +0100548 ieee80211_key_free_common(key);
549}
550
Johannes Berg6d10e462013-03-06 23:09:11 +0100551static void ieee80211_key_destroy(struct ieee80211_key *key,
552 bool delay_tailroom)
553{
554 if (!key)
555 return;
556
557 /*
558 * Synchronize so the TX path can no longer be using
559 * this key before we free/remove it.
560 */
561 synchronize_net();
562
563 __ieee80211_key_destroy(key, delay_tailroom);
564}
565
Johannes Berg79cf2df2013-03-06 22:53:52 +0100566void ieee80211_key_free_unused(struct ieee80211_key *key)
567{
568 WARN_ON(key->sdata || key->local);
569 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200570}
571
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300572int ieee80211_key_link(struct ieee80211_key *key,
573 struct ieee80211_sub_if_data *sdata,
574 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100575{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200576 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100577 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300578 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100579 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100580
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100581 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100582 idx = key->conf.keyidx;
583 key->local = sdata->local;
584 key->sdata = sdata;
585 key->sta = sta;
586
Johannes Bergad0e2b52010-06-01 10:19:19 +0200587 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200588
Johannes Berge31b8212010-10-05 19:39:30 +0200589 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200590 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200591 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200592 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400593 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200594 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100595
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530596 increment_tailroom_need_count(sdata);
597
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100598 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
599 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400600
Johannes Bergad0e2b52010-06-01 10:19:19 +0200601 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200602
Johannes Berg27b3eb92013-08-07 20:11:55 +0200603 if (!local->wowlan) {
604 ret = ieee80211_key_enable_hw_accel(key);
605 if (ret)
606 ieee80211_key_free(key, true);
607 } else {
608 ret = 0;
609 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100610
Johannes Bergad0e2b52010-06-01 10:19:19 +0200611 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300612
613 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200614}
615
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100616void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200617{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200618 if (!key)
619 return;
620
Johannes Berg3a245762008-04-09 16:45:37 +0200621 /*
622 * Replace key with nothingness if it was ever used.
623 */
624 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100625 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200626 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
627 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100628 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200629}
630
Johannes Berg3b967662008-04-08 17:56:52 +0200631void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
632{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200633 struct ieee80211_key *key;
634
Johannes Berg3a245762008-04-09 16:45:37 +0200635 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200636
Johannes Berg9607e6b2009-12-23 13:15:31 +0100637 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200638 return;
639
Johannes Bergad0e2b52010-06-01 10:19:19 +0200640 mutex_lock(&sdata->local->key_mtx);
641
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530642 sdata->crypto_tx_tailroom_needed_cnt = 0;
643
644 list_for_each_entry(key, &sdata->key_list, list) {
645 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200646 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530647 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200648
649 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200650}
651
Johannes Berg830af022011-07-05 16:35:39 +0200652void ieee80211_iter_keys(struct ieee80211_hw *hw,
653 struct ieee80211_vif *vif,
654 void (*iter)(struct ieee80211_hw *hw,
655 struct ieee80211_vif *vif,
656 struct ieee80211_sta *sta,
657 struct ieee80211_key_conf *key,
658 void *data),
659 void *iter_data)
660{
661 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200662 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200663 struct ieee80211_sub_if_data *sdata;
664
665 ASSERT_RTNL();
666
667 mutex_lock(&local->key_mtx);
668 if (vif) {
669 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200670 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200671 iter(hw, &sdata->vif,
672 key->sta ? &key->sta->sta : NULL,
673 &key->conf, iter_data);
674 } else {
675 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200676 list_for_each_entry_safe(key, tmp,
677 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200678 iter(hw, &sdata->vif,
679 key->sta ? &key->sta->sta : NULL,
680 &key->conf, iter_data);
681 }
682 mutex_unlock(&local->key_mtx);
683}
684EXPORT_SYMBOL(ieee80211_iter_keys);
685
Johannes Berg7907c7d2013-12-04 23:47:09 +0100686static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
687 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400688{
689 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200690
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100691 sdata->crypto_tx_tailroom_needed_cnt -=
692 sdata->crypto_tx_tailroom_pending_dec;
693 sdata->crypto_tx_tailroom_pending_dec = 0;
694
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200695 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400696
Johannes Berg6d10e462013-03-06 23:09:11 +0100697 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
698 ieee80211_key_replace(key->sdata, key->sta,
699 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
700 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100701 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100702 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400703
Johannes Bergf7e01042010-12-09 19:49:02 +0100704 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100705}
Johannes Bergf7e01042010-12-09 19:49:02 +0100706
Johannes Berg7907c7d2013-12-04 23:47:09 +0100707void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
708 bool force_synchronize)
709{
710 struct ieee80211_local *local = sdata->local;
711 struct ieee80211_sub_if_data *vlan;
712 struct ieee80211_key *key, *tmp;
713 LIST_HEAD(keys);
714
715 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
716
717 mutex_lock(&local->key_mtx);
718
719 ieee80211_free_keys_iface(sdata, &keys);
720
721 if (sdata->vif.type == NL80211_IFTYPE_AP) {
722 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
723 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100724 }
725
Johannes Berg7907c7d2013-12-04 23:47:09 +0100726 if (!list_empty(&keys) || force_synchronize)
727 synchronize_net();
728 list_for_each_entry_safe(key, tmp, &keys, list)
729 __ieee80211_key_destroy(key, false);
730
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100731 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
732 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100733 if (sdata->vif.type == NL80211_IFTYPE_AP) {
734 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
735 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
736 vlan->crypto_tx_tailroom_pending_dec);
737 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100738
Johannes Berg7907c7d2013-12-04 23:47:09 +0100739 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400740}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200741
Johannes Berg6d10e462013-03-06 23:09:11 +0100742void ieee80211_free_sta_keys(struct ieee80211_local *local,
743 struct sta_info *sta)
744{
Johannes Bergc8782072013-12-04 23:05:45 +0100745 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100746 int i;
747
748 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100749 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100750 key = key_mtx_dereference(local, sta->gtk[i]);
751 if (!key)
752 continue;
753 ieee80211_key_replace(key->sdata, key->sta,
754 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
755 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100756 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100757 }
758
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200759 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
760 key = key_mtx_dereference(local, sta->ptk[i]);
761 if (!key)
762 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100763 ieee80211_key_replace(key->sdata, key->sta,
764 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
765 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100766 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100767 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100768
769 mutex_unlock(&local->key_mtx);
770}
771
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100772void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
773{
774 struct ieee80211_sub_if_data *sdata;
775
776 sdata = container_of(wk, struct ieee80211_sub_if_data,
777 dec_tailroom_needed_wk.work);
778
779 /*
780 * The reason for the delayed tailroom needed decrementing is to
781 * make roaming faster: during roaming, all keys are first deleted
782 * and then new keys are installed. The first new key causes the
783 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
784 * the cost of synchronize_net() (which can be slow). Avoid this
785 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
786 * key removal for a while, so if we roam the value is larger than
787 * zero and no 0->1 transition happens.
788 *
789 * The cost is that if the AP switching was from an AP with keys
790 * to one without, we still allocate tailroom while it would no
791 * longer be needed. However, in the typical (fast) roaming case
792 * within an ESS this usually won't happen.
793 */
794
795 mutex_lock(&sdata->local->key_mtx);
796 sdata->crypto_tx_tailroom_needed_cnt -=
797 sdata->crypto_tx_tailroom_pending_dec;
798 sdata->crypto_tx_tailroom_pending_dec = 0;
799 mutex_unlock(&sdata->local->key_mtx);
800}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200801
802void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
803 const u8 *replay_ctr, gfp_t gfp)
804{
805 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
806
807 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
808
809 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
810}
811EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200812
813void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
814 struct ieee80211_key_seq *seq)
815{
816 struct ieee80211_key *key;
817 u64 pn64;
818
819 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
820 return;
821
822 key = container_of(keyconf, struct ieee80211_key, conf);
823
824 switch (key->conf.cipher) {
825 case WLAN_CIPHER_SUITE_TKIP:
826 seq->tkip.iv32 = key->u.tkip.tx.iv32;
827 seq->tkip.iv16 = key->u.tkip.tx.iv16;
828 break;
829 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200830 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200831 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
832 seq->ccmp.pn[5] = pn64;
833 seq->ccmp.pn[4] = pn64 >> 8;
834 seq->ccmp.pn[3] = pn64 >> 16;
835 seq->ccmp.pn[2] = pn64 >> 24;
836 seq->ccmp.pn[1] = pn64 >> 32;
837 seq->ccmp.pn[0] = pn64 >> 40;
838 break;
839 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200840 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200841 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
842 seq->ccmp.pn[5] = pn64;
843 seq->ccmp.pn[4] = pn64 >> 8;
844 seq->ccmp.pn[3] = pn64 >> 16;
845 seq->ccmp.pn[2] = pn64 >> 24;
846 seq->ccmp.pn[1] = pn64 >> 32;
847 seq->ccmp.pn[0] = pn64 >> 40;
848 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200849 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
850 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
851 pn64 = atomic64_read(&key->u.aes_gmac.tx_pn);
852 seq->ccmp.pn[5] = pn64;
853 seq->ccmp.pn[4] = pn64 >> 8;
854 seq->ccmp.pn[3] = pn64 >> 16;
855 seq->ccmp.pn[2] = pn64 >> 24;
856 seq->ccmp.pn[1] = pn64 >> 32;
857 seq->ccmp.pn[0] = pn64 >> 40;
858 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200859 case WLAN_CIPHER_SUITE_GCMP:
860 case WLAN_CIPHER_SUITE_GCMP_256:
861 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
862 seq->gcmp.pn[5] = pn64;
863 seq->gcmp.pn[4] = pn64 >> 8;
864 seq->gcmp.pn[3] = pn64 >> 16;
865 seq->gcmp.pn[2] = pn64 >> 24;
866 seq->gcmp.pn[1] = pn64 >> 32;
867 seq->gcmp.pn[0] = pn64 >> 40;
868 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200869 default:
870 WARN_ON(1);
871 }
872}
873EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
874
875void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
876 int tid, struct ieee80211_key_seq *seq)
877{
878 struct ieee80211_key *key;
879 const u8 *pn;
880
881 key = container_of(keyconf, struct ieee80211_key, conf);
882
883 switch (key->conf.cipher) {
884 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100885 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200886 return;
887 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
888 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
889 break;
890 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200891 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +0100892 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200893 return;
894 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100895 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200896 else
897 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200898 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200899 break;
900 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200901 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200902 if (WARN_ON(tid != 0))
903 return;
904 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200905 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200906 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200907 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
908 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
909 if (WARN_ON(tid != 0))
910 return;
911 pn = key->u.aes_gmac.rx_pn;
912 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
913 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200914 case WLAN_CIPHER_SUITE_GCMP:
915 case WLAN_CIPHER_SUITE_GCMP_256:
916 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
917 return;
918 if (tid < 0)
919 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
920 else
921 pn = key->u.gcmp.rx_pn[tid];
922 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
923 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200924 }
925}
926EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200927
928void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
929 struct ieee80211_key_seq *seq)
930{
931 struct ieee80211_key *key;
932 u64 pn64;
933
934 key = container_of(keyconf, struct ieee80211_key, conf);
935
936 switch (key->conf.cipher) {
937 case WLAN_CIPHER_SUITE_TKIP:
938 key->u.tkip.tx.iv32 = seq->tkip.iv32;
939 key->u.tkip.tx.iv16 = seq->tkip.iv16;
940 break;
941 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200942 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200943 pn64 = (u64)seq->ccmp.pn[5] |
944 ((u64)seq->ccmp.pn[4] << 8) |
945 ((u64)seq->ccmp.pn[3] << 16) |
946 ((u64)seq->ccmp.pn[2] << 24) |
947 ((u64)seq->ccmp.pn[1] << 32) |
948 ((u64)seq->ccmp.pn[0] << 40);
949 atomic64_set(&key->u.ccmp.tx_pn, pn64);
950 break;
951 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200952 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200953 pn64 = (u64)seq->aes_cmac.pn[5] |
954 ((u64)seq->aes_cmac.pn[4] << 8) |
955 ((u64)seq->aes_cmac.pn[3] << 16) |
956 ((u64)seq->aes_cmac.pn[2] << 24) |
957 ((u64)seq->aes_cmac.pn[1] << 32) |
958 ((u64)seq->aes_cmac.pn[0] << 40);
959 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
960 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200961 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
962 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
963 pn64 = (u64)seq->aes_gmac.pn[5] |
964 ((u64)seq->aes_gmac.pn[4] << 8) |
965 ((u64)seq->aes_gmac.pn[3] << 16) |
966 ((u64)seq->aes_gmac.pn[2] << 24) |
967 ((u64)seq->aes_gmac.pn[1] << 32) |
968 ((u64)seq->aes_gmac.pn[0] << 40);
969 atomic64_set(&key->u.aes_gmac.tx_pn, pn64);
970 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200971 case WLAN_CIPHER_SUITE_GCMP:
972 case WLAN_CIPHER_SUITE_GCMP_256:
973 pn64 = (u64)seq->gcmp.pn[5] |
974 ((u64)seq->gcmp.pn[4] << 8) |
975 ((u64)seq->gcmp.pn[3] << 16) |
976 ((u64)seq->gcmp.pn[2] << 24) |
977 ((u64)seq->gcmp.pn[1] << 32) |
978 ((u64)seq->gcmp.pn[0] << 40);
979 atomic64_set(&key->u.gcmp.tx_pn, pn64);
980 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200981 default:
982 WARN_ON(1);
983 break;
984 }
985}
986EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
987
988void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
989 int tid, struct ieee80211_key_seq *seq)
990{
991 struct ieee80211_key *key;
992 u8 *pn;
993
994 key = container_of(keyconf, struct ieee80211_key, conf);
995
996 switch (key->conf.cipher) {
997 case WLAN_CIPHER_SUITE_TKIP:
998 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
999 return;
1000 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1001 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1002 break;
1003 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001004 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001005 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1006 return;
1007 if (tid < 0)
1008 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1009 else
1010 pn = key->u.ccmp.rx_pn[tid];
1011 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1012 break;
1013 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001014 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001015 if (WARN_ON(tid != 0))
1016 return;
1017 pn = key->u.aes_cmac.rx_pn;
1018 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1019 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001020 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1021 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1022 if (WARN_ON(tid != 0))
1023 return;
1024 pn = key->u.aes_gmac.rx_pn;
1025 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1026 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001027 case WLAN_CIPHER_SUITE_GCMP:
1028 case WLAN_CIPHER_SUITE_GCMP_256:
1029 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1030 return;
1031 if (tid < 0)
1032 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1033 else
1034 pn = key->u.gcmp.rx_pn[tid];
1035 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1036 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001037 default:
1038 WARN_ON(1);
1039 break;
1040 }
1041}
1042EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1043
1044void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1045{
1046 struct ieee80211_key *key;
1047
1048 key = container_of(keyconf, struct ieee80211_key, conf);
1049
1050 assert_key_lock(key->local);
1051
1052 /*
1053 * if key was uploaded, we assume the driver will/has remove(d)
1054 * it, so adjust bookkeeping accordingly
1055 */
1056 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1057 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1058
Johannes Berg1e359a52015-01-05 10:28:49 +01001059 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -05001060 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001061 increment_tailroom_need_count(key->sdata);
1062 }
1063
1064 ieee80211_key_free(key, false);
1065}
1066EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1067
1068struct ieee80211_key_conf *
1069ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1070 struct ieee80211_key_conf *keyconf)
1071{
1072 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1073 struct ieee80211_local *local = sdata->local;
1074 struct ieee80211_key *key;
1075 int err;
1076
1077 if (WARN_ON(!local->wowlan))
1078 return ERR_PTR(-EINVAL);
1079
1080 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1081 return ERR_PTR(-EINVAL);
1082
1083 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1084 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001085 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001086 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001087 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001088
1089 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1090 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1091
1092 err = ieee80211_key_link(key, sdata, NULL);
1093 if (err)
1094 return ERR_PTR(err);
1095
1096 return &key->conf;
1097}
1098EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);