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