blob: 31afd712930df2a7507505a26cd24f4f4e1ee2bd [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 Berg1f5a7e472007-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 Berg1f5a7e472007-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 Berg1f5a7e472007-07-27 15:43:23 +020021#include "debugfs_key.h"
22#include "aes_ccm.h"
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +020023#include "aes_cmac.h"
Johannes Berg1f5a7e472007-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 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010033 * Hardware acceleration is done on a best-effort basis for algorithms
34 * that are implemented in software, for each key the hardware is asked
35 * to enable that key for offloading but if it cannot do that the key is
36 * simply kept for software encryption (unless it is for an algorithm
37 * that isn't implemented in software).
38 * There is currently no way of knowing whether a key is handled in SW
39 * or HW except by looking into debugfs.
Johannes Berg11a843b2007-08-28 17:01:55 -040040 *
Johannes Bergb5c34f62011-01-03 19:51:09 +010041 * All key management is internally protected by a mutex. Within all
42 * other parts of mac80211, key references are, just as STA structure
43 * references, protected by RCU. Note, however, that some things are
44 * unprotected, namely the key->sta dereferences within the hardware
45 * acceleration functions. This means that sta_info_destroy() must
46 * remove the key which waits for an RCU grace period.
Johannes Berg11a843b2007-08-28 17:01:55 -040047 */
48
49static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040050
Johannes Bergad0e2b52010-06-01 10:19:19 +020051static void assert_key_lock(struct ieee80211_local *local)
Johannes Berg3b967662008-04-08 17:56:52 +020052{
Johannes Berg46a5eba2010-09-15 13:28:15 +020053 lockdep_assert_held(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +020054}
55
Johannes Bergdc822b52008-12-29 12:55:09 +010056static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040057{
Johannes Berg11a843b2007-08-28 17:01:55 -040058 if (key->sta)
Johannes Bergdc822b52008-12-29 12:55:09 +010059 return &key->sta->sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040060
Johannes Bergdc822b52008-12-29 12:55:09 +010061 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -040062}
63
Johannes Berg3ffc2a92010-08-27 14:26:52 +030064static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -040065{
Johannes Bergdc822b52008-12-29 12:55:09 +010066 struct ieee80211_sub_if_data *sdata;
67 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -040068 int ret;
69
Johannes Berg3b967662008-04-08 17:56:52 +020070 might_sleep();
71
Johannes Berge31b8212010-10-05 19:39:30 +020072 if (!key->local->ops->set_key)
Johannes Berg3ffc2a92010-08-27 14:26:52 +030073 goto out_unsupported;
Johannes Berg11a843b2007-08-28 17:01:55 -040074
Johannes Bergad0e2b52010-06-01 10:19:19 +020075 assert_key_lock(key->local);
76
Johannes Bergdc822b52008-12-29 12:55:09 +010077 sta = get_sta_for_key(key);
78
Johannes Berge31b8212010-10-05 19:39:30 +020079 /*
80 * If this is a per-STA GTK, check if it
81 * is supported; if not, return.
82 */
83 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
84 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
85 goto out_unsupported;
86
Johannes Bergdc822b52008-12-29 12:55:09 +010087 sdata = key->sdata;
Helmut Schaa18890d42010-11-19 08:11:01 +010088 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
89 /*
90 * The driver doesn't know anything about VLAN interfaces.
91 * Hence, don't send GTKs for VLAN interfaces to the driver.
92 */
93 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
94 goto out_unsupported;
Johannes Bergdc822b52008-12-29 12:55:09 +010095 sdata = container_of(sdata->bss,
96 struct ieee80211_sub_if_data,
97 u.ap);
Helmut Schaa18890d42010-11-19 08:11:01 +010098 }
Johannes Berg11a843b2007-08-28 17:01:55 -040099
Johannes Berg12375ef2009-11-25 20:30:31 +0100100 ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400101
Johannes Berge31b8212010-10-05 19:39:30 +0200102 if (!ret) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400103 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530104
Mohammed Shafi Shajakhaneaef6a92011-05-05 10:59:24 +0530105 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
106 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530107 key->local->crypto_tx_tailroom_needed_cnt--;
108
Johannes Berge31b8212010-10-05 19:39:30 +0200109 return 0;
110 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400111
Johannes Berge31b8212010-10-05 19:39:30 +0200112 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700113 wiphy_err(key->local->hw.wiphy,
114 "failed to set key (%d, %pM) to hardware (%d)\n",
115 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300116
Johannes Berge31b8212010-10-05 19:39:30 +0200117 out_unsupported:
118 switch (key->conf.cipher) {
119 case WLAN_CIPHER_SUITE_WEP40:
120 case WLAN_CIPHER_SUITE_WEP104:
121 case WLAN_CIPHER_SUITE_TKIP:
122 case WLAN_CIPHER_SUITE_CCMP:
123 case WLAN_CIPHER_SUITE_AES_CMAC:
124 /* all of these we can do in software */
125 return 0;
126 default:
127 return -EINVAL;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300128 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400129}
130
131static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
132{
Johannes Bergdc822b52008-12-29 12:55:09 +0100133 struct ieee80211_sub_if_data *sdata;
134 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400135 int ret;
136
Johannes Berg3b967662008-04-08 17:56:52 +0200137 might_sleep();
138
Johannes Bergdb4d1162008-02-25 16:27:45 +0100139 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400140 return;
141
Johannes Bergad0e2b52010-06-01 10:19:19 +0200142 assert_key_lock(key->local);
143
144 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg11a843b2007-08-28 17:01:55 -0400145 return;
146
Johannes Bergdc822b52008-12-29 12:55:09 +0100147 sta = get_sta_for_key(key);
148 sdata = key->sdata;
149
150 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
151 sdata = container_of(sdata->bss,
152 struct ieee80211_sub_if_data,
153 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400154
Johannes Berg12375ef2009-11-25 20:30:31 +0100155 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg24487982009-04-23 18:52:52 +0200156 sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400157
158 if (ret)
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700159 wiphy_err(key->local->hw.wiphy,
160 "failed to remove key (%d, %pM) from hardware (%d)\n",
161 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400162
Johannes Berg3b967662008-04-08 17:56:52 +0200163 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530164
Mohammed Shafi Shajakhaneaef6a92011-05-05 10:59:24 +0530165 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
166 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530167 key->local->crypto_tx_tailroom_needed_cnt++;
Johannes Berg3b967662008-04-08 17:56:52 +0200168}
169
Johannes Berge31b8212010-10-05 19:39:30 +0200170void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
171{
172 struct ieee80211_key *key;
173
174 key = container_of(key_conf, struct ieee80211_key, conf);
175
176 might_sleep();
177 assert_key_lock(key->local);
178
179 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
180
181 /*
182 * Flush TX path to avoid attempts to use this key
183 * after this function returns. Until then, drivers
184 * must be prepared to handle the key.
185 */
186 synchronize_rcu();
187}
188EXPORT_SYMBOL_GPL(ieee80211_key_removed);
189
Johannes Berg3b967662008-04-08 17:56:52 +0200190static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
Johannes Bergf7e01042010-12-09 19:49:02 +0100191 int idx, bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200192{
193 struct ieee80211_key *key = NULL;
194
Johannes Bergad0e2b52010-06-01 10:19:19 +0200195 assert_key_lock(sdata->local);
196
Johannes Berg3b967662008-04-08 17:56:52 +0200197 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200198 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Berg3b967662008-04-08 17:56:52 +0200199
Johannes Bergf7e01042010-12-09 19:49:02 +0100200 if (uni)
201 rcu_assign_pointer(sdata->default_unicast_key, key);
202 if (multi)
203 rcu_assign_pointer(sdata->default_multicast_key, key);
Johannes Berg3b967662008-04-08 17:56:52 +0200204
Johannes Bergf7e01042010-12-09 19:49:02 +0100205 ieee80211_debugfs_key_update_default(sdata);
Johannes Berg3b967662008-04-08 17:56:52 +0200206}
207
Johannes Bergf7e01042010-12-09 19:49:02 +0100208void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
209 bool uni, bool multi)
Johannes Berg3b967662008-04-08 17:56:52 +0200210{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200211 mutex_lock(&sdata->local->key_mtx);
Johannes Bergf7e01042010-12-09 19:49:02 +0100212 __ieee80211_set_default_key(sdata, idx, uni, multi);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200213 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200214}
215
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200216static void
217__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
218{
219 struct ieee80211_key *key = NULL;
220
Johannes Bergad0e2b52010-06-01 10:19:19 +0200221 assert_key_lock(sdata->local);
222
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200223 if (idx >= NUM_DEFAULT_KEYS &&
224 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg40b275b2011-05-13 14:15:49 +0200225 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200226
227 rcu_assign_pointer(sdata->default_mgmt_key, key);
228
Johannes Bergf7e01042010-12-09 19:49:02 +0100229 ieee80211_debugfs_key_update_default(sdata);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200230}
231
232void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
233 int idx)
234{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200235 mutex_lock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200236 __ieee80211_set_default_mgmt_key(sdata, idx);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200237 mutex_unlock(&sdata->local->key_mtx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200238}
239
Johannes Berg3b967662008-04-08 17:56:52 +0200240
241static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
242 struct sta_info *sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200243 bool pairwise,
Johannes Berg3b967662008-04-08 17:56:52 +0200244 struct ieee80211_key *old,
245 struct ieee80211_key *new)
246{
Johannes Bergf7e01042010-12-09 19:49:02 +0100247 int idx;
248 bool defunikey, defmultikey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200249
250 if (new)
251 list_add(&new->list, &sdata->key_list);
252
Johannes Berge31b8212010-10-05 19:39:30 +0200253 if (sta && pairwise) {
254 rcu_assign_pointer(sta->ptk, new);
255 } else if (sta) {
256 if (old)
257 idx = old->conf.keyidx;
258 else
259 idx = new->conf.keyidx;
260 rcu_assign_pointer(sta->gtk[idx], new);
Johannes Berg3b967662008-04-08 17:56:52 +0200261 } else {
262 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
263
264 if (old)
265 idx = old->conf.keyidx;
266 else
267 idx = new->conf.keyidx;
268
Johannes Berg40b275b2011-05-13 14:15:49 +0200269 defunikey = old &&
270 old == key_mtx_dereference(sdata->local,
271 sdata->default_unicast_key);
272 defmultikey = old &&
273 old == key_mtx_dereference(sdata->local,
274 sdata->default_multicast_key);
275 defmgmtkey = old &&
276 old == key_mtx_dereference(sdata->local,
277 sdata->default_mgmt_key);
Johannes Berg3b967662008-04-08 17:56:52 +0200278
Johannes Bergf7e01042010-12-09 19:49:02 +0100279 if (defunikey && !new)
280 __ieee80211_set_default_key(sdata, -1, true, false);
281 if (defmultikey && !new)
282 __ieee80211_set_default_key(sdata, -1, false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200283 if (defmgmtkey && !new)
284 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200285
286 rcu_assign_pointer(sdata->keys[idx], new);
Johannes Bergf7e01042010-12-09 19:49:02 +0100287 if (defunikey && new)
288 __ieee80211_set_default_key(sdata, new->conf.keyidx,
289 true, false);
290 if (defmultikey && new)
291 __ieee80211_set_default_key(sdata, new->conf.keyidx,
292 false, true);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200293 if (defmgmtkey && new)
294 __ieee80211_set_default_mgmt_key(sdata,
295 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200296 }
297
Johannes Bergb5c34f62011-01-03 19:51:09 +0100298 if (old)
299 list_del(&old->list);
Johannes Berg11a843b2007-08-28 17:01:55 -0400300}
301
Johannes Berg97359d12010-08-10 09:46:38 +0200302struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300303 const u8 *key_data,
304 size_t seq_len, const u8 *seq)
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200305{
306 struct ieee80211_key *key;
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100307 int i, j, err;
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200308
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200309 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
Johannes Berg11a843b2007-08-28 17:01:55 -0400310
311 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200312 if (!key)
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100313 return ERR_PTR(-ENOMEM);
Johannes Berg11a843b2007-08-28 17:01:55 -0400314
315 /*
316 * Default to software encryption; we'll later upload the
317 * key to the hardware if possible.
318 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400319 key->conf.flags = 0;
320 key->flags = 0;
321
Johannes Berg97359d12010-08-10 09:46:38 +0200322 key->conf.cipher = cipher;
Johannes Berg11a843b2007-08-28 17:01:55 -0400323 key->conf.keyidx = idx;
324 key->conf.keylen = key_len;
Johannes Berg97359d12010-08-10 09:46:38 +0200325 switch (cipher) {
326 case WLAN_CIPHER_SUITE_WEP40:
327 case WLAN_CIPHER_SUITE_WEP104:
Felix Fietkau76708de2008-10-05 18:02:48 +0200328 key->conf.iv_len = WEP_IV_LEN;
329 key->conf.icv_len = WEP_ICV_LEN;
330 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200331 case WLAN_CIPHER_SUITE_TKIP:
Felix Fietkau76708de2008-10-05 18:02:48 +0200332 key->conf.iv_len = TKIP_IV_LEN;
333 key->conf.icv_len = TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300334 if (seq) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300335 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
336 key->u.tkip.rx[i].iv32 =
337 get_unaligned_le32(&seq[2]);
338 key->u.tkip.rx[i].iv16 =
339 get_unaligned_le16(seq);
340 }
341 }
Felix Fietkau76708de2008-10-05 18:02:48 +0200342 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200343 case WLAN_CIPHER_SUITE_CCMP:
Felix Fietkau76708de2008-10-05 18:02:48 +0200344 key->conf.iv_len = CCMP_HDR_LEN;
345 key->conf.icv_len = CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300346 if (seq) {
Jouni Malinen91902522010-06-11 10:27:33 -0700347 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300348 for (j = 0; j < CCMP_PN_LEN; j++)
349 key->u.ccmp.rx_pn[i][j] =
350 seq[CCMP_PN_LEN - j - 1];
351 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400352 /*
353 * Initialize AES key state here as an optimization so that
354 * it does not need to be initialized for every packet.
355 */
356 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100357 if (IS_ERR(key->u.ccmp.tfm)) {
358 err = PTR_ERR(key->u.ccmp.tfm);
Johannes Berg3b967662008-04-08 17:56:52 +0200359 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200360 return ERR_PTR(err);
Johannes Berg11a843b2007-08-28 17:01:55 -0400361 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200362 break;
363 case WLAN_CIPHER_SUITE_AES_CMAC:
364 key->conf.iv_len = 0;
365 key->conf.icv_len = sizeof(struct ieee80211_mmie);
366 if (seq)
367 for (j = 0; j < 6; j++)
368 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200369 /*
370 * Initialize AES key state here as an optimization so that
371 * it does not need to be initialized for every packet.
372 */
373 key->u.aes_cmac.tfm =
374 ieee80211_aes_cmac_key_setup(key_data);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +0100375 if (IS_ERR(key->u.aes_cmac.tfm)) {
376 err = PTR_ERR(key->u.aes_cmac.tfm);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200377 kfree(key);
Petr Å tetiar1f951a72011-03-27 13:31:26 +0200378 return ERR_PTR(err);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200379 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200380 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200381 }
Johannes Berg60ae0f22010-08-10 09:46:39 +0200382 memcpy(key->conf.key, key_data, key_len);
383 INIT_LIST_HEAD(&key->list);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200384
Johannes Bergdb4d1162008-02-25 16:27:45 +0100385 return key;
386}
Johannes Berg11a843b2007-08-28 17:01:55 -0400387
Johannes Bergad0e2b52010-06-01 10:19:19 +0200388static void __ieee80211_key_destroy(struct ieee80211_key *key)
389{
390 if (!key)
391 return;
392
Johannes Bergd2460f42011-01-03 19:42:24 +0100393 /*
394 * Synchronize so the TX path can no longer be using
395 * this key before we free/remove it.
396 */
397 synchronize_rcu();
398
Jouni Malinen32162a42010-07-26 15:52:03 -0700399 if (key->local)
400 ieee80211_key_disable_hw_accel(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200401
Johannes Berg97359d12010-08-10 09:46:38 +0200402 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200403 ieee80211_aes_key_free(key->u.ccmp.tfm);
Johannes Berg97359d12010-08-10 09:46:38 +0200404 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
Johannes Bergad0e2b52010-06-01 10:19:19 +0200405 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530406 if (key->local) {
Jouni Malinen32162a42010-07-26 15:52:03 -0700407 ieee80211_debugfs_key_remove(key);
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530408 key->local->crypto_tx_tailroom_needed_cnt--;
409 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200410
411 kfree(key);
412}
413
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300414int ieee80211_key_link(struct ieee80211_key *key,
415 struct ieee80211_sub_if_data *sdata,
416 struct sta_info *sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100417{
418 struct ieee80211_key *old_key;
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300419 int idx, ret;
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100420 bool pairwise;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100421
Johannes Bergdb4d1162008-02-25 16:27:45 +0100422 BUG_ON(!sdata);
423 BUG_ON(!key);
424
Mariusz Kozlowski67aa0302011-03-26 18:58:51 +0100425 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100426 idx = key->conf.keyidx;
427 key->local = sdata->local;
428 key->sdata = sdata;
429 key->sta = sta;
430
Johannes Berg11a843b2007-08-28 17:01:55 -0400431 if (sta) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400432 /*
433 * some hardware cannot handle TKIP with QoS, so
434 * we indicate whether QoS could be in use.
435 */
Johannes Berg07346f812008-05-03 01:02:02 +0200436 if (test_sta_flags(sta, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400437 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
438 } else {
Johannes Berg05c914f2008-09-11 00:01:58 +0200439 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400440 struct sta_info *ap;
441
Johannes Berg3b967662008-04-08 17:56:52 +0200442 /*
Johannes Bergb5c34f62011-01-03 19:51:09 +0100443 * We're getting a sta pointer in, so must be under
444 * appropriate locking for sta_info_get().
Johannes Berg3b967662008-04-08 17:56:52 +0200445 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100446
Johannes Berg11a843b2007-08-28 17:01:55 -0400447 /* same here, the AP could be using QoS */
Johannes Bergabe60632009-11-25 17:46:18 +0100448 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
Johannes Berg11a843b2007-08-28 17:01:55 -0400449 if (ap) {
Johannes Berg07346f812008-05-03 01:02:02 +0200450 if (test_sta_flags(ap, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400451 key->conf.flags |=
452 IEEE80211_KEY_FLAG_WMM_STA;
Johannes Berg11a843b2007-08-28 17:01:55 -0400453 }
454 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400455 }
456
Johannes Bergad0e2b52010-06-01 10:19:19 +0200457 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200458
Johannes Berge31b8212010-10-05 19:39:30 +0200459 if (sta && pairwise)
Johannes Berg40b275b2011-05-13 14:15:49 +0200460 old_key = key_mtx_dereference(sdata->local, sta->ptk);
Johannes Berge31b8212010-10-05 19:39:30 +0200461 else if (sta)
Johannes Berg40b275b2011-05-13 14:15:49 +0200462 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400463 else
Johannes Berg40b275b2011-05-13 14:15:49 +0200464 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100465
Johannes Berge31b8212010-10-05 19:39:30 +0200466 __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200467 __ieee80211_key_destroy(old_key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400468
Johannes Bergad0e2b52010-06-01 10:19:19 +0200469 ieee80211_debugfs_key_add(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200470
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530471 key->local->crypto_tx_tailroom_needed_cnt++;
472
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300473 ret = ieee80211_key_enable_hw_accel(key);
Johannes Berg523d2f62009-07-01 21:26:43 +0200474
Johannes Bergad0e2b52010-06-01 10:19:19 +0200475 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +0300476
477 return ret;
Johannes Berg3a245762008-04-09 16:45:37 +0200478}
479
Johannes Berg5c0c3642011-05-12 14:31:49 +0200480void __ieee80211_key_free(struct ieee80211_key *key)
Johannes Berg3a245762008-04-09 16:45:37 +0200481{
Johannes Berg5c0c3642011-05-12 14:31:49 +0200482 if (!key)
483 return;
484
Johannes Berg3a245762008-04-09 16:45:37 +0200485 /*
486 * Replace key with nothingness if it was ever used.
487 */
488 if (key->sdata)
489 __ieee80211_key_replace(key->sdata, key->sta,
Johannes Berge31b8212010-10-05 19:39:30 +0200490 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
491 key, NULL);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200492 __ieee80211_key_destroy(key);
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200493}
494
Jouni Malinen32162a42010-07-26 15:52:03 -0700495void ieee80211_key_free(struct ieee80211_local *local,
496 struct ieee80211_key *key)
Johannes Berg1f5a7e472007-07-27 15:43:23 +0200497{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200498 mutex_lock(&local->key_mtx);
Johannes Berg3a245762008-04-09 16:45:37 +0200499 __ieee80211_key_free(key);
Johannes Bergad0e2b52010-06-01 10:19:19 +0200500 mutex_unlock(&local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200501}
502
503void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
504{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200505 struct ieee80211_key *key;
506
Johannes Berg3a245762008-04-09 16:45:37 +0200507 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200508
Johannes Berg9607e6b2009-12-23 13:15:31 +0100509 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200510 return;
511
Johannes Bergad0e2b52010-06-01 10:19:19 +0200512 mutex_lock(&sdata->local->key_mtx);
513
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530514 sdata->local->crypto_tx_tailroom_needed_cnt = 0;
515
516 list_for_each_entry(key, &sdata->key_list, list) {
517 sdata->local->crypto_tx_tailroom_needed_cnt++;
Johannes Bergad0e2b52010-06-01 10:19:19 +0200518 ieee80211_key_enable_hw_accel(key);
Yogesh Ashok Powaraac6af52011-04-27 18:40:29 +0530519 }
Johannes Bergad0e2b52010-06-01 10:19:19 +0200520
521 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200522}
523
524void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
525{
Johannes Bergad0e2b52010-06-01 10:19:19 +0200526 struct ieee80211_key *key;
527
Johannes Berg3a245762008-04-09 16:45:37 +0200528 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200529
Johannes Bergad0e2b52010-06-01 10:19:19 +0200530 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200531
Johannes Bergad0e2b52010-06-01 10:19:19 +0200532 list_for_each_entry(key, &sdata->key_list, list)
533 ieee80211_key_disable_hw_accel(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200534
Johannes Bergad0e2b52010-06-01 10:19:19 +0200535 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400536}
537
538void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
539{
540 struct ieee80211_key *key, *tmp;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100541
Johannes Bergad0e2b52010-06-01 10:19:19 +0200542 mutex_lock(&sdata->local->key_mtx);
Johannes Berg3b967662008-04-08 17:56:52 +0200543
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200544 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400545
546 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg3a245762008-04-09 16:45:37 +0200547 __ieee80211_key_free(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400548
Johannes Bergf7e01042010-12-09 19:49:02 +0100549 ieee80211_debugfs_key_update_default(sdata);
550
Johannes Bergad0e2b52010-06-01 10:19:19 +0200551 mutex_unlock(&sdata->local->key_mtx);
Johannes Berg11a843b2007-08-28 17:01:55 -0400552}