blob: 8160d9c5372ea09e8fbc6c01b54c3e00703d3188 [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>
Johannes Berg1f5a7e42007-07-27 15:43:23 +020017#include <net/mac80211.h>
18#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020019#include "driver-ops.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020020#include "debugfs_key.h"
21#include "aes_ccm.h"
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +020022#include "aes_cmac.h"
Johannes Berg1f5a7e42007-07-27 15:43:23 +020023
Johannes Berg11a843b2007-08-28 17:01:55 -040024
Johannes Bergdbbea672008-02-26 14:34:06 +010025/**
26 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040027 *
28 * Key handling in mac80211 is done based on per-interface (sub_if_data)
29 * keys and per-station keys. Since each station belongs to an interface,
30 * each station key also belongs to that interface.
31 *
32 * Hardware acceleration is done on a best-effort basis, for each key
33 * that is eligible the hardware is asked to enable that key but if
34 * it cannot do that they key is simply kept for software encryption.
35 * There is currently no way of knowing this except by looking into
36 * debugfs.
37 *
Johannes Berg3b967662008-04-08 17:56:52 +020038 * All key operations are protected internally so you can call them at
39 * any time.
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
44 * functions. This means that sta_info_destroy() must flush the key todo
45 * list.
46 *
47 * All the direct key list manipulation functions must not sleep because
48 * they can operate on STA info structs that are protected by RCU.
Johannes Berg11a843b2007-08-28 17:01:55 -040049 */
50
51static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Johannes Berg11a843b2007-08-28 17:01:55 -040052
Johannes Berg3b967662008-04-08 17:56:52 +020053/* key mutex: used to synchronise todo runners */
54static DEFINE_MUTEX(key_mutex);
55static DEFINE_SPINLOCK(todo_lock);
56static LIST_HEAD(todo_list);
57
58static void key_todo(struct work_struct *work)
59{
60 ieee80211_key_todo();
61}
62
63static DECLARE_WORK(todo_work, key_todo);
64
65/**
66 * add_todo - add todo item for a key
67 *
68 * @key: key to add to do item for
69 * @flag: todo flag(s)
Johannes Berg523d2f62009-07-01 21:26:43 +020070 *
71 * Must be called with IRQs or softirqs disabled.
Johannes Berg3b967662008-04-08 17:56:52 +020072 */
73static void add_todo(struct ieee80211_key *key, u32 flag)
74{
75 if (!key)
76 return;
77
78 spin_lock(&todo_lock);
79 key->flags |= flag;
Johannes Berg245cbe72008-04-13 10:43:50 +020080 /*
81 * Remove again if already on the list so that we move it to the end.
82 */
83 if (!list_empty(&key->todo))
84 list_del(&key->todo);
85 list_add_tail(&key->todo, &todo_list);
Johannes Berg3b967662008-04-08 17:56:52 +020086 schedule_work(&todo_work);
87 spin_unlock(&todo_lock);
88}
89
90/**
91 * ieee80211_key_lock - lock the mac80211 key operation lock
92 *
93 * This locks the (global) mac80211 key operation lock, all
94 * key operations must be done under this lock.
95 */
96static void ieee80211_key_lock(void)
97{
98 mutex_lock(&key_mutex);
99}
100
101/**
102 * ieee80211_key_unlock - unlock the mac80211 key operation lock
103 */
104static void ieee80211_key_unlock(void)
105{
106 mutex_unlock(&key_mutex);
107}
108
109static void assert_key_lock(void)
110{
111 WARN_ON(!mutex_is_locked(&key_mutex));
112}
113
Johannes Bergdc822b52008-12-29 12:55:09 +0100114static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400115{
Johannes Berg11a843b2007-08-28 17:01:55 -0400116 if (key->sta)
Johannes Bergdc822b52008-12-29 12:55:09 +0100117 return &key->sta->sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400118
Johannes Bergdc822b52008-12-29 12:55:09 +0100119 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -0400120}
121
122static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
123{
Johannes Bergdc822b52008-12-29 12:55:09 +0100124 struct ieee80211_sub_if_data *sdata;
125 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400126 int ret;
127
Johannes Berg3b967662008-04-08 17:56:52 +0200128 assert_key_lock();
129 might_sleep();
130
Johannes Berg11a843b2007-08-28 17:01:55 -0400131 if (!key->local->ops->set_key)
132 return;
133
Johannes Bergdc822b52008-12-29 12:55:09 +0100134 sta = get_sta_for_key(key);
135
136 sdata = key->sdata;
137 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
138 sdata = container_of(sdata->bss,
139 struct ieee80211_sub_if_data,
140 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400141
Johannes Berg12375ef2009-11-25 20:30:31 +0100142 ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400143
Johannes Berg3b967662008-04-08 17:56:52 +0200144 if (!ret) {
Johannes Berg523d2f62009-07-01 21:26:43 +0200145 spin_lock_bh(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400146 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg523d2f62009-07-01 21:26:43 +0200147 spin_unlock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200148 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400149
150 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
151 printk(KERN_ERR "mac80211-%s: failed to set key "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700152 "(%d, %pM) to hardware (%d)\n",
Johannes Berg11a843b2007-08-28 17:01:55 -0400153 wiphy_name(key->local->hw.wiphy),
Johannes Bergdc822b52008-12-29 12:55:09 +0100154 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400155}
156
157static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
158{
Johannes Bergdc822b52008-12-29 12:55:09 +0100159 struct ieee80211_sub_if_data *sdata;
160 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400161 int ret;
162
Johannes Berg3b967662008-04-08 17:56:52 +0200163 assert_key_lock();
164 might_sleep();
165
Johannes Bergdb4d1162008-02-25 16:27:45 +0100166 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400167 return;
168
Johannes Berg523d2f62009-07-01 21:26:43 +0200169 spin_lock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200170 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
Johannes Berg523d2f62009-07-01 21:26:43 +0200171 spin_unlock_bh(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400172 return;
Johannes Berg3b967662008-04-08 17:56:52 +0200173 }
Johannes Berg523d2f62009-07-01 21:26:43 +0200174 spin_unlock_bh(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400175
Johannes Bergdc822b52008-12-29 12:55:09 +0100176 sta = get_sta_for_key(key);
177 sdata = key->sdata;
178
179 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
180 sdata = container_of(sdata->bss,
181 struct ieee80211_sub_if_data,
182 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400183
Johannes Berg12375ef2009-11-25 20:30:31 +0100184 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
Johannes Berg24487982009-04-23 18:52:52 +0200185 sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400186
187 if (ret)
188 printk(KERN_ERR "mac80211-%s: failed to remove key "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700189 "(%d, %pM) from hardware (%d)\n",
Johannes Berg11a843b2007-08-28 17:01:55 -0400190 wiphy_name(key->local->hw.wiphy),
Johannes Bergdc822b52008-12-29 12:55:09 +0100191 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400192
Johannes Berg523d2f62009-07-01 21:26:43 +0200193 spin_lock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200194 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg523d2f62009-07-01 21:26:43 +0200195 spin_unlock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200196}
197
198static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
199 int idx)
200{
201 struct ieee80211_key *key = NULL;
202
203 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
204 key = sdata->keys[idx];
205
206 rcu_assign_pointer(sdata->default_key, key);
207
208 if (key)
209 add_todo(key, KEY_FLAG_TODO_DEFKEY);
210}
211
212void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
213{
214 unsigned long flags;
215
Johannes Bergb16bd152008-04-11 21:40:35 +0200216 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200217 __ieee80211_set_default_key(sdata, idx);
Johannes Bergb16bd152008-04-11 21:40:35 +0200218 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200219}
220
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200221static void
222__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
223{
224 struct ieee80211_key *key = NULL;
225
226 if (idx >= NUM_DEFAULT_KEYS &&
227 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
228 key = sdata->keys[idx];
229
230 rcu_assign_pointer(sdata->default_mgmt_key, key);
231
232 if (key)
233 add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY);
234}
235
236void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
237 int idx)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&sdata->local->key_lock, flags);
242 __ieee80211_set_default_mgmt_key(sdata, idx);
243 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
244}
245
Johannes Berg3b967662008-04-08 17:56:52 +0200246
247static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
248 struct sta_info *sta,
249 struct ieee80211_key *old,
250 struct ieee80211_key *new)
251{
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200252 int idx, defkey, defmgmtkey;
Johannes Berg3b967662008-04-08 17:56:52 +0200253
254 if (new)
255 list_add(&new->list, &sdata->key_list);
256
257 if (sta) {
258 rcu_assign_pointer(sta->key, new);
259 } else {
260 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
261
262 if (old)
263 idx = old->conf.keyidx;
264 else
265 idx = new->conf.keyidx;
266
267 defkey = old && sdata->default_key == old;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200268 defmgmtkey = old && sdata->default_mgmt_key == old;
Johannes Berg3b967662008-04-08 17:56:52 +0200269
270 if (defkey && !new)
271 __ieee80211_set_default_key(sdata, -1);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200272 if (defmgmtkey && !new)
273 __ieee80211_set_default_mgmt_key(sdata, -1);
Johannes Berg3b967662008-04-08 17:56:52 +0200274
275 rcu_assign_pointer(sdata->keys[idx], new);
276 if (defkey && new)
277 __ieee80211_set_default_key(sdata, new->conf.keyidx);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200278 if (defmgmtkey && new)
279 __ieee80211_set_default_mgmt_key(sdata,
280 new->conf.keyidx);
Johannes Berg3b967662008-04-08 17:56:52 +0200281 }
282
283 if (old) {
284 /*
285 * We'll use an empty list to indicate that the key
286 * has already been removed.
287 */
288 list_del_init(&old->list);
289 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400290}
291
Johannes Bergdb4d1162008-02-25 16:27:45 +0100292struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
Johannes Berg11a843b2007-08-28 17:01:55 -0400293 int idx,
294 size_t key_len,
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300295 const u8 *key_data,
296 size_t seq_len, const u8 *seq)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200297{
298 struct ieee80211_key *key;
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300299 int i, j;
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200300
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200301 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
Johannes Berg11a843b2007-08-28 17:01:55 -0400302
303 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200304 if (!key)
305 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -0400306
307 /*
308 * Default to software encryption; we'll later upload the
309 * key to the hardware if possible.
310 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400311 key->conf.flags = 0;
312 key->flags = 0;
313
314 key->conf.alg = alg;
315 key->conf.keyidx = idx;
316 key->conf.keylen = key_len;
Felix Fietkau76708de2008-10-05 18:02:48 +0200317 switch (alg) {
318 case ALG_WEP:
319 key->conf.iv_len = WEP_IV_LEN;
320 key->conf.icv_len = WEP_ICV_LEN;
321 break;
322 case ALG_TKIP:
323 key->conf.iv_len = TKIP_IV_LEN;
324 key->conf.icv_len = TKIP_ICV_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300325 if (seq) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300326 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
327 key->u.tkip.rx[i].iv32 =
328 get_unaligned_le32(&seq[2]);
329 key->u.tkip.rx[i].iv16 =
330 get_unaligned_le16(seq);
331 }
332 }
Felix Fietkau76708de2008-10-05 18:02:48 +0200333 break;
334 case ALG_CCMP:
335 key->conf.iv_len = CCMP_HDR_LEN;
336 key->conf.icv_len = CCMP_MIC_LEN;
Jouni Malinen9f26a952009-05-15 12:38:32 +0300337 if (seq) {
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300338 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
339 for (j = 0; j < CCMP_PN_LEN; j++)
340 key->u.ccmp.rx_pn[i][j] =
341 seq[CCMP_PN_LEN - j - 1];
342 }
Felix Fietkau76708de2008-10-05 18:02:48 +0200343 break;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200344 case ALG_AES_CMAC:
345 key->conf.iv_len = 0;
346 key->conf.icv_len = sizeof(struct ieee80211_mmie);
Jouni Malinen9f26a952009-05-15 12:38:32 +0300347 if (seq)
Jouni Malinenfaa8fdc2009-05-11 21:57:58 +0300348 for (j = 0; j < 6; j++)
349 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200350 break;
Felix Fietkau76708de2008-10-05 18:02:48 +0200351 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400352 memcpy(key->conf.key, key_data, key_len);
Johannes Berge48618292008-02-27 13:39:00 +0100353 INIT_LIST_HEAD(&key->list);
Johannes Berg3b967662008-04-08 17:56:52 +0200354 INIT_LIST_HEAD(&key->todo);
Johannes Berg11a843b2007-08-28 17:01:55 -0400355
Johannes Berg11a843b2007-08-28 17:01:55 -0400356 if (alg == ALG_CCMP) {
357 /*
358 * Initialize AES key state here as an optimization so that
359 * it does not need to be initialized for every packet.
360 */
361 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
362 if (!key->u.ccmp.tfm) {
Johannes Berg3b967662008-04-08 17:56:52 +0200363 kfree(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400364 return NULL;
365 }
366 }
367
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200368 if (alg == ALG_AES_CMAC) {
369 /*
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);
375 if (!key->u.aes_cmac.tfm) {
376 kfree(key);
377 return NULL;
378 }
379 }
380
Johannes Bergdb4d1162008-02-25 16:27:45 +0100381 return key;
382}
Johannes Berg11a843b2007-08-28 17:01:55 -0400383
Johannes Bergdb4d1162008-02-25 16:27:45 +0100384void ieee80211_key_link(struct ieee80211_key *key,
385 struct ieee80211_sub_if_data *sdata,
386 struct sta_info *sta)
387{
388 struct ieee80211_key *old_key;
Johannes Berg3b967662008-04-08 17:56:52 +0200389 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100390 int idx;
391
Johannes Bergdb4d1162008-02-25 16:27:45 +0100392 BUG_ON(!sdata);
393 BUG_ON(!key);
394
395 idx = key->conf.keyidx;
396 key->local = sdata->local;
397 key->sdata = sdata;
398 key->sta = sta;
399
Johannes Berg11a843b2007-08-28 17:01:55 -0400400 if (sta) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400401 /*
402 * some hardware cannot handle TKIP with QoS, so
403 * we indicate whether QoS could be in use.
404 */
Johannes Berg07346f812008-05-03 01:02:02 +0200405 if (test_sta_flags(sta, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400406 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
Ivo van Doornc6adbd22008-04-17 21:11:18 +0200407
408 /*
409 * This key is for a specific sta interface,
410 * inform the driver that it should try to store
411 * this key as pairwise key.
412 */
413 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Berg11a843b2007-08-28 17:01:55 -0400414 } else {
Johannes Berg05c914f2008-09-11 00:01:58 +0200415 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400416 struct sta_info *ap;
417
Johannes Berg3b967662008-04-08 17:56:52 +0200418 /*
419 * We're getting a sta pointer in,
420 * so must be under RCU read lock.
421 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100422
Johannes Berg11a843b2007-08-28 17:01:55 -0400423 /* same here, the AP could be using QoS */
Johannes Bergabe60632009-11-25 17:46:18 +0100424 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
Johannes Berg11a843b2007-08-28 17:01:55 -0400425 if (ap) {
Johannes Berg07346f812008-05-03 01:02:02 +0200426 if (test_sta_flags(ap, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400427 key->conf.flags |=
428 IEEE80211_KEY_FLAG_WMM_STA;
Johannes Berg11a843b2007-08-28 17:01:55 -0400429 }
430 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400431 }
432
Johannes Bergb16bd152008-04-11 21:40:35 +0200433 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200434
Johannes Bergd4e46a32007-09-14 11:10:24 -0400435 if (sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100436 old_key = sta->key;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400437 else
Johannes Bergdb4d1162008-02-25 16:27:45 +0100438 old_key = sdata->keys[idx];
439
440 __ieee80211_key_replace(sdata, sta, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400441
Johannes Berg3b967662008-04-08 17:56:52 +0200442 /* free old key later */
443 add_todo(old_key, KEY_FLAG_TODO_DELETE);
444
445 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
Johannes Berg9607e6b2009-12-23 13:15:31 +0100446 if (ieee80211_sdata_running(sdata))
Johannes Berg3a245762008-04-09 16:45:37 +0200447 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
Johannes Berg523d2f62009-07-01 21:26:43 +0200448
449 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200450}
451
452static void __ieee80211_key_free(struct ieee80211_key *key)
453{
454 /*
455 * Replace key with nothingness if it was ever used.
456 */
457 if (key->sdata)
458 __ieee80211_key_replace(key->sdata, key->sta,
459 key, NULL);
460
461 add_todo(key, KEY_FLAG_TODO_DELETE);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200462}
463
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200464void ieee80211_key_free(struct ieee80211_key *key)
465{
Johannes Berg3b967662008-04-08 17:56:52 +0200466 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100467
Johannes Berg8f371712007-08-28 17:01:54 -0400468 if (!key)
469 return;
470
Emmanuel Grumbach00eb7fe2008-06-26 12:13:46 +0300471 if (!key->sdata) {
472 /* The key has not been linked yet, simply free it
473 * and don't Oops */
474 if (key->conf.alg == ALG_CCMP)
475 ieee80211_aes_key_free(key->u.ccmp.tfm);
476 kfree(key);
477 return;
478 }
479
Johannes Bergb16bd152008-04-11 21:40:35 +0200480 spin_lock_irqsave(&key->sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200481 __ieee80211_key_free(key);
Johannes Bergb16bd152008-04-11 21:40:35 +0200482 spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200483}
Johannes Berg11a843b2007-08-28 17:01:55 -0400484
Johannes Berg3a245762008-04-09 16:45:37 +0200485/*
486 * To be safe against concurrent manipulations of the list (which shouldn't
487 * actually happen) we need to hold the spinlock. But under the spinlock we
488 * can't actually do much, so we defer processing to the todo list. Then run
489 * the todo list to be sure the operation and possibly previously pending
490 * operations are completed.
491 */
492static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
493 u32 todo_flags)
494{
495 struct ieee80211_key *key;
496 unsigned long flags;
497
498 might_sleep();
499
Johannes Bergb16bd152008-04-11 21:40:35 +0200500 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200501 list_for_each_entry(key, &sdata->key_list, list)
502 add_todo(key, todo_flags);
Johannes Bergb16bd152008-04-11 21:40:35 +0200503 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200504
505 ieee80211_key_todo();
Johannes Berg3b967662008-04-08 17:56:52 +0200506}
507
508void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
509{
Johannes Berg3a245762008-04-09 16:45:37 +0200510 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200511
Johannes Berg9607e6b2009-12-23 13:15:31 +0100512 if (WARN_ON(!ieee80211_sdata_running(sdata)))
Johannes Berg3b967662008-04-08 17:56:52 +0200513 return;
514
Johannes Berg3a245762008-04-09 16:45:37 +0200515 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
Johannes Berg3b967662008-04-08 17:56:52 +0200516}
517
518void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
519{
Johannes Berg3a245762008-04-09 16:45:37 +0200520 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200521
Johannes Berg3a245762008-04-09 16:45:37 +0200522 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
Johannes Berg3b967662008-04-08 17:56:52 +0200523}
524
Johannes Berg3a245762008-04-09 16:45:37 +0200525static void __ieee80211_key_destroy(struct ieee80211_key *key)
Johannes Berg3b967662008-04-08 17:56:52 +0200526{
527 if (!key)
528 return;
529
530 ieee80211_key_disable_hw_accel(key);
531
Johannes Berg8f371712007-08-28 17:01:54 -0400532 if (key->conf.alg == ALG_CCMP)
533 ieee80211_aes_key_free(key->u.ccmp.tfm);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200534 if (key->conf.alg == ALG_AES_CMAC)
535 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
Johannes Berg8f371712007-08-28 17:01:54 -0400536 ieee80211_debugfs_key_remove(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400537
Johannes Berg8f371712007-08-28 17:01:54 -0400538 kfree(key);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200539}
Johannes Berg11a843b2007-08-28 17:01:55 -0400540
Johannes Berg3b967662008-04-08 17:56:52 +0200541static void __ieee80211_key_todo(void)
Johannes Berg11a843b2007-08-28 17:01:55 -0400542{
Johannes Berg3b967662008-04-08 17:56:52 +0200543 struct ieee80211_key *key;
544 bool work_done;
545 u32 todoflags;
Johannes Berg11a843b2007-08-28 17:01:55 -0400546
Johannes Berg3b967662008-04-08 17:56:52 +0200547 /*
548 * NB: sta_info_destroy relies on this!
549 */
550 synchronize_rcu();
Johannes Berg11a843b2007-08-28 17:01:55 -0400551
Johannes Berg523d2f62009-07-01 21:26:43 +0200552 spin_lock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200553 while (!list_empty(&todo_list)) {
554 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
555 list_del_init(&key->todo);
556 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
557 KEY_FLAG_TODO_DEFKEY |
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200558 KEY_FLAG_TODO_DEFMGMTKEY |
Johannes Berg3a245762008-04-09 16:45:37 +0200559 KEY_FLAG_TODO_HWACCEL_ADD |
560 KEY_FLAG_TODO_HWACCEL_REMOVE |
Johannes Berg3b967662008-04-08 17:56:52 +0200561 KEY_FLAG_TODO_DELETE);
562 key->flags &= ~todoflags;
Johannes Berg523d2f62009-07-01 21:26:43 +0200563 spin_unlock_bh(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400564
Johannes Berg3b967662008-04-08 17:56:52 +0200565 work_done = false;
Johannes Berg11a843b2007-08-28 17:01:55 -0400566
Johannes Berg3b967662008-04-08 17:56:52 +0200567 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
568 ieee80211_debugfs_key_add(key);
569 work_done = true;
570 }
571 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
572 ieee80211_debugfs_key_remove_default(key->sdata);
573 ieee80211_debugfs_key_add_default(key->sdata);
574 work_done = true;
575 }
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200576 if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) {
577 ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
578 ieee80211_debugfs_key_add_mgmt_default(key->sdata);
579 work_done = true;
580 }
Johannes Berg3a245762008-04-09 16:45:37 +0200581 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
Johannes Berg3b967662008-04-08 17:56:52 +0200582 ieee80211_key_enable_hw_accel(key);
583 work_done = true;
584 }
Johannes Berg3a245762008-04-09 16:45:37 +0200585 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
586 ieee80211_key_disable_hw_accel(key);
587 work_done = true;
588 }
Johannes Berg3b967662008-04-08 17:56:52 +0200589 if (todoflags & KEY_FLAG_TODO_DELETE) {
Johannes Berg3a245762008-04-09 16:45:37 +0200590 __ieee80211_key_destroy(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200591 work_done = true;
592 }
593
594 WARN_ON(!work_done);
595
Johannes Berg523d2f62009-07-01 21:26:43 +0200596 spin_lock_bh(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400597 }
Johannes Berg523d2f62009-07-01 21:26:43 +0200598 spin_unlock_bh(&todo_lock);
Johannes Berg3b967662008-04-08 17:56:52 +0200599}
600
601void ieee80211_key_todo(void)
602{
603 ieee80211_key_lock();
604 __ieee80211_key_todo();
605 ieee80211_key_unlock();
Johannes Berg11a843b2007-08-28 17:01:55 -0400606}
607
608void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
609{
610 struct ieee80211_key *key, *tmp;
Johannes Berg3a245762008-04-09 16:45:37 +0200611 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100612
Johannes Berg3b967662008-04-08 17:56:52 +0200613 ieee80211_key_lock();
614
615 ieee80211_debugfs_key_remove_default(sdata);
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200616 ieee80211_debugfs_key_remove_mgmt_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400617
Johannes Bergb16bd152008-04-11 21:40:35 +0200618 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg11a843b2007-08-28 17:01:55 -0400619 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg3a245762008-04-09 16:45:37 +0200620 __ieee80211_key_free(key);
Johannes Bergb16bd152008-04-11 21:40:35 +0200621 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg11a843b2007-08-28 17:01:55 -0400622
Johannes Berg3b967662008-04-08 17:56:52 +0200623 __ieee80211_key_todo();
Johannes Berg11a843b2007-08-28 17:01:55 -0400624
Johannes Berg3b967662008-04-08 17:56:52 +0200625 ieee80211_key_unlock();
Johannes Berg11a843b2007-08-28 17:01:55 -0400626}