blob: a907f2d5c12d857bf1811af24e57f5af09eb8665 [file] [log] [blame]
Johannes Berg1f5a7e472007-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 Berg1f5a7e472007-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 Berg1f5a7e472007-07-27 15:43:23 +020020#include <net/mac80211.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010021#include <asm/unaligned.h>
Johannes Berg1f5a7e472007-07-27 15:43:23 +020022#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020023#include "driver-ops.h"
Johannes Berg1f5a7e472007-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 Berg1f5a7e472007-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
Michal Kaziorf9dca802015-05-13 09:16:48 +000061static void
62update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta)
63{
64 struct ieee80211_sub_if_data *vlan;
65
66 if (sdata->vif.type != NL80211_IFTYPE_AP)
67 return;
68
69 mutex_lock(&sdata->local->mtx);
70
71 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
72 vlan->crypto_tx_tailroom_needed_cnt += delta;
73
74 mutex_unlock(&sdata->local->mtx);
75}
76
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053077static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
78{
79 /*
80 * When this count is zero, SKB resizing for allocating tailroom
81 * for IV or MMIC is skipped. But, this check has created two race
82 * cases in xmit path while transiting from zero count to one:
83 *
84 * 1. SKB resize was skipped because no key was added but just before
85 * the xmit key is added and SW encryption kicks off.
86 *
87 * 2. SKB resize was skipped because all the keys were hw planted but
88 * just before xmit one of the key is deleted and SW encryption kicks
89 * off.
90 *
91 * In both the above case SW encryption will find not enough space for
92 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
93 *
94 * Solution has been explained at
95 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
96 */
97
Michal Kaziorf9dca802015-05-13 09:16:48 +000098 update_vlan_tailroom_need_count(sdata, 1);
99
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530100 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
101 /*
102 * Flush all XMIT packets currently using HW encryption or no
103 * encryption at all if the count transition is from 0 -> 1.
104 */
105 synchronize_net();
106 }
107}
108
Michal Kaziorf9dca802015-05-13 09:16:48 +0000109static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata,
110 int delta)
111{
112 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta);
113
114 update_vlan_tailroom_need_count(sdata, -delta);
115 sdata->crypto_tx_tailroom_needed_cnt -= delta;
116}
117
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300118static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400119{
Johannes Bergdc822b52008-12-29 12:55:09 +0100120 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100121 struct sta_info *sta;
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100122 int ret = -EOPNOTSUPP;
Johannes Berg11a843b2007-08-28 17:01:55 -0400123
Johannes Berg3b967662008-04-08 17:56:52 +0200124 might_sleep();
125
Johannes Berg46191942014-10-13 13:43:29 +0200126 if (key->flags & KEY_FLAG_TAINTED) {
127 /* If we get here, it's during resume and the key is
128 * tainted so shouldn't be used/programmed any more.
129 * However, its flags may still indicate that it was
130 * programmed into the device (since we're in resume)
131 * so clear that flag now to avoid trying to remove
132 * it again later.
133 */
134 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200135 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200136 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200137
Johannes Berge31b8212010-10-05 19:39:30 +0200138 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300139 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400140
Johannes Bergad0e2b52010-06-01 10:19:19 +0200141 assert_key_lock(key->local);
142
Johannes Berg89c91ca2012-01-20 13:55:19 +0100143 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100144
Johannes Berge31b8212010-10-05 19:39:30 +0200145 /*
146 * If this is a per-STA GTK, check if it
147 * is supported; if not, return.
148 */
149 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
150 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
151 goto out_unsupported;
152
Johannes Berg89c91ca2012-01-20 13:55:19 +0100153 if (sta && !sta->uploaded)
154 goto out_unsupported;
155
Johannes Bergdc822b52008-12-29 12:55:09 +0100156 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +0100157 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
158 /*
159 * The driver doesn't know anything about VLAN interfaces.
160 * Hence, don't send GTKs for VLAN interfaces to the driver.
161 */
162 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
163 goto out_unsupported;
Helmut Schaa18890d42010-11-19 08:11:01 +0100164 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400165
Johannes Berg89c91ca2012-01-20 13:55:19 +0100166 ret = drv_set_key(key->local, SET_KEY, sdata,
167 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400168
Johannes Berge31b8212010-10-05 19:39:30 +0200169 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400170 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530171
Johannes Berg1e359a52015-01-05 10:28:49 +0100172 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500173 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Michal Kaziorf9dca802015-05-13 09:16:48 +0000174 decrease_tailroom_need_count(sdata, 1);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530175
Arik Nemtsov077a9152011-10-23 08:21:41 +0200176 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
177 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
178
Johannes Berge31b8212010-10-05 19:39:30 +0200179 return 0;
180 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400181
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100182 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200183 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700184 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100185 key->conf.keyidx,
186 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300187
Johannes Berge31b8212010-10-05 19:39:30 +0200188 out_unsupported:
189 switch (key->conf.cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 case WLAN_CIPHER_SUITE_WEP104:
192 case WLAN_CIPHER_SUITE_TKIP:
193 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200194 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berge31b8212010-10-05 19:39:30 +0200195 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200196 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200197 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
198 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200199 case WLAN_CIPHER_SUITE_GCMP:
200 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergfa7e1fb2015-01-22 18:44:19 +0100201 /* all of these we can do in software - if driver can */
202 if (ret == 1)
203 return 0;
204 if (key->local->hw.flags & IEEE80211_HW_SW_CRYPTO_CONTROL)
205 return -EINVAL;
Johannes Berge31b8212010-10-05 19:39:30 +0200206 return 0;
207 default:
208 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300209 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400210}
211
212static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
213{
Johannes Bergdc822b52008-12-29 12:55:09 +0100214 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100215 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400216 int ret;
217
Johannes Berg3b967662008-04-08 17:56:52 +0200218 might_sleep();
219
Johannes Bergdb4d1162008-02-25 16:27:45 +0100220 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400221 return;
222
Johannes Bergad0e2b52010-06-01 10:19:19 +0200223 assert_key_lock(key->local);
224
225 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400226 return;
227
Johannes Berg89c91ca2012-01-20 13:55:19 +0100228 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100229 sdata = key->sdata;
230
Johannes Berg1e359a52015-01-05 10:28:49 +0100231 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500232 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530233 increment_tailroom_need_count(sdata);
234
Johannes Berg12375ef2009-11-25 20:30:31 +0100235 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100236 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400237
238 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200239 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700240 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100241 key->conf.keyidx,
242 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400243
Johannes Berg3b967662008-04-08 17:56:52 +0200244 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200245}
246
247static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100248 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200249{
250 struct ieee80211_key *key = NULL;
251
Johannes Bergad0e2b52010-06-01 10:19:19 +0200252 assert_key_lock(sdata->local);
253
Johannes Berg3b967662008-04-08 17:56:52 +0200254 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200255 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200256
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300257 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100258 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300259 drv_set_default_unicast_key(sdata->local, sdata, idx);
260 }
261
Johannes Bergf7e01042010-12-09 19:49:02 +0100262 if (multi)
263 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200264
Johannes Bergf7e01042010-12-09 19:49:02 +0100265 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200266}
267
Johannes Bergf7e01042010-12-09 19:49:02 +0100268void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
269 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200270{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200271 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100272 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200273 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200274}
275
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200276static void
277__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
278{
279 struct ieee80211_key *key = NULL;
280
Johannes Bergad0e2b52010-06-01 10:19:19 +0200281 assert_key_lock(sdata->local);
282
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200283 if (idx >= NUM_DEFAULT_KEYS &&
284 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200285 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200286
287 rcu_assign_pointer(sdata->default_mgmt_key, key);
288
Johannes Bergf7e01042010-12-09 19:49:02 +0100289 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200290}
291
292void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
293 int idx)
294{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200295 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200296 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200297 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200298}
299
Johannes Berg3b967662008-04-08 17:56:52 +0200300
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100301static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
302 struct sta_info *sta,
303 bool pairwise,
304 struct ieee80211_key *old,
305 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200306{
Johannes Bergf7e01042010-12-09 19:49:02 +0100307 int idx;
308 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200309
Johannes Berg5282c3b2013-10-29 10:00:08 +0100310 /* caller must provide at least one old/new */
311 if (WARN_ON(!new && !old))
312 return;
313
Johannes Berg3b967662008-04-08 17:56:52 +0200314 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200315 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200316
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200317 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
318
319 if (old)
320 idx = old->conf.keyidx;
321 else
322 idx = new->conf.keyidx;
323
324 if (sta) {
325 if (pairwise) {
326 rcu_assign_pointer(sta->ptk[idx], new);
327 sta->ptk_idx = idx;
328 } else {
329 rcu_assign_pointer(sta->gtk[idx], new);
330 sta->gtk_idx = idx;
331 }
Johannes Berg3b967662008-04-08 17:56:52 +0200332 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200333 defunikey = old &&
334 old == key_mtx_dereference(sdata->local,
335 sdata->default_unicast_key);
336 defmultikey = old &&
337 old == key_mtx_dereference(sdata->local,
338 sdata->default_multicast_key);
339 defmgmtkey = old &&
340 old == key_mtx_dereference(sdata->local,
341 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200342
Johannes Bergf7e01042010-12-09 19:49:02 +0100343 if (defunikey && !new)
344 __ieee80211_set_default_key(sdata, -1, true, false);
345 if (defmultikey && !new)
346 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200347 if (defmgmtkey && !new)
348 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200349
350 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100351 if (defunikey && new)
352 __ieee80211_set_default_key(sdata, new->conf.keyidx,
353 true, false);
354 if (defmultikey && new)
355 __ieee80211_set_default_key(sdata, new->conf.keyidx,
356 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200357 if (defmgmtkey && new)
358 __ieee80211_set_default_mgmt_key(sdata,
359 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200360 }
361
Johannes Bergb5c34f62011-01-03 19:51:09 +0100362 if (old)
363 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400364}
365
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200366struct ieee80211_key *
367ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
368 const u8 *key_data,
369 size_t seq_len, const u8 *seq,
370 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200371{
372 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100373 int i, j, err;
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200374
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200375 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
376 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400377
378 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200379 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100380 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400381
382 /*
383 * Default to software encryption; we'll later upload the
384 * key to the hardware if possible.
385 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400386 key->conf.flags = 0;
387 key->flags = 0;
388
Johannes Berg97359d12010-08-10 09:46:38 +0200389 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400390 key->conf.keyidx = idx;
391 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200392 switch (cipher) {
393 case WLAN_CIPHER_SUITE_WEP40:
394 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200395 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
396 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200397 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200398 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200399 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
400 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300401 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100402 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300403 key->u.tkip.rx[i].iv32 =
404 get_unaligned_le32(&seq[2]);
405 key->u.tkip.rx[i].iv16 =
406 get_unaligned_le16(seq);
407 }
408 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200409 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200410 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200411 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200412 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
413 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300414 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100415 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200416 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300417 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200418 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300419 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400420 /*
421 * Initialize AES key state here as an optimization so that
422 * it does not need to be initialized for every packet.
423 */
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200424 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
425 key_data, key_len, IEEE80211_CCMP_MIC_LEN);
426 if (IS_ERR(key->u.ccmp.tfm)) {
427 err = PTR_ERR(key->u.ccmp.tfm);
428 kfree(key);
429 return ERR_PTR(err);
430 }
431 break;
432 case WLAN_CIPHER_SUITE_CCMP_256:
433 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN;
434 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN;
435 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
436 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++)
437 key->u.ccmp.rx_pn[i][j] =
438 seq[IEEE80211_CCMP_256_PN_LEN - j - 1];
439 /* Initialize AES key state here as an optimization so that
440 * it does not need to be initialized for every packet.
441 */
442 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
443 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100444 if (IS_ERR(key->u.ccmp.tfm)) {
445 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200446 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200447 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400448 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200449 break;
450 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200451 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg60ae0f22010-08-10 09:46:39 +0200452 key->conf.iv_len = 0;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200453 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC)
454 key->conf.icv_len = sizeof(struct ieee80211_mmie);
455 else
456 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
Johannes Berg60ae0f22010-08-10 09:46:39 +0200457 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200458 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100459 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200460 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200461 /*
462 * Initialize AES key state here as an optimization so that
463 * it does not need to be initialized for every packet.
464 */
465 key->u.aes_cmac.tfm =
Jouni Malinen56c52da2015-01-24 19:52:08 +0200466 ieee80211_aes_cmac_key_setup(key_data, key_len);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100467 if (IS_ERR(key->u.aes_cmac.tfm)) {
468 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200469 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200470 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200471 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200472 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200473 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
474 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
475 key->conf.iv_len = 0;
476 key->conf.icv_len = sizeof(struct ieee80211_mmie_16);
477 if (seq)
478 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++)
479 key->u.aes_gmac.rx_pn[j] =
480 seq[IEEE80211_GMAC_PN_LEN - j - 1];
481 /* Initialize AES key state here as an optimization so that
482 * it does not need to be initialized for every packet.
483 */
484 key->u.aes_gmac.tfm =
485 ieee80211_aes_gmac_key_setup(key_data, key_len);
486 if (IS_ERR(key->u.aes_gmac.tfm)) {
487 err = PTR_ERR(key->u.aes_gmac.tfm);
488 kfree(key);
489 return ERR_PTR(err);
490 }
491 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200492 case WLAN_CIPHER_SUITE_GCMP:
493 case WLAN_CIPHER_SUITE_GCMP_256:
494 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN;
495 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN;
496 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++)
497 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++)
498 key->u.gcmp.rx_pn[i][j] =
499 seq[IEEE80211_GCMP_PN_LEN - j - 1];
500 /* Initialize AES key state here as an optimization so that
501 * it does not need to be initialized for every packet.
502 */
503 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data,
504 key_len);
505 if (IS_ERR(key->u.gcmp.tfm)) {
506 err = PTR_ERR(key->u.gcmp.tfm);
507 kfree(key);
508 return ERR_PTR(err);
509 }
510 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200511 default:
512 if (cs) {
513 size_t len = (seq_len > MAX_PN_LEN) ?
514 MAX_PN_LEN : seq_len;
515
516 key->conf.iv_len = cs->hdr_len;
517 key->conf.icv_len = cs->mic_len;
518 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
519 for (j = 0; j < len; j++)
520 key->u.gen.rx_pn[i][j] =
521 seq[len - j - 1];
Cedric Izoardc7ef38e2015-03-17 10:47:33 +0000522 key->flags |= KEY_FLAG_CIPHER_SCHEME;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200523 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200524 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200525 memcpy(key->conf.key, key_data, key_len);
526 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200527
Johannes Bergdb4d1162008-02-25 16:27:45 +0100528 return key;
529}
Johannes Berg11a843b2007-08-28 17:01:55 -0400530
Johannes Berg79cf2df2013-03-06 22:53:52 +0100531static void ieee80211_key_free_common(struct ieee80211_key *key)
532{
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200533 switch (key->conf.cipher) {
534 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200535 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100536 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200537 break;
538 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200539 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg79cf2df2013-03-06 22:53:52 +0100540 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200541 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200542 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
543 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
544 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm);
545 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200546 case WLAN_CIPHER_SUITE_GCMP:
547 case WLAN_CIPHER_SUITE_GCMP_256:
548 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm);
549 break;
550 }
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300551 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100552}
553
Johannes Berg6d10e462013-03-06 23:09:11 +0100554static void __ieee80211_key_destroy(struct ieee80211_key *key,
555 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200556{
Jouni Malinen32162a42010-07-26 15:52:03 -0700557 if (key->local)
558 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200559
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530560 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100561 struct ieee80211_sub_if_data *sdata = key->sdata;
562
Jouni Malinen32162a42010-07-26 15:52:03 -0700563 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100564
565 if (delay_tailroom) {
566 /* see ieee80211_delayed_tailroom_dec */
567 sdata->crypto_tx_tailroom_pending_dec++;
568 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
569 HZ/2);
570 } else {
Michal Kaziorf9dca802015-05-13 09:16:48 +0000571 decrease_tailroom_need_count(sdata, 1);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100572 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530573 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200574
Johannes Berg79cf2df2013-03-06 22:53:52 +0100575 ieee80211_key_free_common(key);
576}
577
Johannes Berg6d10e462013-03-06 23:09:11 +0100578static void ieee80211_key_destroy(struct ieee80211_key *key,
579 bool delay_tailroom)
580{
581 if (!key)
582 return;
583
584 /*
585 * Synchronize so the TX path can no longer be using
586 * this key before we free/remove it.
587 */
588 synchronize_net();
589
590 __ieee80211_key_destroy(key, delay_tailroom);
591}
592
Johannes Berg79cf2df2013-03-06 22:53:52 +0100593void ieee80211_key_free_unused(struct ieee80211_key *key)
594{
595 WARN_ON(key->sdata || key->local);
596 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200597}
598
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300599int ieee80211_key_link(struct ieee80211_key *key,
600 struct ieee80211_sub_if_data *sdata,
601 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100602{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200603 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100604 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300605 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100606 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100607
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100608 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100609 idx = key->conf.keyidx;
610 key->local = sdata->local;
611 key->sdata = sdata;
612 key->sta = sta;
613
Johannes Bergad0e2b52010-06-01 10:19:19 +0200614 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200615
Johannes Berge31b8212010-10-05 19:39:30 +0200616 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200617 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200618 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200619 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400620 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200621 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100622
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530623 increment_tailroom_need_count(sdata);
624
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100625 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
626 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400627
Johannes Bergad0e2b52010-06-01 10:19:19 +0200628 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200629
Johannes Berg27b3eb92013-08-07 20:11:55 +0200630 if (!local->wowlan) {
631 ret = ieee80211_key_enable_hw_accel(key);
632 if (ret)
633 ieee80211_key_free(key, true);
634 } else {
635 ret = 0;
636 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100637
Johannes Bergad0e2b52010-06-01 10:19:19 +0200638 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300639
640 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200641}
642
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100643void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200644{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200645 if (!key)
646 return;
647
Johannes Berg3a245762008-04-09 16:45:37 +0200648 /*
649 * Replace key with nothingness if it was ever used.
650 */
651 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100652 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200653 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
654 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100655 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200656}
657
Johannes Berg3b967662008-04-08 17:56:52 +0200658void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
659{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200660 struct ieee80211_key *key;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000661 struct ieee80211_sub_if_data *vlan;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200662
Johannes Berg3a245762008-04-09 16:45:37 +0200663 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200664
Johannes Berg9607e6b2009-12-23 13:15:31 +0100665 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200666 return;
667
Johannes Bergad0e2b52010-06-01 10:19:19 +0200668 mutex_lock(&sdata->local->key_mtx);
669
Michal Kaziorf9dca802015-05-13 09:16:48 +0000670 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
671 sdata->crypto_tx_tailroom_pending_dec);
672
673 if (sdata->vif.type == NL80211_IFTYPE_AP) {
674 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
675 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
676 vlan->crypto_tx_tailroom_pending_dec);
677 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530678
679 list_for_each_entry(key, &sdata->key_list, list) {
680 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200681 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530682 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200683
684 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200685}
686
Michal Kaziorf9dca802015-05-13 09:16:48 +0000687void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata)
688{
689 struct ieee80211_sub_if_data *vlan;
690
691 mutex_lock(&sdata->local->key_mtx);
692
693 sdata->crypto_tx_tailroom_needed_cnt = 0;
694
695 if (sdata->vif.type == NL80211_IFTYPE_AP) {
696 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
697 vlan->crypto_tx_tailroom_needed_cnt = 0;
698 }
699
700 mutex_unlock(&sdata->local->key_mtx);
701}
702
Johannes Berg830af022011-07-05 16:35:39 +0200703void ieee80211_iter_keys(struct ieee80211_hw *hw,
704 struct ieee80211_vif *vif,
705 void (*iter)(struct ieee80211_hw *hw,
706 struct ieee80211_vif *vif,
707 struct ieee80211_sta *sta,
708 struct ieee80211_key_conf *key,
709 void *data),
710 void *iter_data)
711{
712 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200713 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200714 struct ieee80211_sub_if_data *sdata;
715
716 ASSERT_RTNL();
717
718 mutex_lock(&local->key_mtx);
719 if (vif) {
720 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200721 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200722 iter(hw, &sdata->vif,
723 key->sta ? &key->sta->sta : NULL,
724 &key->conf, iter_data);
725 } else {
726 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200727 list_for_each_entry_safe(key, tmp,
728 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200729 iter(hw, &sdata->vif,
730 key->sta ? &key->sta->sta : NULL,
731 &key->conf, iter_data);
732 }
733 mutex_unlock(&local->key_mtx);
734}
735EXPORT_SYMBOL(ieee80211_iter_keys);
736
Johannes Berg7907c7d2013-12-04 23:47:09 +0100737static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
738 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400739{
740 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200741
Michal Kaziorf9dca802015-05-13 09:16:48 +0000742 decrease_tailroom_need_count(sdata,
743 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100744 sdata->crypto_tx_tailroom_pending_dec = 0;
745
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200746 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400747
Johannes Berg6d10e462013-03-06 23:09:11 +0100748 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
749 ieee80211_key_replace(key->sdata, key->sta,
750 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
751 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100752 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100753 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400754
Johannes Bergf7e01042010-12-09 19:49:02 +0100755 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100756}
Johannes Bergf7e01042010-12-09 19:49:02 +0100757
Johannes Berg7907c7d2013-12-04 23:47:09 +0100758void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
759 bool force_synchronize)
760{
761 struct ieee80211_local *local = sdata->local;
762 struct ieee80211_sub_if_data *vlan;
Michal Kaziorf9dca802015-05-13 09:16:48 +0000763 struct ieee80211_sub_if_data *master;
Johannes Berg7907c7d2013-12-04 23:47:09 +0100764 struct ieee80211_key *key, *tmp;
765 LIST_HEAD(keys);
766
767 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
768
769 mutex_lock(&local->key_mtx);
770
771 ieee80211_free_keys_iface(sdata, &keys);
772
773 if (sdata->vif.type == NL80211_IFTYPE_AP) {
774 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
775 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100776 }
777
Johannes Berg7907c7d2013-12-04 23:47:09 +0100778 if (!list_empty(&keys) || force_synchronize)
779 synchronize_net();
780 list_for_each_entry_safe(key, tmp, &keys, list)
781 __ieee80211_key_destroy(key, false);
782
Michal Kaziorf9dca802015-05-13 09:16:48 +0000783 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
784 if (sdata->bss) {
785 master = container_of(sdata->bss,
786 struct ieee80211_sub_if_data,
787 u.ap);
788
789 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt !=
790 master->crypto_tx_tailroom_needed_cnt);
791 }
792 } else {
793 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
794 sdata->crypto_tx_tailroom_pending_dec);
795 }
796
Johannes Berg7907c7d2013-12-04 23:47:09 +0100797 if (sdata->vif.type == NL80211_IFTYPE_AP) {
798 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
799 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
800 vlan->crypto_tx_tailroom_pending_dec);
801 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100802
Johannes Berg7907c7d2013-12-04 23:47:09 +0100803 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400804}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200805
Johannes Berg6d10e462013-03-06 23:09:11 +0100806void ieee80211_free_sta_keys(struct ieee80211_local *local,
807 struct sta_info *sta)
808{
Johannes Bergc8782072013-12-04 23:05:45 +0100809 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100810 int i;
811
812 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100813 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100814 key = key_mtx_dereference(local, sta->gtk[i]);
815 if (!key)
816 continue;
817 ieee80211_key_replace(key->sdata, key->sta,
818 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
819 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100820 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100821 }
822
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200823 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
824 key = key_mtx_dereference(local, sta->ptk[i]);
825 if (!key)
826 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100827 ieee80211_key_replace(key->sdata, key->sta,
828 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
829 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100830 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100831 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100832
833 mutex_unlock(&local->key_mtx);
834}
835
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100836void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
837{
838 struct ieee80211_sub_if_data *sdata;
839
840 sdata = container_of(wk, struct ieee80211_sub_if_data,
841 dec_tailroom_needed_wk.work);
842
843 /*
844 * The reason for the delayed tailroom needed decrementing is to
845 * make roaming faster: during roaming, all keys are first deleted
846 * and then new keys are installed. The first new key causes the
847 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
848 * the cost of synchronize_net() (which can be slow). Avoid this
849 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
850 * key removal for a while, so if we roam the value is larger than
851 * zero and no 0->1 transition happens.
852 *
853 * The cost is that if the AP switching was from an AP with keys
854 * to one without, we still allocate tailroom while it would no
855 * longer be needed. However, in the typical (fast) roaming case
856 * within an ESS this usually won't happen.
857 */
858
859 mutex_lock(&sdata->local->key_mtx);
Michal Kaziorf9dca802015-05-13 09:16:48 +0000860 decrease_tailroom_need_count(sdata,
861 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100862 sdata->crypto_tx_tailroom_pending_dec = 0;
863 mutex_unlock(&sdata->local->key_mtx);
864}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200865
866void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
867 const u8 *replay_ctr, gfp_t gfp)
868{
869 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
870
871 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
872
873 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
874}
875EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200876
877void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
878 struct ieee80211_key_seq *seq)
879{
880 struct ieee80211_key *key;
881 u64 pn64;
882
883 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
884 return;
885
886 key = container_of(keyconf, struct ieee80211_key, conf);
887
888 switch (key->conf.cipher) {
889 case WLAN_CIPHER_SUITE_TKIP:
890 seq->tkip.iv32 = key->u.tkip.tx.iv32;
891 seq->tkip.iv16 = key->u.tkip.tx.iv16;
892 break;
893 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200894 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200895 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
896 seq->ccmp.pn[5] = pn64;
897 seq->ccmp.pn[4] = pn64 >> 8;
898 seq->ccmp.pn[3] = pn64 >> 16;
899 seq->ccmp.pn[2] = pn64 >> 24;
900 seq->ccmp.pn[1] = pn64 >> 32;
901 seq->ccmp.pn[0] = pn64 >> 40;
902 break;
903 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200904 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200905 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
906 seq->ccmp.pn[5] = pn64;
907 seq->ccmp.pn[4] = pn64 >> 8;
908 seq->ccmp.pn[3] = pn64 >> 16;
909 seq->ccmp.pn[2] = pn64 >> 24;
910 seq->ccmp.pn[1] = pn64 >> 32;
911 seq->ccmp.pn[0] = pn64 >> 40;
912 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200913 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
914 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
915 pn64 = atomic64_read(&key->u.aes_gmac.tx_pn);
916 seq->ccmp.pn[5] = pn64;
917 seq->ccmp.pn[4] = pn64 >> 8;
918 seq->ccmp.pn[3] = pn64 >> 16;
919 seq->ccmp.pn[2] = pn64 >> 24;
920 seq->ccmp.pn[1] = pn64 >> 32;
921 seq->ccmp.pn[0] = pn64 >> 40;
922 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200923 case WLAN_CIPHER_SUITE_GCMP:
924 case WLAN_CIPHER_SUITE_GCMP_256:
925 pn64 = atomic64_read(&key->u.gcmp.tx_pn);
926 seq->gcmp.pn[5] = pn64;
927 seq->gcmp.pn[4] = pn64 >> 8;
928 seq->gcmp.pn[3] = pn64 >> 16;
929 seq->gcmp.pn[2] = pn64 >> 24;
930 seq->gcmp.pn[1] = pn64 >> 32;
931 seq->gcmp.pn[0] = pn64 >> 40;
932 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200933 default:
934 WARN_ON(1);
935 }
936}
937EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
938
939void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
940 int tid, struct ieee80211_key_seq *seq)
941{
942 struct ieee80211_key *key;
943 const u8 *pn;
944
945 key = container_of(keyconf, struct ieee80211_key, conf);
946
947 switch (key->conf.cipher) {
948 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100949 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200950 return;
951 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
952 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
953 break;
954 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200955 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg5a306f52012-11-14 23:22:21 +0100956 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200957 return;
958 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100959 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200960 else
961 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200962 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200963 break;
964 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200965 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg3ea542d2011-07-07 18:58:00 +0200966 if (WARN_ON(tid != 0))
967 return;
968 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200969 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200970 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +0200971 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
972 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
973 if (WARN_ON(tid != 0))
974 return;
975 pn = key->u.aes_gmac.rx_pn;
976 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN);
977 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200978 case WLAN_CIPHER_SUITE_GCMP:
979 case WLAN_CIPHER_SUITE_GCMP_256:
980 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
981 return;
982 if (tid < 0)
983 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
984 else
985 pn = key->u.gcmp.rx_pn[tid];
986 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN);
987 break;
Johannes Berg3ea542d2011-07-07 18:58:00 +0200988 }
989}
990EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200991
992void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
993 struct ieee80211_key_seq *seq)
994{
995 struct ieee80211_key *key;
996 u64 pn64;
997
998 key = container_of(keyconf, struct ieee80211_key, conf);
999
1000 switch (key->conf.cipher) {
1001 case WLAN_CIPHER_SUITE_TKIP:
1002 key->u.tkip.tx.iv32 = seq->tkip.iv32;
1003 key->u.tkip.tx.iv16 = seq->tkip.iv16;
1004 break;
1005 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001006 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001007 pn64 = (u64)seq->ccmp.pn[5] |
1008 ((u64)seq->ccmp.pn[4] << 8) |
1009 ((u64)seq->ccmp.pn[3] << 16) |
1010 ((u64)seq->ccmp.pn[2] << 24) |
1011 ((u64)seq->ccmp.pn[1] << 32) |
1012 ((u64)seq->ccmp.pn[0] << 40);
1013 atomic64_set(&key->u.ccmp.tx_pn, pn64);
1014 break;
1015 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001016 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001017 pn64 = (u64)seq->aes_cmac.pn[5] |
1018 ((u64)seq->aes_cmac.pn[4] << 8) |
1019 ((u64)seq->aes_cmac.pn[3] << 16) |
1020 ((u64)seq->aes_cmac.pn[2] << 24) |
1021 ((u64)seq->aes_cmac.pn[1] << 32) |
1022 ((u64)seq->aes_cmac.pn[0] << 40);
1023 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
1024 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001025 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1026 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1027 pn64 = (u64)seq->aes_gmac.pn[5] |
1028 ((u64)seq->aes_gmac.pn[4] << 8) |
1029 ((u64)seq->aes_gmac.pn[3] << 16) |
1030 ((u64)seq->aes_gmac.pn[2] << 24) |
1031 ((u64)seq->aes_gmac.pn[1] << 32) |
1032 ((u64)seq->aes_gmac.pn[0] << 40);
1033 atomic64_set(&key->u.aes_gmac.tx_pn, pn64);
1034 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001035 case WLAN_CIPHER_SUITE_GCMP:
1036 case WLAN_CIPHER_SUITE_GCMP_256:
1037 pn64 = (u64)seq->gcmp.pn[5] |
1038 ((u64)seq->gcmp.pn[4] << 8) |
1039 ((u64)seq->gcmp.pn[3] << 16) |
1040 ((u64)seq->gcmp.pn[2] << 24) |
1041 ((u64)seq->gcmp.pn[1] << 32) |
1042 ((u64)seq->gcmp.pn[0] << 40);
1043 atomic64_set(&key->u.gcmp.tx_pn, pn64);
1044 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001045 default:
1046 WARN_ON(1);
1047 break;
1048 }
1049}
1050EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
1051
1052void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
1053 int tid, struct ieee80211_key_seq *seq)
1054{
1055 struct ieee80211_key *key;
1056 u8 *pn;
1057
1058 key = container_of(keyconf, struct ieee80211_key, conf);
1059
1060 switch (key->conf.cipher) {
1061 case WLAN_CIPHER_SUITE_TKIP:
1062 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
1063 return;
1064 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
1065 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
1066 break;
1067 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001068 case WLAN_CIPHER_SUITE_CCMP_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001069 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1070 return;
1071 if (tid < 0)
1072 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
1073 else
1074 pn = key->u.ccmp.rx_pn[tid];
1075 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
1076 break;
1077 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +02001078 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Johannes Berg27b3eb92013-08-07 20:11:55 +02001079 if (WARN_ON(tid != 0))
1080 return;
1081 pn = key->u.aes_cmac.rx_pn;
1082 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
1083 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001084 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1085 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1086 if (WARN_ON(tid != 0))
1087 return;
1088 pn = key->u.aes_gmac.rx_pn;
1089 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN);
1090 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001091 case WLAN_CIPHER_SUITE_GCMP:
1092 case WLAN_CIPHER_SUITE_GCMP_256:
1093 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
1094 return;
1095 if (tid < 0)
1096 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS];
1097 else
1098 pn = key->u.gcmp.rx_pn[tid];
1099 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN);
1100 break;
Johannes Berg27b3eb92013-08-07 20:11:55 +02001101 default:
1102 WARN_ON(1);
1103 break;
1104 }
1105}
1106EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
1107
1108void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
1109{
1110 struct ieee80211_key *key;
1111
1112 key = container_of(keyconf, struct ieee80211_key, conf);
1113
1114 assert_key_lock(key->local);
1115
1116 /*
1117 * if key was uploaded, we assume the driver will/has remove(d)
1118 * it, so adjust bookkeeping accordingly
1119 */
1120 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
1121 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
1122
Johannes Berg1e359a52015-01-05 10:28:49 +01001123 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -05001124 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +02001125 increment_tailroom_need_count(key->sdata);
1126 }
1127
1128 ieee80211_key_free(key, false);
1129}
1130EXPORT_SYMBOL_GPL(ieee80211_remove_key);
1131
1132struct ieee80211_key_conf *
1133ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
1134 struct ieee80211_key_conf *keyconf)
1135{
1136 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1137 struct ieee80211_local *local = sdata->local;
1138 struct ieee80211_key *key;
1139 int err;
1140
1141 if (WARN_ON(!local->wowlan))
1142 return ERR_PTR(-EINVAL);
1143
1144 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1145 return ERR_PTR(-EINVAL);
1146
1147 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
1148 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001149 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001150 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +02001151 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001152
1153 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
1154 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
1155
1156 err = ieee80211_key_link(key, sdata, NULL);
1157 if (err)
1158 return ERR_PTR(err);
1159
1160 return &key->conf;
1161}
1162EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);