compat-wireless-2010-03-10
[pandora-wifi.git] / net / mac80211 / key.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007-2008  Johannes Berg <johannes@sipsolutions.net>
6  *
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
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 "driver-ops.h"
20 #include "debugfs_key.h"
21 #include "aes_ccm.h"
22 #include "aes_cmac.h"
23
24 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29))
25 #include <asm/unaligned.h>
26 #endif
27
28 /**
29  * DOC: Key handling basics
30  *
31  * Key handling in mac80211 is done based on per-interface (sub_if_data)
32  * keys and per-station keys. Since each station belongs to an interface,
33  * each station key also belongs to that interface.
34  *
35  * Hardware acceleration is done on a best-effort basis, for each key
36  * that is eligible the hardware is asked to enable that key but if
37  * it cannot do that they key is simply kept for software encryption.
38  * There is currently no way of knowing this except by looking into
39  * debugfs.
40  *
41  * All key operations are protected internally so you can call them at
42  * any time.
43  *
44  * Within mac80211, key references are, just as STA structure references,
45  * protected by RCU. Note, however, that some things are unprotected,
46  * namely the key->sta dereferences within the hardware acceleration
47  * functions. This means that sta_info_destroy() must flush the key todo
48  * list.
49  *
50  * All the direct key list manipulation functions must not sleep because
51  * they can operate on STA info structs that are protected by RCU.
52  */
53
54 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
55
56 /* key mutex: used to synchronise todo runners */
57 static DEFINE_MUTEX(key_mutex);
58 static DEFINE_SPINLOCK(todo_lock);
59 static LIST_HEAD(todo_list);
60
61 static void key_todo(struct work_struct *work)
62 {
63         ieee80211_key_todo();
64 }
65
66 static DECLARE_WORK(todo_work, key_todo);
67
68 /**
69  * add_todo - add todo item for a key
70  *
71  * @key: key to add to do item for
72  * @flag: todo flag(s)
73  *
74  * Must be called with IRQs or softirqs disabled.
75  */
76 static void add_todo(struct ieee80211_key *key, u32 flag)
77 {
78         if (!key)
79                 return;
80
81         spin_lock(&todo_lock);
82         key->flags |= flag;
83         /*
84          * Remove again if already on the list so that we move it to the end.
85          */
86         if (!list_empty(&key->todo))
87                 list_del(&key->todo);
88         list_add_tail(&key->todo, &todo_list);
89         schedule_work(&todo_work);
90         spin_unlock(&todo_lock);
91 }
92
93 /**
94  * ieee80211_key_lock - lock the mac80211 key operation lock
95  *
96  * This locks the (global) mac80211 key operation lock, all
97  * key operations must be done under this lock.
98  */
99 static void ieee80211_key_lock(void)
100 {
101         mutex_lock(&key_mutex);
102 }
103
104 /**
105  * ieee80211_key_unlock - unlock the mac80211 key operation lock
106  */
107 static void ieee80211_key_unlock(void)
108 {
109         mutex_unlock(&key_mutex);
110 }
111
112 static void assert_key_lock(void)
113 {
114         WARN_ON(!mutex_is_locked(&key_mutex));
115 }
116
117 static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
118 {
119         if (key->sta)
120                 return &key->sta->sta;
121
122         return NULL;
123 }
124
125 static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
126 {
127         struct ieee80211_sub_if_data *sdata;
128         struct ieee80211_sta *sta;
129         int ret;
130
131         assert_key_lock();
132         might_sleep();
133
134         if (!key->local->ops->set_key)
135                 return;
136
137         sta = get_sta_for_key(key);
138
139         sdata = key->sdata;
140         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
141                 sdata = container_of(sdata->bss,
142                                      struct ieee80211_sub_if_data,
143                                      u.ap);
144
145         ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
146
147         if (!ret) {
148                 spin_lock_bh(&todo_lock);
149                 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
150                 spin_unlock_bh(&todo_lock);
151         }
152
153         if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
154                 printk(KERN_ERR "mac80211-%s: failed to set key "
155                        "(%d, %pM) to hardware (%d)\n",
156                        wiphy_name(key->local->hw.wiphy),
157                        key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
158 }
159
160 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
161 {
162         struct ieee80211_sub_if_data *sdata;
163         struct ieee80211_sta *sta;
164         int ret;
165
166         assert_key_lock();
167         might_sleep();
168
169         if (!key || !key->local->ops->set_key)
170                 return;
171
172         spin_lock_bh(&todo_lock);
173         if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
174                 spin_unlock_bh(&todo_lock);
175                 return;
176         }
177         spin_unlock_bh(&todo_lock);
178
179         sta = get_sta_for_key(key);
180         sdata = key->sdata;
181
182         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
183                 sdata = container_of(sdata->bss,
184                                      struct ieee80211_sub_if_data,
185                                      u.ap);
186
187         ret = drv_set_key(key->local, DISABLE_KEY, sdata,
188                           sta, &key->conf);
189
190         if (ret)
191                 printk(KERN_ERR "mac80211-%s: failed to remove key "
192                        "(%d, %pM) from hardware (%d)\n",
193                        wiphy_name(key->local->hw.wiphy),
194                        key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
195
196         spin_lock_bh(&todo_lock);
197         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
198         spin_unlock_bh(&todo_lock);
199 }
200
201 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
202                                         int idx)
203 {
204         struct ieee80211_key *key = NULL;
205
206         if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
207                 key = sdata->keys[idx];
208
209         rcu_assign_pointer(sdata->default_key, key);
210
211         if (key)
212                 add_todo(key, KEY_FLAG_TODO_DEFKEY);
213 }
214
215 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
216 {
217         unsigned long flags;
218
219         spin_lock_irqsave(&sdata->local->key_lock, flags);
220         __ieee80211_set_default_key(sdata, idx);
221         spin_unlock_irqrestore(&sdata->local->key_lock, flags);
222 }
223
224 static void
225 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
226 {
227         struct ieee80211_key *key = NULL;
228
229         if (idx >= NUM_DEFAULT_KEYS &&
230             idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
231                 key = sdata->keys[idx];
232
233         rcu_assign_pointer(sdata->default_mgmt_key, key);
234
235         if (key)
236                 add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY);
237 }
238
239 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
240                                     int idx)
241 {
242         unsigned long flags;
243
244         spin_lock_irqsave(&sdata->local->key_lock, flags);
245         __ieee80211_set_default_mgmt_key(sdata, idx);
246         spin_unlock_irqrestore(&sdata->local->key_lock, flags);
247 }
248
249
250 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
251                                     struct sta_info *sta,
252                                     struct ieee80211_key *old,
253                                     struct ieee80211_key *new)
254 {
255         int idx, defkey, defmgmtkey;
256
257         if (new)
258                 list_add(&new->list, &sdata->key_list);
259
260         if (sta) {
261                 rcu_assign_pointer(sta->key, new);
262         } else {
263                 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
264
265                 if (old)
266                         idx = old->conf.keyidx;
267                 else
268                         idx = new->conf.keyidx;
269
270                 defkey = old && sdata->default_key == old;
271                 defmgmtkey = old && sdata->default_mgmt_key == old;
272
273                 if (defkey && !new)
274                         __ieee80211_set_default_key(sdata, -1);
275                 if (defmgmtkey && !new)
276                         __ieee80211_set_default_mgmt_key(sdata, -1);
277
278                 rcu_assign_pointer(sdata->keys[idx], new);
279                 if (defkey && new)
280                         __ieee80211_set_default_key(sdata, new->conf.keyidx);
281                 if (defmgmtkey && new)
282                         __ieee80211_set_default_mgmt_key(sdata,
283                                                          new->conf.keyidx);
284         }
285
286         if (old) {
287                 /*
288                  * We'll use an empty list to indicate that the key
289                  * has already been removed.
290                  */
291                 list_del_init(&old->list);
292         }
293 }
294
295 struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
296                                           int idx,
297                                           size_t key_len,
298                                           const u8 *key_data,
299                                           size_t seq_len, const u8 *seq)
300 {
301         struct ieee80211_key *key;
302         int i, j;
303
304         BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
305
306         key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
307         if (!key)
308                 return NULL;
309
310         /*
311          * Default to software encryption; we'll later upload the
312          * key to the hardware if possible.
313          */
314         key->conf.flags = 0;
315         key->flags = 0;
316
317         key->conf.alg = alg;
318         key->conf.keyidx = idx;
319         key->conf.keylen = key_len;
320         switch (alg) {
321         case ALG_WEP:
322                 key->conf.iv_len = WEP_IV_LEN;
323                 key->conf.icv_len = WEP_ICV_LEN;
324                 break;
325         case ALG_TKIP:
326                 key->conf.iv_len = TKIP_IV_LEN;
327                 key->conf.icv_len = TKIP_ICV_LEN;
328                 if (seq) {
329                         for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
330                                 key->u.tkip.rx[i].iv32 =
331                                         get_unaligned_le32(&seq[2]);
332                                 key->u.tkip.rx[i].iv16 =
333                                         get_unaligned_le16(seq);
334                         }
335                 }
336                 break;
337         case ALG_CCMP:
338                 key->conf.iv_len = CCMP_HDR_LEN;
339                 key->conf.icv_len = CCMP_MIC_LEN;
340                 if (seq) {
341                         for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
342                                 for (j = 0; j < CCMP_PN_LEN; j++)
343                                         key->u.ccmp.rx_pn[i][j] =
344                                                 seq[CCMP_PN_LEN - j - 1];
345                 }
346                 break;
347         case ALG_AES_CMAC:
348                 key->conf.iv_len = 0;
349                 key->conf.icv_len = sizeof(struct ieee80211_mmie);
350                 if (seq)
351                         for (j = 0; j < 6; j++)
352                                 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
353                 break;
354         }
355         memcpy(key->conf.key, key_data, key_len);
356         INIT_LIST_HEAD(&key->list);
357         INIT_LIST_HEAD(&key->todo);
358
359         if (alg == ALG_CCMP) {
360                 /*
361                  * Initialize AES key state here as an optimization so that
362                  * it does not need to be initialized for every packet.
363                  */
364                 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
365                 if (!key->u.ccmp.tfm) {
366                         kfree(key);
367                         return NULL;
368                 }
369         }
370
371         if (alg == ALG_AES_CMAC) {
372                 /*
373                  * Initialize AES key state here as an optimization so that
374                  * it does not need to be initialized for every packet.
375                  */
376                 key->u.aes_cmac.tfm =
377                         ieee80211_aes_cmac_key_setup(key_data);
378                 if (!key->u.aes_cmac.tfm) {
379                         kfree(key);
380                         return NULL;
381                 }
382         }
383
384         return key;
385 }
386
387 void ieee80211_key_link(struct ieee80211_key *key,
388                         struct ieee80211_sub_if_data *sdata,
389                         struct sta_info *sta)
390 {
391         struct ieee80211_key *old_key;
392         unsigned long flags;
393         int idx;
394
395         BUG_ON(!sdata);
396         BUG_ON(!key);
397
398         idx = key->conf.keyidx;
399         key->local = sdata->local;
400         key->sdata = sdata;
401         key->sta = sta;
402
403         if (sta) {
404                 /*
405                  * some hardware cannot handle TKIP with QoS, so
406                  * we indicate whether QoS could be in use.
407                  */
408                 if (test_sta_flags(sta, WLAN_STA_WME))
409                         key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
410
411                 /*
412                  * This key is for a specific sta interface,
413                  * inform the driver that it should try to store
414                  * this key as pairwise key.
415                  */
416                 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
417         } else {
418                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
419                         struct sta_info *ap;
420
421                         /*
422                          * We're getting a sta pointer in,
423                          * so must be under RCU read lock.
424                          */
425
426                         /* same here, the AP could be using QoS */
427                         ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
428                         if (ap) {
429                                 if (test_sta_flags(ap, WLAN_STA_WME))
430                                         key->conf.flags |=
431                                                 IEEE80211_KEY_FLAG_WMM_STA;
432                         }
433                 }
434         }
435
436         spin_lock_irqsave(&sdata->local->key_lock, flags);
437
438         if (sta)
439                 old_key = sta->key;
440         else
441                 old_key = sdata->keys[idx];
442
443         __ieee80211_key_replace(sdata, sta, old_key, key);
444
445         /* free old key later */
446         add_todo(old_key, KEY_FLAG_TODO_DELETE);
447
448         add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
449         if (ieee80211_sdata_running(sdata))
450                 add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
451
452         spin_unlock_irqrestore(&sdata->local->key_lock, flags);
453 }
454
455 static void __ieee80211_key_free(struct ieee80211_key *key)
456 {
457         /*
458          * Replace key with nothingness if it was ever used.
459          */
460         if (key->sdata)
461                 __ieee80211_key_replace(key->sdata, key->sta,
462                                         key, NULL);
463
464         add_todo(key, KEY_FLAG_TODO_DELETE);
465 }
466
467 void ieee80211_key_free(struct ieee80211_key *key)
468 {
469         unsigned long flags;
470
471         if (!key)
472                 return;
473
474         if (!key->sdata) {
475                 /* The key has not been linked yet, simply free it
476                  * and don't Oops */
477                 if (key->conf.alg == ALG_CCMP)
478                         ieee80211_aes_key_free(key->u.ccmp.tfm);
479                 kfree(key);
480                 return;
481         }
482
483         spin_lock_irqsave(&key->sdata->local->key_lock, flags);
484         __ieee80211_key_free(key);
485         spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
486 }
487
488 /*
489  * To be safe against concurrent manipulations of the list (which shouldn't
490  * actually happen) we need to hold the spinlock. But under the spinlock we
491  * can't actually do much, so we defer processing to the todo list. Then run
492  * the todo list to be sure the operation and possibly previously pending
493  * operations are completed.
494  */
495 static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
496                                         u32 todo_flags)
497 {
498         struct ieee80211_key *key;
499         unsigned long flags;
500
501         might_sleep();
502
503         spin_lock_irqsave(&sdata->local->key_lock, flags);
504         list_for_each_entry(key, &sdata->key_list, list)
505                 add_todo(key, todo_flags);
506         spin_unlock_irqrestore(&sdata->local->key_lock, flags);
507
508         ieee80211_key_todo();
509 }
510
511 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
512 {
513         ASSERT_RTNL();
514
515         if (WARN_ON(!ieee80211_sdata_running(sdata)))
516                 return;
517
518         ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
519 }
520
521 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
522 {
523         ASSERT_RTNL();
524
525         ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
526 }
527
528 static void __ieee80211_key_destroy(struct ieee80211_key *key)
529 {
530         if (!key)
531                 return;
532
533         ieee80211_key_disable_hw_accel(key);
534
535         if (key->conf.alg == ALG_CCMP)
536                 ieee80211_aes_key_free(key->u.ccmp.tfm);
537         if (key->conf.alg == ALG_AES_CMAC)
538                 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
539         ieee80211_debugfs_key_remove(key);
540
541         kfree(key);
542 }
543
544 static void __ieee80211_key_todo(void)
545 {
546         struct ieee80211_key *key;
547         bool work_done;
548         u32 todoflags;
549
550         /*
551          * NB: sta_info_destroy relies on this!
552          */
553         synchronize_rcu();
554
555         spin_lock_bh(&todo_lock);
556         while (!list_empty(&todo_list)) {
557                 key = list_first_entry(&todo_list, struct ieee80211_key, todo);
558                 list_del_init(&key->todo);
559                 todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
560                                           KEY_FLAG_TODO_DEFKEY |
561                                           KEY_FLAG_TODO_DEFMGMTKEY |
562                                           KEY_FLAG_TODO_HWACCEL_ADD |
563                                           KEY_FLAG_TODO_HWACCEL_REMOVE |
564                                           KEY_FLAG_TODO_DELETE);
565                 key->flags &= ~todoflags;
566                 spin_unlock_bh(&todo_lock);
567
568                 work_done = false;
569
570                 if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
571                         ieee80211_debugfs_key_add(key);
572                         work_done = true;
573                 }
574                 if (todoflags & KEY_FLAG_TODO_DEFKEY) {
575                         ieee80211_debugfs_key_remove_default(key->sdata);
576                         ieee80211_debugfs_key_add_default(key->sdata);
577                         work_done = true;
578                 }
579                 if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) {
580                         ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
581                         ieee80211_debugfs_key_add_mgmt_default(key->sdata);
582                         work_done = true;
583                 }
584                 if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
585                         ieee80211_key_enable_hw_accel(key);
586                         work_done = true;
587                 }
588                 if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
589                         ieee80211_key_disable_hw_accel(key);
590                         work_done = true;
591                 }
592                 if (todoflags & KEY_FLAG_TODO_DELETE) {
593                         __ieee80211_key_destroy(key);
594                         work_done = true;
595                 }
596
597                 WARN_ON(!work_done);
598
599                 spin_lock_bh(&todo_lock);
600         }
601         spin_unlock_bh(&todo_lock);
602 }
603
604 void ieee80211_key_todo(void)
605 {
606         ieee80211_key_lock();
607         __ieee80211_key_todo();
608         ieee80211_key_unlock();
609 }
610
611 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
612 {
613         struct ieee80211_key *key, *tmp;
614         unsigned long flags;
615
616         ieee80211_key_lock();
617
618         ieee80211_debugfs_key_remove_default(sdata);
619         ieee80211_debugfs_key_remove_mgmt_default(sdata);
620
621         spin_lock_irqsave(&sdata->local->key_lock, flags);
622         list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
623                 __ieee80211_key_free(key);
624         spin_unlock_irqrestore(&sdata->local->key_lock, flags);
625
626         __ieee80211_key_todo();
627
628         ieee80211_key_unlock();
629 }