blob: 2e677376c9585a58d1c8cbeb9390e3de3e4cafa3 [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);
Johannes Berg17c18bf2015-03-21 15:25:43 +0100232 ieee80211_check_fast_xmit_iface(sdata);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300233 drv_set_default_unicast_key(sdata->local, sdata, idx);
234 }
235
Johannes Bergf7e01042010-12-09 19:49:02 +0100236 if (multi)
237 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200238
Johannes Bergf7e01042010-12-09 19:49:02 +0100239 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200240}
241
Johannes Bergf7e01042010-12-09 19:49:02 +0100242void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
243 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200244{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200245 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100246 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200247 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200248}
249
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200250static void
251__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
252{
253 struct ieee80211_key *key = NULL;
254
Johannes Bergad0e2b52010-06-01 10:19:19 +0200255 assert_key_lock(sdata->local);
256
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200257 if (idx >= NUM_DEFAULT_KEYS &&
258 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200259 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200260
261 rcu_assign_pointer(sdata->default_mgmt_key, key);
262
Johannes Bergf7e01042010-12-09 19:49:02 +0100263 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200264}
265
266void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
267 int idx)
268{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200269 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200270 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200271 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200272}
273
Johannes Berg3b967662008-04-08 17:56:52 +0200274
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100275static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
276 struct sta_info *sta,
277 bool pairwise,
278 struct ieee80211_key *old,
279 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200280{
Johannes Bergf7e01042010-12-09 19:49:02 +0100281 int idx;
282 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200283
Johannes Berg5282c3b2013-10-29 10:00:08 +0100284 /* caller must provide at least one old/new */
285 if (WARN_ON(!new && !old))
286 return;
287
Johannes Berg3b967662008-04-08 17:56:52 +0200288 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200289 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200290
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200291 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
292
293 if (old)
294 idx = old->conf.keyidx;
295 else
296 idx = new->conf.keyidx;
297
298 if (sta) {
299 if (pairwise) {
300 rcu_assign_pointer(sta->ptk[idx], new);
301 sta->ptk_idx = idx;
Johannes Berg17c18bf2015-03-21 15:25:43 +0100302 ieee80211_check_fast_xmit(sta);
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200303 } else {
304 rcu_assign_pointer(sta->gtk[idx], new);
305 sta->gtk_idx = idx;
306 }
Johannes Berg3b967662008-04-08 17:56:52 +0200307 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200308 defunikey = old &&
309 old == key_mtx_dereference(sdata->local,
310 sdata->default_unicast_key);
311 defmultikey = old &&
312 old == key_mtx_dereference(sdata->local,
313 sdata->default_multicast_key);
314 defmgmtkey = old &&
315 old == key_mtx_dereference(sdata->local,
316 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200317
Johannes Bergf7e01042010-12-09 19:49:02 +0100318 if (defunikey && !new)
319 __ieee80211_set_default_key(sdata, -1, true, false);
320 if (defmultikey && !new)
321 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200322 if (defmgmtkey && !new)
323 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200324
325 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100326 if (defunikey && new)
327 __ieee80211_set_default_key(sdata, new->conf.keyidx,
328 true, false);
329 if (defmultikey && new)
330 __ieee80211_set_default_key(sdata, new->conf.keyidx,
331 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200332 if (defmgmtkey && new)
333 __ieee80211_set_default_mgmt_key(sdata,
334 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200335 }
336
Johannes Bergb5c34f62011-01-03 19:51:09 +0100337 if (old)
338 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400339}
340
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200341struct ieee80211_key *
342ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
343 const u8 *key_data,
344 size_t seq_len, const u8 *seq,
345 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200346{
347 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100348 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200349
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200350 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
351 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400352
353 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200354 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100355 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400356
357 /*
358 * Default to software encryption; we'll later upload the
359 * key to the hardware if possible.
360 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400361 key->conf.flags = 0;
362 key->flags = 0;
363
Johannes Berg97359d12010-08-10 09:46:38 +0200364 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400365 key->conf.keyidx = idx;
366 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200367 switch (cipher) {
368 case WLAN_CIPHER_SUITE_WEP40:
369 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200370 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
371 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200372 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200373 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200374 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
375 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300376 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100377 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300378 key->u.tkip.rx[i].iv32 =
379 get_unaligned_le32(&seq[2]);
380 key->u.tkip.rx[i].iv16 =
381 get_unaligned_le16(seq);
382 }
383 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200384 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200385 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200386 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200387 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
388 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300389 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100390 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200391 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300392 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200393 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300394 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400395 /*
396 * Initialize AES key state here as an optimization so that
397 * it does not need to be initialized for every packet.
398 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200399 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
400 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
401 if (IS_ERR(key->u.ccmp.tfm)) {
402 err = PTR_ERR(key->u.ccmp.tfm);
403 kfree(key);
404 return ERR_PTR(err);
405 }
406 break;
407 case WLAN_CIPHER_SUITE_CCMP_256:
408 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
409 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
410 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
411 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
412 key->u.ccmp.rx_pn[i][j] =
413 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
414 /* Initialize AES key state here as an optimization so that
415 * it does not need to be initialized for every packet.
416 */
417 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
418 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100419 if (IS_ERR(key->u.ccmp.tfm)) {
420 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200421 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200422 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400423 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200424 break;
425 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200426 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200427 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200428 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
429 key->conf.icv_len = sizeof(struct ieee80211_mmie);
430 else
431 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200432 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200433 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100434 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200435 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200436 /*
437 * Initialize AES key state here as an optimization so that
438 * it does not need to be initialized for every packet.
439 */
440 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200441 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100442 if (IS_ERR(key->u.aes_cmac.tfm)) {
443 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200444 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200445 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200446 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200447 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200448 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
449 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
450 key->conf.iv_len = 0;
451 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
452 if (seq)
453 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
454 key->u.aes_gmac.rx_pn[j] =
455 seq[IEEE80211_GMAC_PN_LEN - j - 1];
456 /* Initialize AES key state here as an optimization so that
457 * it does not need to be initialized for every packet.
458 */
459 key->u.aes_gmac.tfm =
460 ieee80211_aes_gmac_key_setup(key_data, key_len);
461 if (IS_ERR(key->u.aes_gmac.tfm)) {
462 err = PTR_ERR(key->u.aes_gmac.tfm);
463 kfree(key);
464 return ERR_PTR(err);
465 }
466 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200467 case WLAN_CIPHER_SUITE_GCMP:
468 case WLAN_CIPHER_SUITE_GCMP_256:
469 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
470 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
471 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
472 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
473 key->u.gcmp.rx_pn[i][j] =
474 seq[IEEE80211_GCMP_PN_LEN - j - 1];
475 /* Initialize AES key state here as an optimization so that
476 * it does not need to be initialized for every packet.
477 */
478 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
479 key_len);
480 if (IS_ERR(key->u.gcmp.tfm)) {
481 err = PTR_ERR(key->u.gcmp.tfm);
482 kfree(key);
483 return ERR_PTR(err);
484 }
485 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200486 default:
487 if (cs) {
Johannes Berge3a55b52015-05-05 16:32:29 +0200488 if (seq_len && seq_len != cs->pn_len) {
489 kfree(key);
490 return ERR_PTR(-EINVAL);
491 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200492
493 key->conf.iv_len = cs->hdr_len;
494 key->conf.icv_len = cs->mic_len;
495 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berge3a55b52015-05-05 16:32:29 +0200496 for (j = 0; j < seq_len; j++)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200497 key->u.gen.rx_pn[i][j] =
Johannes Berge3a55b52015-05-05 16:32:29 +0200498 seq[seq_len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000499 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200500 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200501 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200502 memcpy(key->conf.key, key_data, key_len);
503 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200504
Johannes Bergdb4d1162008-02-25 16:27:45 +0100505 return key;
506}
Johannes Berg11a843b2007-08-28 17:01:55 -0400507
Johannes Berg79cf2df2013-03-06 22:53:52 +0100508static void ieee80211_key_free_common(struct ieee80211_key *key)
509{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200510 switch (key->conf.cipher) {
511 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200512 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100513 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200514 break;
515 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200516 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100517 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200518 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200519 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
520 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
521 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
522 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200523 case WLAN_CIPHER_SUITE_GCMP:
524 case WLAN_CIPHER_SUITE_GCMP_256:
525 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
526 break;
527 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300528 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100529}
530
Johannes Berg6d10e462013-03-06 23:09:11 +0100531static void __ieee80211_key_destroy(struct ieee80211_key *key,
532 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200533{
Jouni Malinen32162a42010-07-26 15:52:03 -0700534 if (key->local)
535 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200536
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530537 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100538 struct ieee80211_sub_if_data *sdata = key->sdata;
539
Jouni Malinen32162a42010-07-26 15:52:03 -0700540 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100541
542 if (delay_tailroom) {
543 /* see ieee80211_delayed_tailroom_dec */
544 sdata->crypto_tx_tailroom_pending_dec++;
545 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
546 HZ/2);
547 } else {
548 sdata->crypto_tx_tailroom_needed_cnt--;
549 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530550 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200551
Johannes Berg79cf2df2013-03-06 22:53:52 +0100552 ieee80211_key_free_common(key);
553}
554
Johannes Berg6d10e462013-03-06 23:09:11 +0100555static void ieee80211_key_destroy(struct ieee80211_key *key,
556 bool delay_tailroom)
557{
558 if (!key)
559 return;
560
561 /*
562 * Synchronize so the TX path can no longer be using
563 * this key before we free/remove it.
564 */
565 synchronize_net();
566
567 __ieee80211_key_destroy(key, delay_tailroom);
568}
569
Johannes Berg79cf2df2013-03-06 22:53:52 +0100570void ieee80211_key_free_unused(struct ieee80211_key *key)
571{
572 WARN_ON(key->sdata || key->local);
573 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200574}
575
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300576int ieee80211_key_link(struct ieee80211_key *key,
577 struct ieee80211_sub_if_data *sdata,
578 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100579{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200580 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100581 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300582 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100583 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100584
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100585 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100586 idx = key->conf.keyidx;
587 key->local = sdata->local;
588 key->sdata = sdata;
589 key->sta = sta;
590
Johannes Bergad0e2b52010-06-01 10:19:19 +0200591 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200592
Johannes Berge31b8212010-10-05 19:39:30 +0200593 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200594 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200595 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200596 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400597 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200598 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100599
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530600 increment_tailroom_need_count(sdata);
601
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100602 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
603 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400604
Johannes Bergad0e2b52010-06-01 10:19:19 +0200605 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200606
Johannes Berg27b3eb92013-08-07 20:11:55 +0200607 if (!local->wowlan) {
608 ret = ieee80211_key_enable_hw_accel(key);
609 if (ret)
610 ieee80211_key_free(key, true);
611 } else {
612 ret = 0;
613 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100614
Johannes Bergad0e2b52010-06-01 10:19:19 +0200615 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300616
617 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200618}
619
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100620void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200621{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200622 if (!key)
623 return;
624
Johannes Berg3a245762008-04-09 16:45:37 +0200625 /*
626 * Replace key with nothingness if it was ever used.
627 */
628 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100629 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200630 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
631 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100632 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200633}
634
Johannes Berg3b967662008-04-08 17:56:52 +0200635void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
636{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200637 struct ieee80211_key *key;
638
Johannes Berg3a245762008-04-09 16:45:37 +0200639 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200640
Johannes Berg9607e6b662009-12-23 13:15:31 +0100641 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200642 return;
643
Johannes Bergad0e2b52010-06-01 10:19:19 +0200644 mutex_lock(&sdata->local->key_mtx);
645
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530646 sdata->crypto_tx_tailroom_needed_cnt = 0;
647
648 list_for_each_entry(key, &sdata->key_list, list) {
649 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200650 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530651 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200652
653 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200654}
655
Johannes Berg830af022011-07-05 16:35:39 +0200656void ieee80211_iter_keys(struct ieee80211_hw *hw,
657 struct ieee80211_vif *vif,
658 void (*iter)(struct ieee80211_hw *hw,
659 struct ieee80211_vif *vif,
660 struct ieee80211_sta *sta,
661 struct ieee80211_key_conf *key,
662 void *data),
663 void *iter_data)
664{
665 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200666 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200667 struct ieee80211_sub_if_data *sdata;
668
669 ASSERT_RTNL();
670
671 mutex_lock(&local->key_mtx);
672 if (vif) {
673 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200674 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200675 iter(hw, &sdata->vif,
676 key->sta ? &key->sta->sta : NULL,
677 &key->conf, iter_data);
678 } else {
679 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200680 list_for_each_entry_safe(key, tmp,
681 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200682 iter(hw, &sdata->vif,
683 key->sta ? &key->sta->sta : NULL,
684 &key->conf, iter_data);
685 }
686 mutex_unlock(&local->key_mtx);
687}
688EXPORT_SYMBOL(ieee80211_iter_keys);
689
Johannes Berg7907c7d2013-12-04 23:47:09 +0100690static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
691 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400692{
693 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200694
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100695 sdata->crypto_tx_tailroom_needed_cnt -=
696 sdata->crypto_tx_tailroom_pending_dec;
697 sdata->crypto_tx_tailroom_pending_dec = 0;
698
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200699 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400700
Johannes Berg6d10e462013-03-06 23:09:11 +0100701 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
702 ieee80211_key_replace(key->sdata, key->sta,
703 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
704 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100705 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100706 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400707
Johannes Bergf7e01042010-12-09 19:49:02 +0100708 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100709}
Johannes Bergf7e01042010-12-09 19:49:02 +0100710
Johannes Berg7907c7d2013-12-04 23:47:09 +0100711void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
712 bool force_synchronize)
713{
714 struct ieee80211_local *local = sdata->local;
715 struct ieee80211_sub_if_data *vlan;
716 struct ieee80211_key *key, *tmp;
717 LIST_HEAD(keys);
718
719 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
720
721 mutex_lock(&local->key_mtx);
722
723 ieee80211_free_keys_iface(sdata, &keys);
724
725 if (sdata->vif.type == NL80211_IFTYPE_AP) {
726 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
727 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100728 }
729
Johannes Berg7907c7d2013-12-04 23:47:09 +0100730 if (!list_empty(&keys) || force_synchronize)
731 synchronize_net();
732 list_for_each_entry_safe(key, tmp, &keys, list)
733 __ieee80211_key_destroy(key, false);
734
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100735 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
736 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100737 if (sdata->vif.type == NL80211_IFTYPE_AP) {
738 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
739 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
740 vlan->crypto_tx_tailroom_pending_dec);
741 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100742
Johannes Berg7907c7d2013-12-04 23:47:09 +0100743 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400744}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200745
Johannes Berg6d10e462013-03-06 23:09:11 +0100746void ieee80211_free_sta_keys(struct ieee80211_local *local,
747 struct sta_info *sta)
748{
Johannes Bergc8782072013-12-04 23:05:45 +0100749 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100750 int i;
751
752 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100753 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100754 key = key_mtx_dereference(local, sta->gtk[i]);
755 if (!key)
756 continue;
757 ieee80211_key_replace(key->sdata, key->sta,
758 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
759 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100760 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100761 }
762
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200763 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
764 key = key_mtx_dereference(local, sta->ptk[i]);
765 if (!key)
766 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100767 ieee80211_key_replace(key->sdata, key->sta,
768 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
769 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100770 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100771 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100772
773 mutex_unlock(&local->key_mtx);
774}
775
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100776void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
777{
778 struct ieee80211_sub_if_data *sdata;
779
780 sdata = container_of(wk, struct ieee80211_sub_if_data,
781 dec_tailroom_needed_wk.work);
782
783 /*
784 * The reason for the delayed tailroom needed decrementing is to
785 * make roaming faster: during roaming, all keys are first deleted
786 * and then new keys are installed. The first new key causes the
787 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
788 * the cost of synchronize_net() (which can be slow). Avoid this
789 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
790 * key removal for a while, so if we roam the value is larger than
791 * zero and no 0->1 transition happens.
792 *
793 * The cost is that if the AP switching was from an AP with keys
794 * to one without, we still allocate tailroom while it would no
795 * longer be needed. However, in the typical (fast) roaming case
796 * within an ESS this usually won't happen.
797 */
798
799 mutex_lock(&sdata->local->key_mtx);
800 sdata->crypto_tx_tailroom_needed_cnt -=
801 sdata->crypto_tx_tailroom_pending_dec;
802 sdata->crypto_tx_tailroom_pending_dec = 0;
803 mutex_unlock(&sdata->local->key_mtx);
804}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200805
806void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
807 const u8 *replay_ctr, gfp_t gfp)
808{
809 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
810
811 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
812
813 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
814}
815EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200816
817void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
818 struct ieee80211_key_seq *seq)
819{
820 struct ieee80211_key *key;
821 u64 pn64;
822
823 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
824 return;
825
826 key = container_of(keyconf, struct ieee80211_key, conf);
827
828 switch (key->conf.cipher) {
829 case WLAN_CIPHER_SUITE_TKIP:
830 seq->tkip.iv32 = key->u.tkip.tx.iv32;
831 seq->tkip.iv16 = key->u.tkip.tx.iv16;
832 break;
833 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200834 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200835 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
836 seq->ccmp.pn[5] = pn64;
837 seq->ccmp.pn[4] = pn64 >> 8;
838 seq->ccmp.pn[3] = pn64 >> 16;
839 seq->ccmp.pn[2] = pn64 >> 24;
840 seq->ccmp.pn[1] = pn64 >> 32;
841 seq->ccmp.pn[0] = pn64 >> 40;
842 break;
843 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200844 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200845 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
846 seq->ccmp.pn[5] = pn64;
847 seq->ccmp.pn[4] = pn64 >> 8;
848 seq->ccmp.pn[3] = pn64 >> 16;
849 seq->ccmp.pn[2] = pn64 >> 24;
850 seq->ccmp.pn[1] = pn64 >> 32;
851 seq->ccmp.pn[0] = pn64 >> 40;
852 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200853 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
854 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
855 pn64 = atomic64_read(&key->u.aes_gmac.tx_pn);
856 seq->ccmp.pn[5] = pn64;
857 seq->ccmp.pn[4] = pn64 >> 8;
858 seq->ccmp.pn[3] = pn64 >> 16;
859 seq->ccmp.pn[2] = pn64 >> 24;
860 seq->ccmp.pn[1] = pn64 >> 32;
861 seq->ccmp.pn[0] = pn64 >> 40;
862 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200863 case WLAN_CIPHER_SUITE_GCMP:
864 case WLAN_CIPHER_SUITE_GCMP_256:
865 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
866 seq->gcmp.pn[5] = pn64;
867 seq->gcmp.pn[4] = pn64 >> 8;
868 seq->gcmp.pn[3] = pn64 >> 16;
869 seq->gcmp.pn[2] = pn64 >> 24;
870 seq->gcmp.pn[1] = pn64 >> 32;
871 seq->gcmp.pn[0] = pn64 >> 40;
872 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200873 default:
874 WARN_ON(1);
875 }
876}
877EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
878
879void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
880 int tid, struct ieee80211_key_seq *seq)
881{
882 struct ieee80211_key *key;
883 const u8 *pn;
884
885 key = container_of(keyconf, struct ieee80211_key, conf);
886
887 switch (key->conf.cipher) {
888 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100889 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200890 return;
891 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
892 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
893 break;
894 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200895 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +0100896 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200897 return;
898 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100899 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200900 else
901 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200902 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200903 break;
904 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200905 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200906 if (WARN_ON(tid != 0))
907 return;
908 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200909 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200910 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200911 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
912 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
913 if (WARN_ON(tid != 0))
914 return;
915 pn = key->u.aes_gmac.rx_pn;
916 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
917 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200918 case WLAN_CIPHER_SUITE_GCMP:
919 case WLAN_CIPHER_SUITE_GCMP_256:
920 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
921 return;
922 if (tid < 0)
923 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
924 else
925 pn = key->u.gcmp.rx_pn[tid];
926 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
927 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200928 }
929}
930EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200931
932void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
933 struct ieee80211_key_seq *seq)
934{
935 struct ieee80211_key *key;
936 u64 pn64;
937
938 key = container_of(keyconf, struct ieee80211_key, conf);
939
940 switch (key->conf.cipher) {
941 case WLAN_CIPHER_SUITE_TKIP:
942 key->u.tkip.tx.iv32 = seq->tkip.iv32;
943 key->u.tkip.tx.iv16 = seq->tkip.iv16;
944 break;
945 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200946 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200947 pn64 = (u64)seq->ccmp.pn[5] |
948 ((u64)seq->ccmp.pn[4] << 8) |
949 ((u64)seq->ccmp.pn[3] << 16) |
950 ((u64)seq->ccmp.pn[2] << 24) |
951 ((u64)seq->ccmp.pn[1] << 32) |
952 ((u64)seq->ccmp.pn[0] << 40);
953 atomic64_set(&key->u.ccmp.tx_pn, pn64);
954 break;
955 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200956 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +0200957 pn64 = (u64)seq->aes_cmac.pn[5] |
958 ((u64)seq->aes_cmac.pn[4] << 8) |
959 ((u64)seq->aes_cmac.pn[3] << 16) |
960 ((u64)seq->aes_cmac.pn[2] << 24) |
961 ((u64)seq->aes_cmac.pn[1] << 32) |
962 ((u64)seq->aes_cmac.pn[0] << 40);
963 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
964 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200965 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
966 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
967 pn64 = (u64)seq->aes_gmac.pn[5] |
968 ((u64)seq->aes_gmac.pn[4] << 8) |
969 ((u64)seq->aes_gmac.pn[3] << 16) |
970 ((u64)seq->aes_gmac.pn[2] << 24) |
971 ((u64)seq->aes_gmac.pn[1] << 32) |
972 ((u64)seq->aes_gmac.pn[0] << 40);
973 atomic64_set(&key->u.aes_gmac.tx_pn, pn64);
974 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200975 case WLAN_CIPHER_SUITE_GCMP:
976 case WLAN_CIPHER_SUITE_GCMP_256:
977 pn64 = (u64)seq->gcmp.pn[5] |
978 ((u64)seq->gcmp.pn[4] << 8) |
979 ((u64)seq->gcmp.pn[3] << 16) |
980 ((u64)seq->gcmp.pn[2] << 24) |
981 ((u64)seq->gcmp.pn[1] << 32) |
982 ((u64)seq->gcmp.pn[0] << 40);
983 atomic64_set(&key->u.gcmp.tx_pn, pn64);
984 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200985 default:
986 WARN_ON(1);
987 break;
988 }
989}
990EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
991
992void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
993 int tid, struct ieee80211_key_seq *seq)
994{
995 struct ieee80211_key *key;
996 u8 *pn;
997
998 key = container_of(keyconf, struct ieee80211_key, conf);
999
1000 switch (key->conf.cipher) {
1001 case WLAN_CIPHER_SUITE_TKIP:
1002 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1003 return;
1004 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1005 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1006 break;
1007 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001008 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001009 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1010 return;
1011 if (tid < 0)
1012 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1013 else
1014 pn = key->u.ccmp.rx_pn[tid];
1015 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1016 break;
1017 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001018 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001019 if (WARN_ON(tid != 0))
1020 return;
1021 pn = key->u.aes_cmac.rx_pn;
1022 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1023 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001024 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1025 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1026 if (WARN_ON(tid != 0))
1027 return;
1028 pn = key->u.aes_gmac.rx_pn;
1029 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1030 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001031 case WLAN_CIPHER_SUITE_GCMP:
1032 case WLAN_CIPHER_SUITE_GCMP_256:
1033 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1034 return;
1035 if (tid < 0)
1036 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1037 else
1038 pn = key->u.gcmp.rx_pn[tid];
1039 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1040 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001041 default:
1042 WARN_ON(1);
1043 break;
1044 }
1045}
1046EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1047
1048void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1049{
1050 struct ieee80211_key *key;
1051
1052 key = container_of(keyconf, struct ieee80211_key, conf);
1053
1054 assert_key_lock(key->local);
1055
1056 /*
1057 * if key was uploaded, we assume the driver will/has remove(d)
1058 * it, so adjust bookkeeping accordingly
1059 */
1060 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1061 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1062
Johannes Berg1e359a52015-01-05 10:28:49 +01001063 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -05001064 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001065 increment_tailroom_need_count(key->sdata);
1066 }
1067
1068 ieee80211_key_free(key, false);
1069}
1070EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1071
1072struct ieee80211_key_conf *
1073ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1074 struct ieee80211_key_conf *keyconf)
1075{
1076 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1077 struct ieee80211_local *local = sdata->local;
1078 struct ieee80211_key *key;
1079 int err;
1080
1081 if (WARN_ON(!local->wowlan))
1082 return ERR_PTR(-EINVAL);
1083
1084 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1085 return ERR_PTR(-EINVAL);
1086
1087 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1088 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001089 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001090 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001091 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001092
1093 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1094 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1095
1096 err = ieee80211_key_link(key, sdata, NULL);
1097 if (err)
1098 return ERR_PTR(err);
1099
1100 return &key->conf;
1101}
1102EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);