blob: f8d9f0ee59bf1b549ba18c4be160bbf3533e2249 [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"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020027
Johannes Berg11a843b2007-08-28 17:01:55 -040028
Johannes Bergdbbea672008-02-26 14:34:06 +010029/**
30 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040031 *
32 * Key handling in mac80211 is done based on per-interface (sub_if_data)
33 * keys and per-station keys. Since each station belongs to an interface,
34 * each station key also belongs to that interface.
35 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010036 * Hardware acceleration is done on a best-effort basis for algorithms
37 * that are implemented in software, for each key the hardware is asked
38 * to enable that key for offloading but if it cannot do that the key is
39 * simply kept for software encryption (unless it is for an algorithm
40 * that isn't implemented in software).
41 * There is currently no way of knowing whether a key is handled in SW
42 * or HW except by looking into debugfs.
Johannes Berg11a843b2007-08-28 17:01:55 -040043 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010044 * All key management is internally protected by a mutex. Within all
45 * other parts of mac80211, key references are, just as STA structure
46 * references, protected by RCU. Note, however, that some things are
47 * unprotected, namely the key->sta dereferences within the hardware
48 * acceleration functions. This means that sta_info_destroy() must
49 * remove the key which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040050 */
51
52static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040053
Johannes Bergad0e2b52010-06-01 10:19:19 +020054static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020055{
Johannes Berg46a5eba2010-09-15 13:28:15 +020056 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020057}
58
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +053059static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
60{
61 /*
62 * When this count is zero, SKB resizing for allocating tailroom
63 * for IV or MMIC is skipped. But, this check has created two race
64 * cases in xmit path while transiting from zero count to one:
65 *
66 * 1. SKB resize was skipped because no key was added but just before
67 * the xmit key is added and SW encryption kicks off.
68 *
69 * 2. SKB resize was skipped because all the keys were hw planted but
70 * just before xmit one of the key is deleted and SW encryption kicks
71 * off.
72 *
73 * In both the above case SW encryption will find not enough space for
74 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
75 *
76 * Solution has been explained at
77 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
78 */
79
80 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
81 /*
82 * Flush all XMIT packets currently using HW encryption or no
83 * encryption at all if the count transition is from 0 -> 1.
84 */
85 synchronize_net();
86 }
87}
88
Johannes Berg3ffc2a92010-08-27 14:26:52 +030089static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040090{
Johannes Bergdc822b52008-12-29 12:55:09 +010091 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +010092 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040093 int ret;
94
Johannes Berg3b967662008-04-08 17:56:52 +020095 might_sleep();
96
Johannes Berg46191942014-10-13 13:43:29 +020097 if (key->flags & KEY_FLAG_TAINTED) {
98 /* If we get here, it's during resume and the key is
99 * tainted so shouldn't be used/programmed any more.
100 * However, its flags may still indicate that it was
101 * programmed into the device (since we're in resume)
102 * so clear that flag now to avoid trying to remove
103 * it again later.
104 */
105 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg27b3eb92013-08-07 20:11:55 +0200106 return -EINVAL;
Johannes Berg46191942014-10-13 13:43:29 +0200107 }
Johannes Berg27b3eb92013-08-07 20:11:55 +0200108
Johannes Berge31b8212010-10-05 19:39:30 +0200109 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300110 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -0400111
Johannes Bergad0e2b52010-06-01 10:19:19 +0200112 assert_key_lock(key->local);
113
Johannes Berg89c91ca2012-01-20 13:55:19 +0100114 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100115
Johannes Berge31b8212010-10-05 19:39:30 +0200116 /*
117 * If this is a per-STA GTK, check if it
118 * is supported; if not, return.
119 */
120 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
121 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
122 goto out_unsupported;
123
Johannes Berg89c91ca2012-01-20 13:55:19 +0100124 if (sta && !sta->uploaded)
125 goto out_unsupported;
126
Johannes Bergdc822b52008-12-29 12:55:09 +0100127 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +0100128 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
129 /*
130 * The driver doesn't know anything about VLAN interfaces.
131 * Hence, don't send GTKs for VLAN interfaces to the driver.
132 */
133 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
134 goto out_unsupported;
Helmut Schaa18890d42010-11-19 08:11:01 +0100135 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400136
Johannes Berg89c91ca2012-01-20 13:55:19 +0100137 ret = drv_set_key(key->local, SET_KEY, sdata,
138 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400139
Johannes Berge31b8212010-10-05 19:39:30 +0200140 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400141 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530142
Johannes Berg1e359a52015-01-05 10:28:49 +0100143 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500144 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530145 sdata->crypto_tx_tailroom_needed_cnt--;
146
Arik Nemtsov077a9152011-10-23 08:21:41 +0200147 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
148 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
149
Johannes Berge31b8212010-10-05 19:39:30 +0200150 return 0;
151 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400152
Johannes Berge31b8212010-10-05 19:39:30 +0200153 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200154 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700155 "failed to set key (%d, %pM) to hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100156 key->conf.keyidx,
157 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300158
Johannes Berge31b8212010-10-05 19:39:30 +0200159 out_unsupported:
160 switch (key->conf.cipher) {
161 case WLAN_CIPHER_SUITE_WEP40:
162 case WLAN_CIPHER_SUITE_WEP104:
163 case WLAN_CIPHER_SUITE_TKIP:
164 case WLAN_CIPHER_SUITE_CCMP:
165 case WLAN_CIPHER_SUITE_AES_CMAC:
166 /* all of these we can do in software */
167 return 0;
168 default:
169 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300170 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400171}
172
173static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
174{
Johannes Bergdc822b52008-12-29 12:55:09 +0100175 struct ieee80211_sub_if_data *sdata;
Johannes Berg89c91ca2012-01-20 13:55:19 +0100176 struct sta_info *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400177 int ret;
178
Johannes Berg3b967662008-04-08 17:56:52 +0200179 might_sleep();
180
Johannes Bergdb4d1162008-02-25 16:27:45 +0100181 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400182 return;
183
Johannes Bergad0e2b52010-06-01 10:19:19 +0200184 assert_key_lock(key->local);
185
186 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400187 return;
188
Johannes Berg89c91ca2012-01-20 13:55:19 +0100189 sta = key->sta;
Johannes Bergdc822b52008-12-29 12:55:09 +0100190 sdata = key->sdata;
191
Johannes Berg1e359a52015-01-05 10:28:49 +0100192 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500193 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530194 increment_tailroom_need_count(sdata);
195
Johannes Berg12375ef2009-11-25 20:30:31 +0100196 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg89c91ca2012-01-20 13:55:19 +0100197 sta ? &sta->sta : NULL, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400198
199 if (ret)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200200 sdata_err(sdata,
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700201 "failed to remove key (%d, %pM) from hardware (%d)\n",
Johannes Berg89c91ca2012-01-20 13:55:19 +0100202 key->conf.keyidx,
203 sta ? sta->sta.addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400204
Johannes Berg3b967662008-04-08 17:56:52 +0200205 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200206}
207
208static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100209 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200210{
211 struct ieee80211_key *key = NULL;
212
Johannes Bergad0e2b52010-06-01 10:19:19 +0200213 assert_key_lock(sdata->local);
214
Johannes Berg3b967662008-04-08 17:56:52 +0200215 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200216 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200217
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300218 if (uni) {
Johannes Bergf7e01042010-12-09 19:49:02 +0100219 rcu_assign_pointer(sdata->default_unicast_key, key);
Yoni Divinskyde5fad82012-05-30 11:36:39 +0300220 drv_set_default_unicast_key(sdata->local, sdata, idx);
221 }
222
Johannes Bergf7e01042010-12-09 19:49:02 +0100223 if (multi)
224 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200225
Johannes Bergf7e01042010-12-09 19:49:02 +0100226 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200227}
228
Johannes Bergf7e01042010-12-09 19:49:02 +0100229void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
230 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200231{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200232 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100233 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200234 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200235}
236
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200237static void
238__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
239{
240 struct ieee80211_key *key = NULL;
241
Johannes Bergad0e2b52010-06-01 10:19:19 +0200242 assert_key_lock(sdata->local);
243
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200244 if (idx >= NUM_DEFAULT_KEYS &&
245 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200246 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200247
248 rcu_assign_pointer(sdata->default_mgmt_key, key);
249
Johannes Bergf7e01042010-12-09 19:49:02 +0100250 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200251}
252
253void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
254 int idx)
255{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200256 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200257 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200258 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200259}
260
Johannes Berg3b967662008-04-08 17:56:52 +0200261
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100262static void ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
263 struct sta_info *sta,
264 bool pairwise,
265 struct ieee80211_key *old,
266 struct ieee80211_key *new)
Johannes Berg3b967662008-04-08 17:56:52 +0200267{
Johannes Bergf7e01042010-12-09 19:49:02 +0100268 int idx;
269 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200270
Johannes Berg5282c3b2013-10-29 10:00:08 +0100271 /* caller must provide at least one old/new */
272 if (WARN_ON(!new && !old))
273 return;
274
Johannes Berg3b967662008-04-08 17:56:52 +0200275 if (new)
Johannes Bergf850e002011-07-13 19:50:53 +0200276 list_add_tail(&new->list, &sdata->key_list);
Johannes Berg3b967662008-04-08 17:56:52 +0200277
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200278 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
279
280 if (old)
281 idx = old->conf.keyidx;
282 else
283 idx = new->conf.keyidx;
284
285 if (sta) {
286 if (pairwise) {
287 rcu_assign_pointer(sta->ptk[idx], new);
288 sta->ptk_idx = idx;
289 } else {
290 rcu_assign_pointer(sta->gtk[idx], new);
291 sta->gtk_idx = idx;
292 }
Johannes Berg3b967662008-04-08 17:56:52 +0200293 } else {
Johannes Berg40b275b2011-05-13 14:15:49 +0200294 defunikey = old &&
295 old == key_mtx_dereference(sdata->local,
296 sdata->default_unicast_key);
297 defmultikey = old &&
298 old == key_mtx_dereference(sdata->local,
299 sdata->default_multicast_key);
300 defmgmtkey = old &&
301 old == key_mtx_dereference(sdata->local,
302 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200303
Johannes Bergf7e01042010-12-09 19:49:02 +0100304 if (defunikey && !new)
305 __ieee80211_set_default_key(sdata, -1, true, false);
306 if (defmultikey && !new)
307 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200308 if (defmgmtkey && !new)
309 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200310
311 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100312 if (defunikey && new)
313 __ieee80211_set_default_key(sdata, new->conf.keyidx,
314 true, false);
315 if (defmultikey && new)
316 __ieee80211_set_default_key(sdata, new->conf.keyidx,
317 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200318 if (defmgmtkey && new)
319 __ieee80211_set_default_mgmt_key(sdata,
320 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200321 }
322
Johannes Bergb5c34f62011-01-03 19:51:09 +0100323 if (old)
324 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400325}
326
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200327struct ieee80211_key *
328ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
329 const u8 *key_data,
330 size_t seq_len, const u8 *seq,
331 const struct ieee80211_cipher_scheme *cs)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200332{
333 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100334 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200335
Johannes Berg8c5bb1f2014-04-29 17:55:26 +0200336 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS))
337 return ERR_PTR(-EINVAL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400338
339 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200340 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100341 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400342
343 /*
344 * Default to software encryption; we'll later upload the
345 * key to the hardware if possible.
346 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400347 key->conf.flags = 0;
348 key->flags = 0;
349
Johannes Berg97359d12010-08-10 09:46:38 +0200350 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400351 key->conf.keyidx = idx;
352 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200353 switch (cipher) {
354 case WLAN_CIPHER_SUITE_WEP40:
355 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200356 key->conf.iv_len = IEEE80211_WEP_IV_LEN;
357 key->conf.icv_len = IEEE80211_WEP_ICV_LEN;
Felix Fietkau76708de2008-10-05 18:02:48 +0200358 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200359 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200360 key->conf.iv_len = IEEE80211_TKIP_IV_LEN;
361 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300362 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100363 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300364 key->u.tkip.rx[i].iv32 =
365 get_unaligned_le32(&seq[2]);
366 key->u.tkip.rx[i].iv16 =
367 get_unaligned_le16(seq);
368 }
369 }
Johannes Berg523b02e2011-07-07 22:28:01 +0200370 spin_lock_init(&key->u.tkip.txlock);
Felix Fietkau76708de2008-10-05 18:02:48 +0200371 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200372 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200373 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN;
374 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300375 if (seq) {
Johannes Berg5a306f52012-11-14 23:22:21 +0100376 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200377 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300378 key->u.ccmp.rx_pn[i][j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200379 seq[IEEE80211_CCMP_PN_LEN - j - 1];
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300380 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400381 /*
382 * Initialize AES key state here as an optimization so that
383 * it does not need to be initialized for every packet.
384 */
385 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100386 if (IS_ERR(key->u.ccmp.tfm)) {
387 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200388 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200389 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400390 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200391 break;
392 case WLAN_CIPHER_SUITE_AES_CMAC:
393 key->conf.iv_len = 0;
394 key->conf.icv_len = sizeof(struct ieee80211_mmie);
395 if (seq)
Johannes Berg4325f6c2013-05-08 13:09:08 +0200396 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++)
Johannes Berg0f927322012-11-14 23:15:11 +0100397 key->u.aes_cmac.rx_pn[j] =
Johannes Berg4325f6c2013-05-08 13:09:08 +0200398 seq[IEEE80211_CMAC_PN_LEN - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200399 /*
400 * Initialize AES key state here as an optimization so that
401 * it does not need to be initialized for every packet.
402 */
403 key->u.aes_cmac.tfm =
404 ieee80211_aes_cmac_key_setup(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100405 if (IS_ERR(key->u.aes_cmac.tfm)) {
406 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200407 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200408 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200409 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200410 break;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200411 default:
412 if (cs) {
413 size_t len = (seq_len > MAX_PN_LEN) ?
414 MAX_PN_LEN : seq_len;
415
416 key->conf.iv_len = cs->hdr_len;
417 key->conf.icv_len = cs->mic_len;
418 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
419 for (j = 0; j < len; j++)
420 key->u.gen.rx_pn[i][j] =
421 seq[len - j - 1];
422 }
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200423 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200424 memcpy(key->conf.key, key_data, key_len);
425 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200426
Johannes Bergdb4d1162008-02-25 16:27:45 +0100427 return key;
428}
Johannes Berg11a843b2007-08-28 17:01:55 -0400429
Johannes Berg79cf2df2013-03-06 22:53:52 +0100430static void ieee80211_key_free_common(struct ieee80211_key *key)
431{
432 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
433 ieee80211_aes_key_free(key->u.ccmp.tfm);
434 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
435 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Johannes Berg29c3f9c2014-09-10 13:39:55 +0300436 kzfree(key);
Johannes Berg79cf2df2013-03-06 22:53:52 +0100437}
438
Johannes Berg6d10e462013-03-06 23:09:11 +0100439static void __ieee80211_key_destroy(struct ieee80211_key *key,
440 bool delay_tailroom)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200441{
Jouni Malinen32162a42010-07-26 15:52:03 -0700442 if (key->local)
443 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200444
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530445 if (key->local) {
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100446 struct ieee80211_sub_if_data *sdata = key->sdata;
447
Jouni Malinen32162a42010-07-26 15:52:03 -0700448 ieee80211_debugfs_key_remove(key);
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100449
450 if (delay_tailroom) {
451 /* see ieee80211_delayed_tailroom_dec */
452 sdata->crypto_tx_tailroom_pending_dec++;
453 schedule_delayed_work(&sdata->dec_tailroom_needed_wk,
454 HZ/2);
455 } else {
456 sdata->crypto_tx_tailroom_needed_cnt--;
457 }
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530458 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200459
Johannes Berg79cf2df2013-03-06 22:53:52 +0100460 ieee80211_key_free_common(key);
461}
462
Johannes Berg6d10e462013-03-06 23:09:11 +0100463static void ieee80211_key_destroy(struct ieee80211_key *key,
464 bool delay_tailroom)
465{
466 if (!key)
467 return;
468
469 /*
470 * Synchronize so the TX path can no longer be using
471 * this key before we free/remove it.
472 */
473 synchronize_net();
474
475 __ieee80211_key_destroy(key, delay_tailroom);
476}
477
Johannes Berg79cf2df2013-03-06 22:53:52 +0100478void ieee80211_key_free_unused(struct ieee80211_key *key)
479{
480 WARN_ON(key->sdata || key->local);
481 ieee80211_key_free_common(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200482}
483
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300484int ieee80211_key_link(struct ieee80211_key *key,
485 struct ieee80211_sub_if_data *sdata,
486 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100487{
Johannes Berg27b3eb92013-08-07 20:11:55 +0200488 struct ieee80211_local *local = sdata->local;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100489 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300490 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100491 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100492
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100493 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100494 idx = key->conf.keyidx;
495 key->local = sdata->local;
496 key->sdata = sdata;
497 key->sta = sta;
498
Johannes Bergad0e2b52010-06-01 10:19:19 +0200499 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200500
Johannes Berge31b8212010-10-05 19:39:30 +0200501 if (sta && pairwise)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200502 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]);
Johannes Berge31b8212010-10-05 19:39:30 +0200503 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200504 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400505 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200506 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100507
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530508 increment_tailroom_need_count(sdata);
509
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100510 ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
511 ieee80211_key_destroy(old_key, true);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400512
Johannes Bergad0e2b52010-06-01 10:19:19 +0200513 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200514
Johannes Berg27b3eb92013-08-07 20:11:55 +0200515 if (!local->wowlan) {
516 ret = ieee80211_key_enable_hw_accel(key);
517 if (ret)
518 ieee80211_key_free(key, true);
519 } else {
520 ret = 0;
521 }
Johannes Berg79cf2df2013-03-06 22:53:52 +0100522
Johannes Bergad0e2b52010-06-01 10:19:19 +0200523 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300524
525 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200526}
527
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100528void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom)
Johannes Berg3a245762008-04-09 16:45:37 +0200529{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200530 if (!key)
531 return;
532
Johannes Berg3a245762008-04-09 16:45:37 +0200533 /*
534 * Replace key with nothingness if it was ever used.
535 */
536 if (key->sdata)
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100537 ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200538 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
539 key, NULL);
Johannes Berg3b8d9c22013-03-06 22:58:23 +0100540 ieee80211_key_destroy(key, delay_tailroom);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200541}
542
Johannes Berg3b967662008-04-08 17:56:52 +0200543void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
544{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200545 struct ieee80211_key *key;
546
Johannes Berg3a245762008-04-09 16:45:37 +0200547 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200548
Johannes Berg9607e6b662009-12-23 13:15:31 +0100549 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200550 return;
551
Johannes Bergad0e2b52010-06-01 10:19:19 +0200552 mutex_lock(&sdata->local->key_mtx);
553
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530554 sdata->crypto_tx_tailroom_needed_cnt = 0;
555
556 list_for_each_entry(key, &sdata->key_list, list) {
557 increment_tailroom_need_count(sdata);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200558 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +0530559 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200560
561 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200562}
563
Johannes Berg830af022011-07-05 16:35:39 +0200564void ieee80211_iter_keys(struct ieee80211_hw *hw,
565 struct ieee80211_vif *vif,
566 void (*iter)(struct ieee80211_hw *hw,
567 struct ieee80211_vif *vif,
568 struct ieee80211_sta *sta,
569 struct ieee80211_key_conf *key,
570 void *data),
571 void *iter_data)
572{
573 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200574 struct ieee80211_key *key, *tmp;
Johannes Berg830af022011-07-05 16:35:39 +0200575 struct ieee80211_sub_if_data *sdata;
576
577 ASSERT_RTNL();
578
579 mutex_lock(&local->key_mtx);
580 if (vif) {
581 sdata = vif_to_sdata(vif);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200582 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200583 iter(hw, &sdata->vif,
584 key->sta ? &key->sta->sta : NULL,
585 &key->conf, iter_data);
586 } else {
587 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg27b3eb92013-08-07 20:11:55 +0200588 list_for_each_entry_safe(key, tmp,
589 &sdata->key_list, list)
Johannes Berg830af022011-07-05 16:35:39 +0200590 iter(hw, &sdata->vif,
591 key->sta ? &key->sta->sta : NULL,
592 &key->conf, iter_data);
593 }
594 mutex_unlock(&local->key_mtx);
595}
596EXPORT_SYMBOL(ieee80211_iter_keys);
597
Johannes Berg7907c7d2013-12-04 23:47:09 +0100598static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata,
599 struct list_head *keys)
Johannes Berg11a843b2007-08-28 17:01:55 -0400600{
601 struct ieee80211_key *key, *tmp;
Johannes Berg3b967662008-04-08 17:56:52 +0200602
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100603 sdata->crypto_tx_tailroom_needed_cnt -=
604 sdata->crypto_tx_tailroom_pending_dec;
605 sdata->crypto_tx_tailroom_pending_dec = 0;
606
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200607 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400608
Johannes Berg6d10e462013-03-06 23:09:11 +0100609 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) {
610 ieee80211_key_replace(key->sdata, key->sta,
611 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
612 key, NULL);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100613 list_add_tail(&key->list, keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100614 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400615
Johannes Bergf7e01042010-12-09 19:49:02 +0100616 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100617}
Johannes Bergf7e01042010-12-09 19:49:02 +0100618
Johannes Berg7907c7d2013-12-04 23:47:09 +0100619void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata,
620 bool force_synchronize)
621{
622 struct ieee80211_local *local = sdata->local;
623 struct ieee80211_sub_if_data *vlan;
624 struct ieee80211_key *key, *tmp;
625 LIST_HEAD(keys);
626
627 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk);
628
629 mutex_lock(&local->key_mtx);
630
631 ieee80211_free_keys_iface(sdata, &keys);
632
633 if (sdata->vif.type == NL80211_IFTYPE_AP) {
634 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
635 ieee80211_free_keys_iface(vlan, &keys);
Johannes Berg6d10e462013-03-06 23:09:11 +0100636 }
637
Johannes Berg7907c7d2013-12-04 23:47:09 +0100638 if (!list_empty(&keys) || force_synchronize)
639 synchronize_net();
640 list_for_each_entry_safe(key, tmp, &keys, list)
641 __ieee80211_key_destroy(key, false);
642
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100643 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt ||
644 sdata->crypto_tx_tailroom_pending_dec);
Johannes Berg7907c7d2013-12-04 23:47:09 +0100645 if (sdata->vif.type == NL80211_IFTYPE_AP) {
646 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
647 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt ||
648 vlan->crypto_tx_tailroom_pending_dec);
649 }
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100650
Johannes Berg7907c7d2013-12-04 23:47:09 +0100651 mutex_unlock(&local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400652}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200653
Johannes Berg6d10e462013-03-06 23:09:11 +0100654void ieee80211_free_sta_keys(struct ieee80211_local *local,
655 struct sta_info *sta)
656{
Johannes Bergc8782072013-12-04 23:05:45 +0100657 struct ieee80211_key *key;
Johannes Berg6d10e462013-03-06 23:09:11 +0100658 int i;
659
660 mutex_lock(&local->key_mtx);
Johannes Berg28a9bc62014-12-17 13:55:49 +0100661 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) {
Johannes Berg6d10e462013-03-06 23:09:11 +0100662 key = key_mtx_dereference(local, sta->gtk[i]);
663 if (!key)
664 continue;
665 ieee80211_key_replace(key->sdata, key->sta,
666 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
667 key, NULL);
Johannes Bergc8782072013-12-04 23:05:45 +0100668 __ieee80211_key_destroy(key, true);
Johannes Berg6d10e462013-03-06 23:09:11 +0100669 }
670
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200671 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
672 key = key_mtx_dereference(local, sta->ptk[i]);
673 if (!key)
674 continue;
Johannes Berg6d10e462013-03-06 23:09:11 +0100675 ieee80211_key_replace(key->sdata, key->sta,
676 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
677 key, NULL);
Johannes Berg6d10e462013-03-06 23:09:11 +0100678 __ieee80211_key_destroy(key, true);
Johannes Bergc8782072013-12-04 23:05:45 +0100679 }
Johannes Berg6d10e462013-03-06 23:09:11 +0100680
681 mutex_unlock(&local->key_mtx);
682}
683
Johannes Berg8d1f7ec2013-02-23 00:59:03 +0100684void ieee80211_delayed_tailroom_dec(struct work_struct *wk)
685{
686 struct ieee80211_sub_if_data *sdata;
687
688 sdata = container_of(wk, struct ieee80211_sub_if_data,
689 dec_tailroom_needed_wk.work);
690
691 /*
692 * The reason for the delayed tailroom needed decrementing is to
693 * make roaming faster: during roaming, all keys are first deleted
694 * and then new keys are installed. The first new key causes the
695 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes
696 * the cost of synchronize_net() (which can be slow). Avoid this
697 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on
698 * key removal for a while, so if we roam the value is larger than
699 * zero and no 0->1 transition happens.
700 *
701 * The cost is that if the AP switching was from an AP with keys
702 * to one without, we still allocate tailroom while it would no
703 * longer be needed. However, in the typical (fast) roaming case
704 * within an ESS this usually won't happen.
705 */
706
707 mutex_lock(&sdata->local->key_mtx);
708 sdata->crypto_tx_tailroom_needed_cnt -=
709 sdata->crypto_tx_tailroom_pending_dec;
710 sdata->crypto_tx_tailroom_pending_dec = 0;
711 mutex_unlock(&sdata->local->key_mtx);
712}
Johannes Bergc68f4b82011-07-05 16:35:41 +0200713
714void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
715 const u8 *replay_ctr, gfp_t gfp)
716{
717 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
718
719 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
720
721 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
722}
723EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200724
725void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
726 struct ieee80211_key_seq *seq)
727{
728 struct ieee80211_key *key;
729 u64 pn64;
730
731 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
732 return;
733
734 key = container_of(keyconf, struct ieee80211_key, conf);
735
736 switch (key->conf.cipher) {
737 case WLAN_CIPHER_SUITE_TKIP:
738 seq->tkip.iv32 = key->u.tkip.tx.iv32;
739 seq->tkip.iv16 = key->u.tkip.tx.iv16;
740 break;
741 case WLAN_CIPHER_SUITE_CCMP:
742 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
743 seq->ccmp.pn[5] = pn64;
744 seq->ccmp.pn[4] = pn64 >> 8;
745 seq->ccmp.pn[3] = pn64 >> 16;
746 seq->ccmp.pn[2] = pn64 >> 24;
747 seq->ccmp.pn[1] = pn64 >> 32;
748 seq->ccmp.pn[0] = pn64 >> 40;
749 break;
750 case WLAN_CIPHER_SUITE_AES_CMAC:
751 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
752 seq->ccmp.pn[5] = pn64;
753 seq->ccmp.pn[4] = pn64 >> 8;
754 seq->ccmp.pn[3] = pn64 >> 16;
755 seq->ccmp.pn[2] = pn64 >> 24;
756 seq->ccmp.pn[1] = pn64 >> 32;
757 seq->ccmp.pn[0] = pn64 >> 40;
758 break;
759 default:
760 WARN_ON(1);
761 }
762}
763EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
764
765void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
766 int tid, struct ieee80211_key_seq *seq)
767{
768 struct ieee80211_key *key;
769 const u8 *pn;
770
771 key = container_of(keyconf, struct ieee80211_key, conf);
772
773 switch (key->conf.cipher) {
774 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100775 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200776 return;
777 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
778 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
779 break;
780 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg5a306f52012-11-14 23:22:21 +0100781 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
Johannes Berg3ea542d2011-07-07 18:58:00 +0200782 return;
783 if (tid < 0)
Johannes Berg5a306f52012-11-14 23:22:21 +0100784 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
Johannes Berg3ea542d2011-07-07 18:58:00 +0200785 else
786 pn = key->u.ccmp.rx_pn[tid];
Johannes Berg4325f6c2013-05-08 13:09:08 +0200787 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200788 break;
789 case WLAN_CIPHER_SUITE_AES_CMAC:
790 if (WARN_ON(tid != 0))
791 return;
792 pn = key->u.aes_cmac.rx_pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +0200793 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN);
Johannes Berg3ea542d2011-07-07 18:58:00 +0200794 break;
795 }
796}
797EXPORT_SYMBOL(ieee80211_get_key_rx_seq);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200798
799void ieee80211_set_key_tx_seq(struct ieee80211_key_conf *keyconf,
800 struct ieee80211_key_seq *seq)
801{
802 struct ieee80211_key *key;
803 u64 pn64;
804
805 key = container_of(keyconf, struct ieee80211_key, conf);
806
807 switch (key->conf.cipher) {
808 case WLAN_CIPHER_SUITE_TKIP:
809 key->u.tkip.tx.iv32 = seq->tkip.iv32;
810 key->u.tkip.tx.iv16 = seq->tkip.iv16;
811 break;
812 case WLAN_CIPHER_SUITE_CCMP:
813 pn64 = (u64)seq->ccmp.pn[5] |
814 ((u64)seq->ccmp.pn[4] << 8) |
815 ((u64)seq->ccmp.pn[3] << 16) |
816 ((u64)seq->ccmp.pn[2] << 24) |
817 ((u64)seq->ccmp.pn[1] << 32) |
818 ((u64)seq->ccmp.pn[0] << 40);
819 atomic64_set(&key->u.ccmp.tx_pn, pn64);
820 break;
821 case WLAN_CIPHER_SUITE_AES_CMAC:
822 pn64 = (u64)seq->aes_cmac.pn[5] |
823 ((u64)seq->aes_cmac.pn[4] << 8) |
824 ((u64)seq->aes_cmac.pn[3] << 16) |
825 ((u64)seq->aes_cmac.pn[2] << 24) |
826 ((u64)seq->aes_cmac.pn[1] << 32) |
827 ((u64)seq->aes_cmac.pn[0] << 40);
828 atomic64_set(&key->u.aes_cmac.tx_pn, pn64);
829 break;
830 default:
831 WARN_ON(1);
832 break;
833 }
834}
835EXPORT_SYMBOL_GPL(ieee80211_set_key_tx_seq);
836
837void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf,
838 int tid, struct ieee80211_key_seq *seq)
839{
840 struct ieee80211_key *key;
841 u8 *pn;
842
843 key = container_of(keyconf, struct ieee80211_key, conf);
844
845 switch (key->conf.cipher) {
846 case WLAN_CIPHER_SUITE_TKIP:
847 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS))
848 return;
849 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32;
850 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16;
851 break;
852 case WLAN_CIPHER_SUITE_CCMP:
853 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS))
854 return;
855 if (tid < 0)
856 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS];
857 else
858 pn = key->u.ccmp.rx_pn[tid];
859 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN);
860 break;
861 case WLAN_CIPHER_SUITE_AES_CMAC:
862 if (WARN_ON(tid != 0))
863 return;
864 pn = key->u.aes_cmac.rx_pn;
865 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN);
866 break;
867 default:
868 WARN_ON(1);
869 break;
870 }
871}
872EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq);
873
874void ieee80211_remove_key(struct ieee80211_key_conf *keyconf)
875{
876 struct ieee80211_key *key;
877
878 key = container_of(keyconf, struct ieee80211_key, conf);
879
880 assert_key_lock(key->local);
881
882 /*
883 * if key was uploaded, we assume the driver will/has remove(d)
884 * it, so adjust bookkeeping accordingly
885 */
886 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
887 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
888
Johannes Berg1e359a52015-01-05 10:28:49 +0100889 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
Ido Yarivdb128472015-01-06 08:39:02 -0500890 (key->conf.flags & IEEE80211_KEY_FLAG_RESERVE_TAILROOM)))
Johannes Berg27b3eb92013-08-07 20:11:55 +0200891 increment_tailroom_need_count(key->sdata);
892 }
893
894 ieee80211_key_free(key, false);
895}
896EXPORT_SYMBOL_GPL(ieee80211_remove_key);
897
898struct ieee80211_key_conf *
899ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
900 struct ieee80211_key_conf *keyconf)
901{
902 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
903 struct ieee80211_local *local = sdata->local;
904 struct ieee80211_key *key;
905 int err;
906
907 if (WARN_ON(!local->wowlan))
908 return ERR_PTR(-EINVAL);
909
910 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
911 return ERR_PTR(-EINVAL);
912
913 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx,
914 keyconf->keylen, keyconf->key,
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200915 0, NULL, NULL);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200916 if (IS_ERR(key))
Johannes Bergc5dc1642013-08-28 19:03:36 +0200917 return ERR_CAST(key);
Johannes Berg27b3eb92013-08-07 20:11:55 +0200918
919 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
920 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
921
922 err = ieee80211_key_link(key, sdata, NULL);
923 if (err)
924 return ERR_PTR(err);
925
926 return &key->conf;
927}
928EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add);