1
2
3
4
5
6
7
8
9
10
11
12#include <linux/if_ether.h>
13#include <linux/etherdevice.h>
14#include <linux/list.h>
15#include <linux/rcupdate.h>
16#include <linux/rtnetlink.h>
17#include <net/mac80211.h>
18#include "ieee80211_i.h"
19#include "debugfs_key.h"
20#include "aes_ccm.h"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
50static const u8 zero_addr[ETH_ALEN];
51
52
53static DEFINE_MUTEX(key_mutex);
54static DEFINE_SPINLOCK(todo_lock);
55static LIST_HEAD(todo_list);
56
57static void key_todo(struct work_struct *work)
58{
59 ieee80211_key_todo();
60}
61
62static DECLARE_WORK(todo_work, key_todo);
63
64
65
66
67
68
69
70static void add_todo(struct ieee80211_key *key, u32 flag)
71{
72 if (!key)
73 return;
74
75 spin_lock(&todo_lock);
76 key->flags |= flag;
77
78
79
80 if (!list_empty(&key->todo))
81 list_del(&key->todo);
82 list_add_tail(&key->todo, &todo_list);
83 schedule_work(&todo_work);
84 spin_unlock(&todo_lock);
85}
86
87
88
89
90
91
92
93static void ieee80211_key_lock(void)
94{
95 mutex_lock(&key_mutex);
96}
97
98
99
100
101static void ieee80211_key_unlock(void)
102{
103 mutex_unlock(&key_mutex);
104}
105
106static void assert_key_lock(void)
107{
108 WARN_ON(!mutex_is_locked(&key_mutex));
109}
110
111static const u8 *get_mac_for_key(struct ieee80211_key *key)
112{
113 const u8 *addr = bcast_addr;
114
115
116
117
118
119
120 if (key->conf.alg != ALG_WEP &&
121 (key->sdata->vif.type == NL80211_IFTYPE_AP ||
122 key->sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
123 addr = zero_addr;
124
125 if (key->sta)
126 addr = key->sta->sta.addr;
127
128 return addr;
129}
130
131static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
132{
133 const u8 *addr;
134 int ret;
135 DECLARE_MAC_BUF(mac);
136
137 assert_key_lock();
138 might_sleep();
139
140 if (!key->local->ops->set_key)
141 return;
142
143 addr = get_mac_for_key(key);
144
145 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
146 key->sdata->dev->dev_addr, addr,
147 &key->conf);
148
149 if (!ret) {
150 spin_lock(&todo_lock);
151 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
152 spin_unlock(&todo_lock);
153 }
154
155 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
156 printk(KERN_ERR "mac80211-%s: failed to set key "
157 "(%d, %s) to hardware (%d)\n",
158 wiphy_name(key->local->hw.wiphy),
159 key->conf.keyidx, print_mac(mac, addr), ret);
160}
161
162static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
163{
164 const u8 *addr;
165 int ret;
166 DECLARE_MAC_BUF(mac);
167
168 assert_key_lock();
169 might_sleep();
170
171 if (!key || !key->local->ops->set_key)
172 return;
173
174 spin_lock(&todo_lock);
175 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
176 spin_unlock(&todo_lock);
177 return;
178 }
179 spin_unlock(&todo_lock);
180
181 addr = get_mac_for_key(key);
182
183 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
184 key->sdata->dev->dev_addr, addr,
185 &key->conf);
186
187 if (ret)
188 printk(KERN_ERR "mac80211-%s: failed to remove key "
189 "(%d, %s) from hardware (%d)\n",
190 wiphy_name(key->local->hw.wiphy),
191 key->conf.keyidx, print_mac(mac, addr), ret);
192
193 spin_lock(&todo_lock);
194 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
195 spin_unlock(&todo_lock);
196}
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
216 spin_lock_irqsave(&sdata->local->key_lock, flags);
217 __ieee80211_set_default_key(sdata, idx);
218 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
219}
220
221
222static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
223 struct sta_info *sta,
224 struct ieee80211_key *old,
225 struct ieee80211_key *new)
226{
227 int idx, defkey;
228
229 if (new)
230 list_add(&new->list, &sdata->key_list);
231
232 if (sta) {
233 rcu_assign_pointer(sta->key, new);
234 } else {
235 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
236
237 if (old)
238 idx = old->conf.keyidx;
239 else
240 idx = new->conf.keyidx;
241
242 defkey = old && sdata->default_key == old;
243
244 if (defkey && !new)
245 __ieee80211_set_default_key(sdata, -1);
246
247 rcu_assign_pointer(sdata->keys[idx], new);
248 if (defkey && new)
249 __ieee80211_set_default_key(sdata, new->conf.keyidx);
250 }
251
252 if (old) {
253
254
255
256
257 list_del_init(&old->list);
258 }
259}
260
261struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
262 int idx,
263 size_t key_len,
264 const u8 *key_data)
265{
266 struct ieee80211_key *key;
267
268 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
269
270 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
271 if (!key)
272 return NULL;
273
274
275
276
277
278 key->conf.flags = 0;
279 key->flags = 0;
280
281 key->conf.alg = alg;
282 key->conf.keyidx = idx;
283 key->conf.keylen = key_len;
284 switch (alg) {
285 case ALG_WEP:
286 key->conf.iv_len = WEP_IV_LEN;
287 key->conf.icv_len = WEP_ICV_LEN;
288 break;
289 case ALG_TKIP:
290 key->conf.iv_len = TKIP_IV_LEN;
291 key->conf.icv_len = TKIP_ICV_LEN;
292 break;
293 case ALG_CCMP:
294 key->conf.iv_len = CCMP_HDR_LEN;
295 key->conf.icv_len = CCMP_MIC_LEN;
296 break;
297 }
298 memcpy(key->conf.key, key_data, key_len);
299 INIT_LIST_HEAD(&key->list);
300 INIT_LIST_HEAD(&key->todo);
301
302 if (alg == ALG_CCMP) {
303
304
305
306
307 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
308 if (!key->u.ccmp.tfm) {
309 kfree(key);
310 return NULL;
311 }
312 }
313
314 return key;
315}
316
317void ieee80211_key_link(struct ieee80211_key *key,
318 struct ieee80211_sub_if_data *sdata,
319 struct sta_info *sta)
320{
321 struct ieee80211_key *old_key;
322 unsigned long flags;
323 int idx;
324
325 BUG_ON(!sdata);
326 BUG_ON(!key);
327
328 idx = key->conf.keyidx;
329 key->local = sdata->local;
330 key->sdata = sdata;
331 key->sta = sta;
332
333 if (sta) {
334
335
336
337
338 if (test_sta_flags(sta, WLAN_STA_WME))
339 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
340
341
342
343
344
345
346 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
347 } else {
348 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
349 struct sta_info *ap;
350
351
352
353
354
355
356
357 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
358 if (ap) {
359 if (test_sta_flags(ap, WLAN_STA_WME))
360 key->conf.flags |=
361 IEEE80211_KEY_FLAG_WMM_STA;
362 }
363 }
364 }
365
366 spin_lock_irqsave(&sdata->local->key_lock, flags);
367
368 if (sta)
369 old_key = sta->key;
370 else
371 old_key = sdata->keys[idx];
372
373 __ieee80211_key_replace(sdata, sta, old_key, key);
374
375 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
376
377
378 add_todo(old_key, KEY_FLAG_TODO_DELETE);
379
380 add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
381 if (netif_running(sdata->dev))
382 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
383}
384
385static void __ieee80211_key_free(struct ieee80211_key *key)
386{
387
388
389
390 if (key->sdata)
391 __ieee80211_key_replace(key->sdata, key->sta,
392 key, NULL);
393
394 add_todo(key, KEY_FLAG_TODO_DELETE);
395}
396
397void ieee80211_key_free(struct ieee80211_key *key)
398{
399 unsigned long flags;
400
401 if (!key)
402 return;
403
404 if (!key->sdata) {
405
406
407 if (key->conf.alg == ALG_CCMP)
408 ieee80211_aes_key_free(key->u.ccmp.tfm);
409 kfree(key);
410 return;
411 }
412
413 spin_lock_irqsave(&key->sdata->local->key_lock, flags);
414 __ieee80211_key_free(key);
415 spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
416}
417
418
419
420
421
422
423
424
425static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
426 u32 todo_flags)
427{
428 struct ieee80211_key *key;
429 unsigned long flags;
430
431 might_sleep();
432
433 spin_lock_irqsave(&sdata->local->key_lock, flags);
434 list_for_each_entry(key, &sdata->key_list, list)
435 add_todo(key, todo_flags);
436 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
437
438 ieee80211_key_todo();
439}
440
441void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
442{
443 ASSERT_RTNL();
444
445 if (WARN_ON(!netif_running(sdata->dev)))
446 return;
447
448 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
449}
450
451void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
452{
453 ASSERT_RTNL();
454
455 ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
456}
457
458static void __ieee80211_key_destroy(struct ieee80211_key *key)
459{
460 if (!key)
461 return;
462
463 ieee80211_key_disable_hw_accel(key);
464
465 if (key->conf.alg == ALG_CCMP)
466 ieee80211_aes_key_free(key->u.ccmp.tfm);
467 ieee80211_debugfs_key_remove(key);
468
469 kfree(key);
470}
471
472static void __ieee80211_key_todo(void)
473{
474 struct ieee80211_key *key;
475 bool work_done;
476 u32 todoflags;
477
478
479
480
481 synchronize_rcu();
482
483 spin_lock(&todo_lock);
484 while (!list_empty(&todo_list)) {
485 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
486 list_del_init(&key->todo);
487 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
488 KEY_FLAG_TODO_DEFKEY |
489 KEY_FLAG_TODO_HWACCEL_ADD |
490 KEY_FLAG_TODO_HWACCEL_REMOVE |
491 KEY_FLAG_TODO_DELETE);
492 key->flags &= ~todoflags;
493 spin_unlock(&todo_lock);
494
495 work_done = false;
496
497 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
498 ieee80211_debugfs_key_add(key);
499 work_done = true;
500 }
501 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
502 ieee80211_debugfs_key_remove_default(key->sdata);
503 ieee80211_debugfs_key_add_default(key->sdata);
504 work_done = true;
505 }
506 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
507 ieee80211_key_enable_hw_accel(key);
508 work_done = true;
509 }
510 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
511 ieee80211_key_disable_hw_accel(key);
512 work_done = true;
513 }
514 if (todoflags & KEY_FLAG_TODO_DELETE) {
515 __ieee80211_key_destroy(key);
516 work_done = true;
517 }
518
519 WARN_ON(!work_done);
520
521 spin_lock(&todo_lock);
522 }
523 spin_unlock(&todo_lock);
524}
525
526void ieee80211_key_todo(void)
527{
528 ieee80211_key_lock();
529 __ieee80211_key_todo();
530 ieee80211_key_unlock();
531}
532
533void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
534{
535 struct ieee80211_key *key, *tmp;
536 unsigned long flags;
537
538 ieee80211_key_lock();
539
540 ieee80211_debugfs_key_remove_default(sdata);
541
542 spin_lock_irqsave(&sdata->local->key_lock, flags);
543 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
544 __ieee80211_key_free(key);
545 spin_unlock_irqrestore(&sdata->local->key_lock, flags);
546
547 __ieee80211_key_todo();
548
549 ieee80211_key_unlock();
550}