blob: 84cf9196820fe70b546febee2f9e61ebb79ef501 [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 Berg1f5a7e42007-07-27 15:43:23 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Johannes Berg11a843b2007-08-28 17:01:55 -040012#include <linux/if_ether.h>
13#include <linux/etherdevice.h>
14#include <linux/list.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040015#include <linux/rcupdate.h>
Johannes Bergdb4d1162008-02-25 16:27:45 +010016#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020018#include <net/mac80211.h>
19#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020020#include "driver-ops.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020021#include "debugfs_key.h"
22#include "aes_ccm.h"
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +020023#include "aes_cmac.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020024
Johannes Berg11a843b2007-08-28 17:01:55 -040025
Johannes Bergdbbea672008-02-26 14:34:06 +010026/**
27 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040028 *
29 * Key handling in mac80211 is done based on per-interface (sub_if_data)
30 * keys and per-station keys. Since each station belongs to an interface,
31 * each station key also belongs to that interface.
32 *
33 * Hardware acceleration is done on a best-effort basis, for each key
34 * that is eligible the hardware is asked to enable that key but if
35 * it cannot do that they key is simply kept for software encryption.
36 * There is currently no way of knowing this except by looking into
37 * debugfs.
38 *
Johannes Bergad0e2b52010-06-01 10:19:19 +020039 * All key operations are protected internally.
Johannes Bergdb4d1162008-02-25 16:27:45 +010040 *
Johannes Berg3b967662008-04-08 17:56:52 +020041 * Within mac80211, key references are, just as STA structure references,
42 * protected by RCU. Note, however, that some things are unprotected,
43 * namely the key->sta dereferences within the hardware acceleration
Johannes Bergad0e2b52010-06-01 10:19:19 +020044 * functions. This means that sta_info_destroy() must remove the key
45 * which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040046 */
47
48static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040049
Johannes Bergad0e2b52010-06-01 10:19:19 +020050static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020051{
Johannes Berg46a5eba2010-09-15 13:28:15 +020052 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020053}
54
Johannes Bergdc822b52008-12-29 12:55:09 +010055static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040056{
Johannes Berg11a843b2007-08-28 17:01:55 -040057 if (key->sta)
Johannes Bergdc822b52008-12-29 12:55:09 +010058 return &key->sta->sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040059
Johannes Bergdc822b52008-12-29 12:55:09 +010060 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -040061}
62
Johannes Berg3ffc2a92010-08-27 14:26:52 +030063static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040064{
Johannes Bergdc822b52008-12-29 12:55:09 +010065 struct ieee80211_sub_if_data *sdata;
66 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040067 int ret;
68
Johannes Berg3b967662008-04-08 17:56:52 +020069 might_sleep();
70
Johannes Berge31b8212010-10-05 19:39:30 +020071 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +030072 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -040073
Johannes Bergad0e2b52010-06-01 10:19:19 +020074 assert_key_lock(key->local);
75
Johannes Bergdc822b52008-12-29 12:55:09 +010076 sta = get_sta_for_key(key);
77
Johannes Berge31b8212010-10-05 19:39:30 +020078 /*
79 * If this is a per-STA GTK, check if it
80 * is supported; if not, return.
81 */
82 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
83 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
84 goto out_unsupported;
85
Johannes Bergdc822b52008-12-29 12:55:09 +010086 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +010087 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
88 /*
89 * The driver doesn't know anything about VLAN interfaces.
90 * Hence, don't send GTKs for VLAN interfaces to the driver.
91 */
92 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
93 goto out_unsupported;
Johannes Bergdc822b52008-12-29 12:55:09 +010094 sdata = container_of(sdata->bss,
95 struct ieee80211_sub_if_data,
96 u.ap);
Helmut Schaa18890d42010-11-19 08:11:01 +010097 }
Johannes Berg11a843b2007-08-28 17:01:55 -040098
Johannes Berg12375ef2009-11-25 20:30:31 +010099 ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400100
Johannes Berge31b8212010-10-05 19:39:30 +0200101 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400102 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berge31b8212010-10-05 19:39:30 +0200103 return 0;
104 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400105
Johannes Berge31b8212010-10-05 19:39:30 +0200106 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700107 wiphy_err(key->local->hw.wiphy,
108 "failed to set key (%d, %pM) to hardware (%d)\n",
109 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300110
Johannes Berge31b8212010-10-05 19:39:30 +0200111 out_unsupported:
112 switch (key->conf.cipher) {
113 case WLAN_CIPHER_SUITE_WEP40:
114 case WLAN_CIPHER_SUITE_WEP104:
115 case WLAN_CIPHER_SUITE_TKIP:
116 case WLAN_CIPHER_SUITE_CCMP:
117 case WLAN_CIPHER_SUITE_AES_CMAC:
118 /* all of these we can do in software */
119 return 0;
120 default:
121 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300122 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400123}
124
125static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
126{
Johannes Bergdc822b52008-12-29 12:55:09 +0100127 struct ieee80211_sub_if_data *sdata;
128 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400129 int ret;
130
Johannes Berg3b967662008-04-08 17:56:52 +0200131 might_sleep();
132
Johannes Bergdb4d1162008-02-25 16:27:45 +0100133 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400134 return;
135
Johannes Bergad0e2b52010-06-01 10:19:19 +0200136 assert_key_lock(key->local);
137
138 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400139 return;
140
Johannes Bergdc822b52008-12-29 12:55:09 +0100141 sta = get_sta_for_key(key);
142 sdata = key->sdata;
143
144 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
145 sdata = container_of(sdata->bss,
146 struct ieee80211_sub_if_data,
147 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400148
Johannes Berg12375ef2009-11-25 20:30:31 +0100149 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg24487982009-04-23 18:52:52 +0200150 sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400151
152 if (ret)
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700153 wiphy_err(key->local->hw.wiphy,
154 "failed to remove key (%d, %pM) from hardware (%d)\n",
155 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400156
Johannes Berg3b967662008-04-08 17:56:52 +0200157 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200158}
159
Johannes Berge31b8212010-10-05 19:39:30 +0200160void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
161{
162 struct ieee80211_key *key;
163
164 key = container_of(key_conf, struct ieee80211_key, conf);
165
166 might_sleep();
167 assert_key_lock(key->local);
168
169 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
170
171 /*
172 * Flush TX path to avoid attempts to use this key
173 * after this function returns. Until then, drivers
174 * must be prepared to handle the key.
175 */
176 synchronize_rcu();
177}
178EXPORT_SYMBOL_GPL(ieee80211_key_removed);
179
Johannes Berg3b967662008-04-08 17:56:52 +0200180static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100181 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200182{
183 struct ieee80211_key *key = NULL;
184
Johannes Bergad0e2b52010-06-01 10:19:19 +0200185 assert_key_lock(sdata->local);
186
Johannes Berg3b967662008-04-08 17:56:52 +0200187 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
188 key = sdata->keys[idx];
189
Johannes Bergf7e01042010-12-09 19:49:02 +0100190 if (uni)
191 rcu_assign_pointer(sdata->default_unicast_key, key);
192 if (multi)
193 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200194
Johannes Bergf7e01042010-12-09 19:49:02 +0100195 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200196}
197
Johannes Bergf7e01042010-12-09 19:49:02 +0100198void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
199 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200200{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200201 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100202 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200203 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200204}
205
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200206static void
207__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
208{
209 struct ieee80211_key *key = NULL;
210
Johannes Bergad0e2b52010-06-01 10:19:19 +0200211 assert_key_lock(sdata->local);
212
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200213 if (idx >= NUM_DEFAULT_KEYS &&
214 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
215 key = sdata->keys[idx];
216
217 rcu_assign_pointer(sdata->default_mgmt_key, key);
218
Johannes Bergf7e01042010-12-09 19:49:02 +0100219 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200220}
221
222void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
223 int idx)
224{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200225 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200226 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200227 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200228}
229
Johannes Berg3b967662008-04-08 17:56:52 +0200230
231static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
232 struct sta_info *sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200233 bool pairwise,
Johannes Berg3b967662008-04-08 17:56:52 +0200234 struct ieee80211_key *old,
235 struct ieee80211_key *new)
236{
Johannes Bergf7e01042010-12-09 19:49:02 +0100237 int idx;
238 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200239
240 if (new)
241 list_add(&new->list, &sdata->key_list);
242
Johannes Berge31b8212010-10-05 19:39:30 +0200243 if (sta && pairwise) {
244 rcu_assign_pointer(sta->ptk, new);
245 } else if (sta) {
246 if (old)
247 idx = old->conf.keyidx;
248 else
249 idx = new->conf.keyidx;
250 rcu_assign_pointer(sta->gtk[idx], new);
Johannes Berg3b967662008-04-08 17:56:52 +0200251 } else {
252 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
253
254 if (old)
255 idx = old->conf.keyidx;
256 else
257 idx = new->conf.keyidx;
258
Johannes Bergf7e01042010-12-09 19:49:02 +0100259 defunikey = old && sdata->default_unicast_key == old;
260 defmultikey = old && sdata->default_multicast_key == old;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200261 defmgmtkey = old && sdata->default_mgmt_key == old;
Johannes Berg3b967662008-04-08 17:56:52 +0200262
Johannes Bergf7e01042010-12-09 19:49:02 +0100263 if (defunikey && !new)
264 __ieee80211_set_default_key(sdata, -1, true, false);
265 if (defmultikey && !new)
266 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200267 if (defmgmtkey && !new)
268 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200269
270 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100271 if (defunikey && new)
272 __ieee80211_set_default_key(sdata, new->conf.keyidx,
273 true, false);
274 if (defmultikey && new)
275 __ieee80211_set_default_key(sdata, new->conf.keyidx,
276 false, true);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200277 if (defmgmtkey && new)
278 __ieee80211_set_default_mgmt_key(sdata,
279 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200280 }
281
282 if (old) {
283 /*
284 * We'll use an empty list to indicate that the key
285 * has already been removed.
286 */
287 list_del_init(&old->list);
288 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400289}
290
Johannes Berg97359d12010-08-10 09:46:38 +0200291struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300292 const u8 *key_data,
293 size_t seq_len, const u8 *seq)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200294{
295 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100296 int i, j, err;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200297
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200298 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
Johannes Berg11a843b2007-08-28 17:01:55 -0400299
300 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200301 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100302 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400303
304 /*
305 * Default to software encryption; we'll later upload the
306 * key to the hardware if possible.
307 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400308 key->conf.flags = 0;
309 key->flags = 0;
310
Johannes Berg97359d12010-08-10 09:46:38 +0200311 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400312 key->conf.keyidx = idx;
313 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200314 switch (cipher) {
315 case WLAN_CIPHER_SUITE_WEP40:
316 case WLAN_CIPHER_SUITE_WEP104:
Felix Fietkau76708de2008-10-05 18:02:48 +0200317 key->conf.iv_len = WEP_IV_LEN;
318 key->conf.icv_len = WEP_ICV_LEN;
319 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200320 case WLAN_CIPHER_SUITE_TKIP:
Felix Fietkau76708de2008-10-05 18:02:48 +0200321 key->conf.iv_len = TKIP_IV_LEN;
322 key->conf.icv_len = TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300323 if (seq) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300324 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
325 key->u.tkip.rx[i].iv32 =
326 get_unaligned_le32(&seq[2]);
327 key->u.tkip.rx[i].iv16 =
328 get_unaligned_le16(seq);
329 }
330 }
Felix Fietkau76708de2008-10-05 18:02:48 +0200331 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200332 case WLAN_CIPHER_SUITE_CCMP:
Felix Fietkau76708de2008-10-05 18:02:48 +0200333 key->conf.iv_len = CCMP_HDR_LEN;
334 key->conf.icv_len = CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300335 if (seq) {
Jouni Malinen91902522010-06-11 10:27:33 -0700336 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300337 for (j = 0; j < CCMP_PN_LEN; j++)
338 key->u.ccmp.rx_pn[i][j] =
339 seq[CCMP_PN_LEN - j - 1];
340 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400341 /*
342 * Initialize AES key state here as an optimization so that
343 * it does not need to be initialized for every packet.
344 */
345 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100346 if (IS_ERR(key->u.ccmp.tfm)) {
347 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200348 kfree(key);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100349 key = ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400350 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200351 break;
352 case WLAN_CIPHER_SUITE_AES_CMAC:
353 key->conf.iv_len = 0;
354 key->conf.icv_len = sizeof(struct ieee80211_mmie);
355 if (seq)
356 for (j = 0; j < 6; j++)
357 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200358 /*
359 * Initialize AES key state here as an optimization so that
360 * it does not need to be initialized for every packet.
361 */
362 key->u.aes_cmac.tfm =
363 ieee80211_aes_cmac_key_setup(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100364 if (IS_ERR(key->u.aes_cmac.tfm)) {
365 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200366 kfree(key);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100367 key = ERR_PTR(err);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200368 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200369 break;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200370 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200371 memcpy(key->conf.key, key_data, key_len);
372 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200373
Johannes Bergdb4d1162008-02-25 16:27:45 +0100374 return key;
375}
Johannes Berg11a843b2007-08-28 17:01:55 -0400376
Johannes Bergad0e2b52010-06-01 10:19:19 +0200377static void __ieee80211_key_destroy(struct ieee80211_key *key)
378{
379 if (!key)
380 return;
381
Jouni Malinen32162a42010-07-26 15:52:03 -0700382 if (key->local)
383 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200384
Johannes Berg97359d12010-08-10 09:46:38 +0200385 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200386 ieee80211_aes_key_free(key->u.ccmp.tfm);
Johannes Berg97359d12010-08-10 09:46:38 +0200387 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200388 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Jouni Malinen32162a42010-07-26 15:52:03 -0700389 if (key->local)
390 ieee80211_debugfs_key_remove(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200391
392 kfree(key);
393}
394
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300395int ieee80211_key_link(struct ieee80211_key *key,
396 struct ieee80211_sub_if_data *sdata,
397 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100398{
399 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300400 int idx, ret;
Johannes Berge31b8212010-10-05 19:39:30 +0200401 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100402
Johannes Bergdb4d1162008-02-25 16:27:45 +0100403 BUG_ON(!sdata);
404 BUG_ON(!key);
405
406 idx = key->conf.keyidx;
407 key->local = sdata->local;
408 key->sdata = sdata;
409 key->sta = sta;
410
Johannes Berg11a843b2007-08-28 17:01:55 -0400411 if (sta) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400412 /*
413 * some hardware cannot handle TKIP with QoS, so
414 * we indicate whether QoS could be in use.
415 */
Johannes Berg07346f812008-05-03 01:02:02 +0200416 if (test_sta_flags(sta, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400417 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
418 } else {
Johannes Berg05c914f2008-09-11 00:01:58 +0200419 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400420 struct sta_info *ap;
421
Johannes Berg3b967662008-04-08 17:56:52 +0200422 /*
423 * We're getting a sta pointer in,
424 * so must be under RCU read lock.
425 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100426
Johannes Berg11a843b2007-08-28 17:01:55 -0400427 /* same here, the AP could be using QoS */
Johannes Bergabe60632009-11-25 17:46:18 +0100428 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
Johannes Berg11a843b2007-08-28 17:01:55 -0400429 if (ap) {
Johannes Berg07346f812008-05-03 01:02:02 +0200430 if (test_sta_flags(ap, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400431 key->conf.flags |=
432 IEEE80211_KEY_FLAG_WMM_STA;
Johannes Berg11a843b2007-08-28 17:01:55 -0400433 }
434 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400435 }
436
Johannes Bergad0e2b52010-06-01 10:19:19 +0200437 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200438
Johannes Berge31b8212010-10-05 19:39:30 +0200439 if (sta && pairwise)
440 old_key = sta->ptk;
441 else if (sta)
442 old_key = sta->gtk[idx];
Johannes Bergd4e46a32007-09-14 11:10:24 -0400443 else
Johannes Bergdb4d1162008-02-25 16:27:45 +0100444 old_key = sdata->keys[idx];
445
Johannes Berge31b8212010-10-05 19:39:30 +0200446 __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200447 __ieee80211_key_destroy(old_key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400448
Johannes Bergad0e2b52010-06-01 10:19:19 +0200449 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200450
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300451 ret = ieee80211_key_enable_hw_accel(key);
Johannes Berg523d2f62009-07-01 21:26:43 +0200452
Johannes Bergad0e2b52010-06-01 10:19:19 +0200453 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300454
455 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200456}
457
458static void __ieee80211_key_free(struct ieee80211_key *key)
459{
460 /*
461 * Replace key with nothingness if it was ever used.
462 */
463 if (key->sdata)
464 __ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200465 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
466 key, NULL);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200467 __ieee80211_key_destroy(key);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200468}
469
Jouni Malinen32162a42010-07-26 15:52:03 -0700470void ieee80211_key_free(struct ieee80211_local *local,
471 struct ieee80211_key *key)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200472{
Johannes Berg8f371712007-08-28 17:01:54 -0400473 if (!key)
474 return;
475
Johannes Bergad0e2b52010-06-01 10:19:19 +0200476 mutex_lock(&local->key_mtx);
Johannes Berg3a245762008-04-09 16:45:37 +0200477 __ieee80211_key_free(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200478 mutex_unlock(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200479}
480
481void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
482{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200483 struct ieee80211_key *key;
484
Johannes Berg3a245762008-04-09 16:45:37 +0200485 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200486
Johannes Berg9607e6b2009-12-23 13:15:31 +0100487 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200488 return;
489
Johannes Bergad0e2b52010-06-01 10:19:19 +0200490 mutex_lock(&sdata->local->key_mtx);
491
492 list_for_each_entry(key, &sdata->key_list, list)
493 ieee80211_key_enable_hw_accel(key);
494
495 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200496}
497
498void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
499{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200500 struct ieee80211_key *key;
501
Johannes Berg3a245762008-04-09 16:45:37 +0200502 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200503
Johannes Bergad0e2b52010-06-01 10:19:19 +0200504 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200505
Johannes Bergad0e2b52010-06-01 10:19:19 +0200506 list_for_each_entry(key, &sdata->key_list, list)
507 ieee80211_key_disable_hw_accel(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200508
Johannes Bergad0e2b52010-06-01 10:19:19 +0200509 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400510}
511
512void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
513{
514 struct ieee80211_key *key, *tmp;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100515
Johannes Bergad0e2b52010-06-01 10:19:19 +0200516 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200517
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200518 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400519
520 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg3a245762008-04-09 16:45:37 +0200521 __ieee80211_key_free(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400522
Johannes Bergf7e01042010-12-09 19:49:02 +0100523 ieee80211_debugfs_key_update_default(sdata);
524
Johannes Bergad0e2b52010-06-01 10:19:19 +0200525 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400526}