blob: b0a025c9b615998795fb0895cf5e3c36a018235b [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"
19#include "debugfs_key.h"
20#include "aes_ccm.h"
21
Johannes Berg11a843b2007-08-28 17:01:55 -040022
Johannes Bergdbbea672008-02-26 14:34:06 +010023/**
24 * DOC: Key handling basics
Johannes Berg11a843b2007-08-28 17:01:55 -040025 *
26 * Key handling in mac80211 is done based on per-interface (sub_if_data)
27 * keys and per-station keys. Since each station belongs to an interface,
28 * each station key also belongs to that interface.
29 *
30 * Hardware acceleration is done on a best-effort basis, for each key
31 * that is eligible the hardware is asked to enable that key but if
32 * it cannot do that they key is simply kept for software encryption.
33 * There is currently no way of knowing this except by looking into
34 * debugfs.
35 *
Johannes Berg3b967662008-04-08 17:56:52 +020036 * All key operations are protected internally so you can call them at
37 * any time.
Johannes Bergdb4d1162008-02-25 16:27:45 +010038 *
Johannes Berg3b967662008-04-08 17:56:52 +020039 * Within mac80211, key references are, just as STA structure references,
40 * protected by RCU. Note, however, that some things are unprotected,
41 * namely the key->sta dereferences within the hardware acceleration
42 * functions. This means that sta_info_destroy() must flush the key todo
43 * list.
44 *
45 * All the direct key list manipulation functions must not sleep because
46 * they can operate on STA info structs that are protected by RCU.
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 Berg3b967662008-04-08 17:56:52 +020051/* key mutex: used to synchronise todo runners */
52static DEFINE_MUTEX(key_mutex);
53static DEFINE_SPINLOCK(todo_lock);
54static LIST_HEAD(todo_list);
55
56static void key_todo(struct work_struct *work)
57{
58 ieee80211_key_todo();
59}
60
61static DECLARE_WORK(todo_work, key_todo);
62
63/**
64 * add_todo - add todo item for a key
65 *
66 * @key: key to add to do item for
67 * @flag: todo flag(s)
68 */
69static void add_todo(struct ieee80211_key *key, u32 flag)
70{
71 if (!key)
72 return;
73
74 spin_lock(&todo_lock);
75 key->flags |= flag;
Johannes Berg245cbe72008-04-13 10:43:50 +020076 /*
77 * Remove again if already on the list so that we move it to the end.
78 */
79 if (!list_empty(&key->todo))
80 list_del(&key->todo);
81 list_add_tail(&key->todo, &todo_list);
Johannes Berg3b967662008-04-08 17:56:52 +020082 schedule_work(&todo_work);
83 spin_unlock(&todo_lock);
84}
85
86/**
87 * ieee80211_key_lock - lock the mac80211 key operation lock
88 *
89 * This locks the (global) mac80211 key operation lock, all
90 * key operations must be done under this lock.
91 */
92static void ieee80211_key_lock(void)
93{
94 mutex_lock(&key_mutex);
95}
96
97/**
98 * ieee80211_key_unlock - unlock the mac80211 key operation lock
99 */
100static void ieee80211_key_unlock(void)
101{
102 mutex_unlock(&key_mutex);
103}
104
105static void assert_key_lock(void)
106{
107 WARN_ON(!mutex_is_locked(&key_mutex));
108}
109
Johannes Bergdc822b52008-12-29 12:55:09 +0100110static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400111{
Johannes Berg11a843b2007-08-28 17:01:55 -0400112 if (key->sta)
Johannes Bergdc822b52008-12-29 12:55:09 +0100113 return &key->sta->sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400114
Johannes Bergdc822b52008-12-29 12:55:09 +0100115 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -0400116}
117
118static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
119{
Johannes Bergdc822b52008-12-29 12:55:09 +0100120 struct ieee80211_sub_if_data *sdata;
121 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400122 int ret;
123
Johannes Berg3b967662008-04-08 17:56:52 +0200124 assert_key_lock();
125 might_sleep();
126
Johannes Berg11a843b2007-08-28 17:01:55 -0400127 if (!key->local->ops->set_key)
128 return;
129
Johannes Bergdc822b52008-12-29 12:55:09 +0100130 sta = get_sta_for_key(key);
131
132 sdata = key->sdata;
133 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
134 sdata = container_of(sdata->bss,
135 struct ieee80211_sub_if_data,
136 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400137
138 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
Johannes Bergdc822b52008-12-29 12:55:09 +0100139 &sdata->vif, sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400140
Johannes Berg3b967662008-04-08 17:56:52 +0200141 if (!ret) {
142 spin_lock(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400143 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
Johannes Berg3b967662008-04-08 17:56:52 +0200144 spin_unlock(&todo_lock);
145 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400146
147 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
148 printk(KERN_ERR "mac80211-%s: failed to set key "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700149 "(%d, %pM) to hardware (%d)\n",
Johannes Berg11a843b2007-08-28 17:01:55 -0400150 wiphy_name(key->local->hw.wiphy),
Johannes Bergdc822b52008-12-29 12:55:09 +0100151 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400152}
153
154static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
155{
Johannes Bergdc822b52008-12-29 12:55:09 +0100156 struct ieee80211_sub_if_data *sdata;
157 struct ieee80211_sta *sta;
Johannes Berg11a843b2007-08-28 17:01:55 -0400158 int ret;
159
Johannes Berg3b967662008-04-08 17:56:52 +0200160 assert_key_lock();
161 might_sleep();
162
Johannes Bergdb4d1162008-02-25 16:27:45 +0100163 if (!key || !key->local->ops->set_key)
Johannes Berg11a843b2007-08-28 17:01:55 -0400164 return;
165
Johannes Berg3b967662008-04-08 17:56:52 +0200166 spin_lock(&todo_lock);
167 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
168 spin_unlock(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400169 return;
Johannes Berg3b967662008-04-08 17:56:52 +0200170 }
171 spin_unlock(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400172
Johannes Bergdc822b52008-12-29 12:55:09 +0100173 sta = get_sta_for_key(key);
174 sdata = key->sdata;
175
176 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
177 sdata = container_of(sdata->bss,
178 struct ieee80211_sub_if_data,
179 u.ap);
Johannes Berg11a843b2007-08-28 17:01:55 -0400180
181 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
Johannes Bergdc822b52008-12-29 12:55:09 +0100182 &sdata->vif, sta, &key->conf);
Johannes Berg11a843b2007-08-28 17:01:55 -0400183
184 if (ret)
185 printk(KERN_ERR "mac80211-%s: failed to remove key "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700186 "(%d, %pM) from hardware (%d)\n",
Johannes Berg11a843b2007-08-28 17:01:55 -0400187 wiphy_name(key->local->hw.wiphy),
Johannes Bergdc822b52008-12-29 12:55:09 +0100188 key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
Johannes Berg11a843b2007-08-28 17:01:55 -0400189
Johannes Berg3b967662008-04-08 17:56:52 +0200190 spin_lock(&todo_lock);
191 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
192 spin_unlock(&todo_lock);
193}
194
195static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
196 int idx)
197{
198 struct ieee80211_key *key = NULL;
199
200 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
201 key = sdata->keys[idx];
202
203 rcu_assign_pointer(sdata->default_key, key);
204
205 if (key)
206 add_todo(key, KEY_FLAG_TODO_DEFKEY);
207}
208
209void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
210{
211 unsigned long flags;
212
Johannes Bergb16bd152008-04-11 21:40:35 +0200213 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200214 __ieee80211_set_default_key(sdata, idx);
Johannes Bergb16bd152008-04-11 21:40:35 +0200215 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200216}
217
218
219static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
220 struct sta_info *sta,
221 struct ieee80211_key *old,
222 struct ieee80211_key *new)
223{
224 int idx, defkey;
225
226 if (new)
227 list_add(&new->list, &sdata->key_list);
228
229 if (sta) {
230 rcu_assign_pointer(sta->key, new);
231 } else {
232 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
233
234 if (old)
235 idx = old->conf.keyidx;
236 else
237 idx = new->conf.keyidx;
238
239 defkey = old && sdata->default_key == old;
240
241 if (defkey && !new)
242 __ieee80211_set_default_key(sdata, -1);
243
244 rcu_assign_pointer(sdata->keys[idx], new);
245 if (defkey && new)
246 __ieee80211_set_default_key(sdata, new->conf.keyidx);
247 }
248
249 if (old) {
250 /*
251 * We'll use an empty list to indicate that the key
252 * has already been removed.
253 */
254 list_del_init(&old->list);
255 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400256}
257
Johannes Bergdb4d1162008-02-25 16:27:45 +0100258struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
Johannes Berg11a843b2007-08-28 17:01:55 -0400259 int idx,
260 size_t key_len,
261 const u8 *key_data)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200262{
263 struct ieee80211_key *key;
264
Johannes Bergd4e46a32007-09-14 11:10:24 -0400265 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
Johannes Berg11a843b2007-08-28 17:01:55 -0400266
267 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200268 if (!key)
269 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -0400270
271 /*
272 * Default to software encryption; we'll later upload the
273 * key to the hardware if possible.
274 */
Johannes Berg11a843b2007-08-28 17:01:55 -0400275 key->conf.flags = 0;
276 key->flags = 0;
277
278 key->conf.alg = alg;
279 key->conf.keyidx = idx;
280 key->conf.keylen = key_len;
Felix Fietkau76708de2008-10-05 18:02:48 +0200281 switch (alg) {
282 case ALG_WEP:
283 key->conf.iv_len = WEP_IV_LEN;
284 key->conf.icv_len = WEP_ICV_LEN;
285 break;
286 case ALG_TKIP:
287 key->conf.iv_len = TKIP_IV_LEN;
288 key->conf.icv_len = TKIP_ICV_LEN;
289 break;
290 case ALG_CCMP:
291 key->conf.iv_len = CCMP_HDR_LEN;
292 key->conf.icv_len = CCMP_MIC_LEN;
293 break;
294 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400295 memcpy(key->conf.key, key_data, key_len);
Johannes Berge48618292008-02-27 13:39:00 +0100296 INIT_LIST_HEAD(&key->list);
Johannes Berg3b967662008-04-08 17:56:52 +0200297 INIT_LIST_HEAD(&key->todo);
Johannes Berg11a843b2007-08-28 17:01:55 -0400298
Johannes Berg11a843b2007-08-28 17:01:55 -0400299 if (alg == ALG_CCMP) {
300 /*
301 * Initialize AES key state here as an optimization so that
302 * it does not need to be initialized for every packet.
303 */
304 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
305 if (!key->u.ccmp.tfm) {
Johannes Berg3b967662008-04-08 17:56:52 +0200306 kfree(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400307 return NULL;
308 }
309 }
310
Johannes Bergdb4d1162008-02-25 16:27:45 +0100311 return key;
312}
Johannes Berg11a843b2007-08-28 17:01:55 -0400313
Johannes Bergdb4d1162008-02-25 16:27:45 +0100314void ieee80211_key_link(struct ieee80211_key *key,
315 struct ieee80211_sub_if_data *sdata,
316 struct sta_info *sta)
317{
318 struct ieee80211_key *old_key;
Johannes Berg3b967662008-04-08 17:56:52 +0200319 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100320 int idx;
321
Johannes Bergdb4d1162008-02-25 16:27:45 +0100322 BUG_ON(!sdata);
323 BUG_ON(!key);
324
325 idx = key->conf.keyidx;
326 key->local = sdata->local;
327 key->sdata = sdata;
328 key->sta = sta;
329
Johannes Berg11a843b2007-08-28 17:01:55 -0400330 if (sta) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400331 /*
332 * some hardware cannot handle TKIP with QoS, so
333 * we indicate whether QoS could be in use.
334 */
Johannes Berg07346f812008-05-03 01:02:02 +0200335 if (test_sta_flags(sta, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400336 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
Ivo van Doornc6adbd22008-04-17 21:11:18 +0200337
338 /*
339 * This key is for a specific sta interface,
340 * inform the driver that it should try to store
341 * this key as pairwise key.
342 */
343 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
Johannes Berg11a843b2007-08-28 17:01:55 -0400344 } else {
Johannes Berg05c914f2008-09-11 00:01:58 +0200345 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg11a843b2007-08-28 17:01:55 -0400346 struct sta_info *ap;
347
Johannes Berg3b967662008-04-08 17:56:52 +0200348 /*
349 * We're getting a sta pointer in,
350 * so must be under RCU read lock.
351 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100352
Johannes Berg11a843b2007-08-28 17:01:55 -0400353 /* same here, the AP could be using QoS */
354 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
355 if (ap) {
Johannes Berg07346f812008-05-03 01:02:02 +0200356 if (test_sta_flags(ap, WLAN_STA_WME))
Johannes Berg11a843b2007-08-28 17:01:55 -0400357 key->conf.flags |=
358 IEEE80211_KEY_FLAG_WMM_STA;
Johannes Berg11a843b2007-08-28 17:01:55 -0400359 }
360 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400361 }
362
Johannes Bergb16bd152008-04-11 21:40:35 +0200363 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3b967662008-04-08 17:56:52 +0200364
Johannes Bergd4e46a32007-09-14 11:10:24 -0400365 if (sta)
Johannes Bergdb4d1162008-02-25 16:27:45 +0100366 old_key = sta->key;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400367 else
Johannes Bergdb4d1162008-02-25 16:27:45 +0100368 old_key = sdata->keys[idx];
369
370 __ieee80211_key_replace(sdata, sta, old_key, key);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400371
Johannes Bergb16bd152008-04-11 21:40:35 +0200372 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400373
Johannes Berg3b967662008-04-08 17:56:52 +0200374 /* free old key later */
375 add_todo(old_key, KEY_FLAG_TODO_DELETE);
376
377 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
Johannes Berge48618292008-02-27 13:39:00 +0100378 if (netif_running(sdata->dev))
Johannes Berg3a245762008-04-09 16:45:37 +0200379 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
380}
381
382static void __ieee80211_key_free(struct ieee80211_key *key)
383{
384 /*
385 * Replace key with nothingness if it was ever used.
386 */
387 if (key->sdata)
388 __ieee80211_key_replace(key->sdata, key->sta,
389 key, NULL);
390
391 add_todo(key, KEY_FLAG_TODO_DELETE);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200392}
393
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200394void ieee80211_key_free(struct ieee80211_key *key)
395{
Johannes Berg3b967662008-04-08 17:56:52 +0200396 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100397
Johannes Berg8f371712007-08-28 17:01:54 -0400398 if (!key)
399 return;
400
Emmanuel Grumbach00eb7fe2008-06-26 12:13:46 +0300401 if (!key->sdata) {
402 /* The key has not been linked yet, simply free it
403 * and don't Oops */
404 if (key->conf.alg == ALG_CCMP)
405 ieee80211_aes_key_free(key->u.ccmp.tfm);
406 kfree(key);
407 return;
408 }
409
Johannes Bergb16bd152008-04-11 21:40:35 +0200410 spin_lock_irqsave(&key->sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200411 __ieee80211_key_free(key);
Johannes Bergb16bd152008-04-11 21:40:35 +0200412 spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200413}
Johannes Berg11a843b2007-08-28 17:01:55 -0400414
Johannes Berg3a245762008-04-09 16:45:37 +0200415/*
416 * To be safe against concurrent manipulations of the list (which shouldn't
417 * actually happen) we need to hold the spinlock. But under the spinlock we
418 * can't actually do much, so we defer processing to the todo list. Then run
419 * the todo list to be sure the operation and possibly previously pending
420 * operations are completed.
421 */
422static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
423 u32 todo_flags)
424{
425 struct ieee80211_key *key;
426 unsigned long flags;
427
428 might_sleep();
429
Johannes Bergb16bd152008-04-11 21:40:35 +0200430 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200431 list_for_each_entry(key, &sdata->key_list, list)
432 add_todo(key, todo_flags);
Johannes Bergb16bd152008-04-11 21:40:35 +0200433 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg3a245762008-04-09 16:45:37 +0200434
435 ieee80211_key_todo();
Johannes Berg3b967662008-04-08 17:56:52 +0200436}
437
438void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
439{
Johannes Berg3a245762008-04-09 16:45:37 +0200440 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200441
442 if (WARN_ON(!netif_running(sdata->dev)))
443 return;
444
Johannes Berg3a245762008-04-09 16:45:37 +0200445 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
Johannes Berg3b967662008-04-08 17:56:52 +0200446}
447
448void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
449{
Johannes Berg3a245762008-04-09 16:45:37 +0200450 ASSERT_RTNL();
Johannes Berg3b967662008-04-08 17:56:52 +0200451
Johannes Berg3a245762008-04-09 16:45:37 +0200452 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
Johannes Berg3b967662008-04-08 17:56:52 +0200453}
454
Johannes Berg3a245762008-04-09 16:45:37 +0200455static void __ieee80211_key_destroy(struct ieee80211_key *key)
Johannes Berg3b967662008-04-08 17:56:52 +0200456{
457 if (!key)
458 return;
459
460 ieee80211_key_disable_hw_accel(key);
461
Johannes Berg8f371712007-08-28 17:01:54 -0400462 if (key->conf.alg == ALG_CCMP)
463 ieee80211_aes_key_free(key->u.ccmp.tfm);
464 ieee80211_debugfs_key_remove(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400465
Johannes Berg8f371712007-08-28 17:01:54 -0400466 kfree(key);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200467}
Johannes Berg11a843b2007-08-28 17:01:55 -0400468
Johannes Berg3b967662008-04-08 17:56:52 +0200469static void __ieee80211_key_todo(void)
Johannes Berg11a843b2007-08-28 17:01:55 -0400470{
Johannes Berg3b967662008-04-08 17:56:52 +0200471 struct ieee80211_key *key;
472 bool work_done;
473 u32 todoflags;
Johannes Berg11a843b2007-08-28 17:01:55 -0400474
Johannes Berg3b967662008-04-08 17:56:52 +0200475 /*
476 * NB: sta_info_destroy relies on this!
477 */
478 synchronize_rcu();
Johannes Berg11a843b2007-08-28 17:01:55 -0400479
Johannes Berg3b967662008-04-08 17:56:52 +0200480 spin_lock(&todo_lock);
481 while (!list_empty(&todo_list)) {
482 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
483 list_del_init(&key->todo);
484 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
485 KEY_FLAG_TODO_DEFKEY |
Johannes Berg3a245762008-04-09 16:45:37 +0200486 KEY_FLAG_TODO_HWACCEL_ADD |
487 KEY_FLAG_TODO_HWACCEL_REMOVE |
Johannes Berg3b967662008-04-08 17:56:52 +0200488 KEY_FLAG_TODO_DELETE);
489 key->flags &= ~todoflags;
490 spin_unlock(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400491
Johannes Berg3b967662008-04-08 17:56:52 +0200492 work_done = false;
Johannes Berg11a843b2007-08-28 17:01:55 -0400493
Johannes Berg3b967662008-04-08 17:56:52 +0200494 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
495 ieee80211_debugfs_key_add(key);
496 work_done = true;
497 }
498 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
499 ieee80211_debugfs_key_remove_default(key->sdata);
500 ieee80211_debugfs_key_add_default(key->sdata);
501 work_done = true;
502 }
Johannes Berg3a245762008-04-09 16:45:37 +0200503 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
Johannes Berg3b967662008-04-08 17:56:52 +0200504 ieee80211_key_enable_hw_accel(key);
505 work_done = true;
506 }
Johannes Berg3a245762008-04-09 16:45:37 +0200507 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
508 ieee80211_key_disable_hw_accel(key);
509 work_done = true;
510 }
Johannes Berg3b967662008-04-08 17:56:52 +0200511 if (todoflags & KEY_FLAG_TODO_DELETE) {
Johannes Berg3a245762008-04-09 16:45:37 +0200512 __ieee80211_key_destroy(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200513 work_done = true;
514 }
515
516 WARN_ON(!work_done);
517
518 spin_lock(&todo_lock);
Johannes Berg11a843b2007-08-28 17:01:55 -0400519 }
Johannes Berg3b967662008-04-08 17:56:52 +0200520 spin_unlock(&todo_lock);
521}
522
523void ieee80211_key_todo(void)
524{
525 ieee80211_key_lock();
526 __ieee80211_key_todo();
527 ieee80211_key_unlock();
Johannes Berg11a843b2007-08-28 17:01:55 -0400528}
529
530void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
531{
532 struct ieee80211_key *key, *tmp;
Johannes Berg3a245762008-04-09 16:45:37 +0200533 unsigned long flags;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100534
Johannes Berg3b967662008-04-08 17:56:52 +0200535 ieee80211_key_lock();
536
537 ieee80211_debugfs_key_remove_default(sdata);
Johannes Berg11a843b2007-08-28 17:01:55 -0400538
Johannes Bergb16bd152008-04-11 21:40:35 +0200539 spin_lock_irqsave(&sdata->local->key_lock, flags);
Johannes Berg11a843b2007-08-28 17:01:55 -0400540 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
Johannes Berg3a245762008-04-09 16:45:37 +0200541 __ieee80211_key_free(key);
Johannes Bergb16bd152008-04-11 21:40:35 +0200542 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
Johannes Berg11a843b2007-08-28 17:01:55 -0400543
Johannes Berg3b967662008-04-08 17:56:52 +0200544 __ieee80211_key_todo();
Johannes Berg11a843b2007-08-28 17:01:55 -0400545
Johannes Berg3b967662008-04-08 17:56:52 +0200546 ieee80211_key_unlock();
Johannes Berg11a843b2007-08-28 17:01:55 -0400547}