Merge branch 'wl12xx-next' of git://git.kernel.org/pub/scm/linux/kernel/git/luca...
[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.
40  *
41  * 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 remove the key
45  * which waits for an RCU grace period.
46  */
47
48 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
49
50 static void assert_key_lock(struct ieee80211_local *local)
51 {
52         lockdep_assert_held(&local->key_mtx);
53 }
54
55 static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
56 {
57         if (key->sta)
58                 return &key->sta->sta;
59
60         return NULL;
61 }
62
63 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
64 {
65         struct ieee80211_sub_if_data *sdata;
66         struct ieee80211_sta *sta;
67         int ret;
68
69         might_sleep();
70
71         if (!key->local->ops->set_key)
72                 goto out_unsupported;
73
74         assert_key_lock(key->local);
75
76         sta = get_sta_for_key(key);
77
78         /*
79          * If this is a per-STA GTK, check if it
80          * is supported; if not, return.
81          */
82         if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
83             !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
84                 goto out_unsupported;
85
86         sdata = key->sdata;
87         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
88                 /*
89                  * The driver doesn't know anything about VLAN interfaces.
90                  * Hence, don't send GTKs for VLAN interfaces to the driver.
91                  */
92                 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
93                         goto out_unsupported;
94                 sdata = container_of(sdata->bss,
95                                      struct ieee80211_sub_if_data,
96                                      u.ap);
97         }
98
99         ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
100
101         if (!ret) {
102                 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
103                 return 0;
104         }
105
106         if (ret != -ENOSPC && ret != -EOPNOTSUPP)
107                 wiphy_err(key->local->hw.wiphy,
108                           "failed to set key (%d, %pM) to hardware (%d)\n",
109                           key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
110
111  out_unsupported:
112         switch (key->conf.cipher) {
113         case WLAN_CIPHER_SUITE_WEP40:
114         case WLAN_CIPHER_SUITE_WEP104:
115         case WLAN_CIPHER_SUITE_TKIP:
116         case WLAN_CIPHER_SUITE_CCMP:
117         case WLAN_CIPHER_SUITE_AES_CMAC:
118                 /* all of these we can do in software */
119                 return 0;
120         default:
121                 return -EINVAL;
122         }
123 }
124
125 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
126 {
127         struct ieee80211_sub_if_data *sdata;
128         struct ieee80211_sta *sta;
129         int ret;
130
131         might_sleep();
132
133         if (!key || !key->local->ops->set_key)
134                 return;
135
136         assert_key_lock(key->local);
137
138         if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
139                 return;
140
141         sta = get_sta_for_key(key);
142         sdata = key->sdata;
143
144         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
145                 sdata = container_of(sdata->bss,
146                                      struct ieee80211_sub_if_data,
147                                      u.ap);
148
149         ret = drv_set_key(key->local, DISABLE_KEY, sdata,
150                           sta, &key->conf);
151
152         if (ret)
153                 wiphy_err(key->local->hw.wiphy,
154                           "failed to remove key (%d, %pM) from hardware (%d)\n",
155                           key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
156
157         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
158 }
159
160 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
161 {
162         struct ieee80211_key *key;
163
164         key = container_of(key_conf, struct ieee80211_key, conf);
165
166         might_sleep();
167         assert_key_lock(key->local);
168
169         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
170
171         /*
172          * Flush TX path to avoid attempts to use this key
173          * after this function returns. Until then, drivers
174          * must be prepared to handle the key.
175          */
176         synchronize_rcu();
177 }
178 EXPORT_SYMBOL_GPL(ieee80211_key_removed);
179
180 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
181                                         int idx, bool uni, bool multi)
182 {
183         struct ieee80211_key *key = NULL;
184
185         assert_key_lock(sdata->local);
186
187         if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
188                 key = sdata->keys[idx];
189
190         if (uni)
191                 rcu_assign_pointer(sdata->default_unicast_key, key);
192         if (multi)
193                 rcu_assign_pointer(sdata->default_multicast_key, key);
194
195         ieee80211_debugfs_key_update_default(sdata);
196 }
197
198 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
199                                bool uni, bool multi)
200 {
201         mutex_lock(&sdata->local->key_mtx);
202         __ieee80211_set_default_key(sdata, idx, uni, multi);
203         mutex_unlock(&sdata->local->key_mtx);
204 }
205
206 static void
207 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
208 {
209         struct ieee80211_key *key = NULL;
210
211         assert_key_lock(sdata->local);
212
213         if (idx >= NUM_DEFAULT_KEYS &&
214             idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
215                 key = sdata->keys[idx];
216
217         rcu_assign_pointer(sdata->default_mgmt_key, key);
218
219         ieee80211_debugfs_key_update_default(sdata);
220 }
221
222 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
223                                     int idx)
224 {
225         mutex_lock(&sdata->local->key_mtx);
226         __ieee80211_set_default_mgmt_key(sdata, idx);
227         mutex_unlock(&sdata->local->key_mtx);
228 }
229
230
231 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
232                                     struct sta_info *sta,
233                                     bool pairwise,
234                                     struct ieee80211_key *old,
235                                     struct ieee80211_key *new)
236 {
237         int idx;
238         bool defunikey, defmultikey, defmgmtkey;
239
240         if (new)
241                 list_add(&new->list, &sdata->key_list);
242
243         if (sta && pairwise) {
244                 rcu_assign_pointer(sta->ptk, new);
245         } else if (sta) {
246                 if (old)
247                         idx = old->conf.keyidx;
248                 else
249                         idx = new->conf.keyidx;
250                 rcu_assign_pointer(sta->gtk[idx], new);
251         } else {
252                 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
253
254                 if (old)
255                         idx = old->conf.keyidx;
256                 else
257                         idx = new->conf.keyidx;
258
259                 defunikey = old && sdata->default_unicast_key == old;
260                 defmultikey = old && sdata->default_multicast_key == old;
261                 defmgmtkey = old && sdata->default_mgmt_key == old;
262
263                 if (defunikey && !new)
264                         __ieee80211_set_default_key(sdata, -1, true, false);
265                 if (defmultikey && !new)
266                         __ieee80211_set_default_key(sdata, -1, false, true);
267                 if (defmgmtkey && !new)
268                         __ieee80211_set_default_mgmt_key(sdata, -1);
269
270                 rcu_assign_pointer(sdata->keys[idx], new);
271                 if (defunikey && new)
272                         __ieee80211_set_default_key(sdata, new->conf.keyidx,
273                                                     true, false);
274                 if (defmultikey && new)
275                         __ieee80211_set_default_key(sdata, new->conf.keyidx,
276                                                     false, true);
277                 if (defmgmtkey && new)
278                         __ieee80211_set_default_mgmt_key(sdata,
279                                                          new->conf.keyidx);
280         }
281
282         if (old) {
283                 /*
284                  * We'll use an empty list to indicate that the key
285                  * has already been removed.
286                  */
287                 list_del_init(&old->list);
288         }
289 }
290
291 struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
292                                           const u8 *key_data,
293                                           size_t seq_len, const u8 *seq)
294 {
295         struct ieee80211_key *key;
296         int i, j, err;
297
298         BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
299
300         key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
301         if (!key)
302                 return ERR_PTR(-ENOMEM);
303
304         /*
305          * Default to software encryption; we'll later upload the
306          * key to the hardware if possible.
307          */
308         key->conf.flags = 0;
309         key->flags = 0;
310
311         key->conf.cipher = cipher;
312         key->conf.keyidx = idx;
313         key->conf.keylen = key_len;
314         switch (cipher) {
315         case WLAN_CIPHER_SUITE_WEP40:
316         case WLAN_CIPHER_SUITE_WEP104:
317                 key->conf.iv_len = WEP_IV_LEN;
318                 key->conf.icv_len = WEP_ICV_LEN;
319                 break;
320         case WLAN_CIPHER_SUITE_TKIP:
321                 key->conf.iv_len = TKIP_IV_LEN;
322                 key->conf.icv_len = TKIP_ICV_LEN;
323                 if (seq) {
324                         for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
325                                 key->u.tkip.rx[i].iv32 =
326                                         get_unaligned_le32(&seq[2]);
327                                 key->u.tkip.rx[i].iv16 =
328                                         get_unaligned_le16(seq);
329                         }
330                 }
331                 break;
332         case WLAN_CIPHER_SUITE_CCMP:
333                 key->conf.iv_len = CCMP_HDR_LEN;
334                 key->conf.icv_len = CCMP_MIC_LEN;
335                 if (seq) {
336                         for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
337                                 for (j = 0; j < CCMP_PN_LEN; j++)
338                                         key->u.ccmp.rx_pn[i][j] =
339                                                 seq[CCMP_PN_LEN - j - 1];
340                 }
341                 /*
342                  * Initialize AES key state here as an optimization so that
343                  * it does not need to be initialized for every packet.
344                  */
345                 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
346                 if (IS_ERR(key->u.ccmp.tfm)) {
347                         err = PTR_ERR(key->u.ccmp.tfm);
348                         kfree(key);
349                         key = ERR_PTR(err);
350                 }
351                 break;
352         case WLAN_CIPHER_SUITE_AES_CMAC:
353                 key->conf.iv_len = 0;
354                 key->conf.icv_len = sizeof(struct ieee80211_mmie);
355                 if (seq)
356                         for (j = 0; j < 6; j++)
357                                 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
358                 /*
359                  * Initialize AES key state here as an optimization so that
360                  * it does not need to be initialized for every packet.
361                  */
362                 key->u.aes_cmac.tfm =
363                         ieee80211_aes_cmac_key_setup(key_data);
364                 if (IS_ERR(key->u.aes_cmac.tfm)) {
365                         err = PTR_ERR(key->u.aes_cmac.tfm);
366                         kfree(key);
367                         key = ERR_PTR(err);
368                 }
369                 break;
370         }
371         memcpy(key->conf.key, key_data, key_len);
372         INIT_LIST_HEAD(&key->list);
373
374         return key;
375 }
376
377 static void __ieee80211_key_destroy(struct ieee80211_key *key)
378 {
379         if (!key)
380                 return;
381
382         if (key->local)
383                 ieee80211_key_disable_hw_accel(key);
384
385         if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
386                 ieee80211_aes_key_free(key->u.ccmp.tfm);
387         if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
388                 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
389         if (key->local)
390                 ieee80211_debugfs_key_remove(key);
391
392         kfree(key);
393 }
394
395 int ieee80211_key_link(struct ieee80211_key *key,
396                        struct ieee80211_sub_if_data *sdata,
397                        struct sta_info *sta)
398 {
399         struct ieee80211_key *old_key;
400         int idx, ret;
401         bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
402
403         BUG_ON(!sdata);
404         BUG_ON(!key);
405
406         idx = key->conf.keyidx;
407         key->local = sdata->local;
408         key->sdata = sdata;
409         key->sta = sta;
410
411         if (sta) {
412                 /*
413                  * some hardware cannot handle TKIP with QoS, so
414                  * we indicate whether QoS could be in use.
415                  */
416                 if (test_sta_flags(sta, WLAN_STA_WME))
417                         key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
418         } else {
419                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
420                         struct sta_info *ap;
421
422                         /*
423                          * We're getting a sta pointer in,
424                          * so must be under RCU read lock.
425                          */
426
427                         /* same here, the AP could be using QoS */
428                         ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
429                         if (ap) {
430                                 if (test_sta_flags(ap, WLAN_STA_WME))
431                                         key->conf.flags |=
432                                                 IEEE80211_KEY_FLAG_WMM_STA;
433                         }
434                 }
435         }
436
437         mutex_lock(&sdata->local->key_mtx);
438
439         if (sta && pairwise)
440                 old_key = sta->ptk;
441         else if (sta)
442                 old_key = sta->gtk[idx];
443         else
444                 old_key = sdata->keys[idx];
445
446         __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
447         __ieee80211_key_destroy(old_key);
448
449         ieee80211_debugfs_key_add(key);
450
451         ret = ieee80211_key_enable_hw_accel(key);
452
453         mutex_unlock(&sdata->local->key_mtx);
454
455         return ret;
456 }
457
458 static void __ieee80211_key_free(struct ieee80211_key *key)
459 {
460         /*
461          * Replace key with nothingness if it was ever used.
462          */
463         if (key->sdata)
464                 __ieee80211_key_replace(key->sdata, key->sta,
465                                 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
466                                 key, NULL);
467         __ieee80211_key_destroy(key);
468 }
469
470 void ieee80211_key_free(struct ieee80211_local *local,
471                         struct ieee80211_key *key)
472 {
473         if (!key)
474                 return;
475
476         mutex_lock(&local->key_mtx);
477         __ieee80211_key_free(key);
478         mutex_unlock(&local->key_mtx);
479 }
480
481 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
482 {
483         struct ieee80211_key *key;
484
485         ASSERT_RTNL();
486
487         if (WARN_ON(!ieee80211_sdata_running(sdata)))
488                 return;
489
490         mutex_lock(&sdata->local->key_mtx);
491
492         list_for_each_entry(key, &sdata->key_list, list)
493                 ieee80211_key_enable_hw_accel(key);
494
495         mutex_unlock(&sdata->local->key_mtx);
496 }
497
498 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
499 {
500         struct ieee80211_key *key;
501
502         ASSERT_RTNL();
503
504         mutex_lock(&sdata->local->key_mtx);
505
506         list_for_each_entry(key, &sdata->key_list, list)
507                 ieee80211_key_disable_hw_accel(key);
508
509         mutex_unlock(&sdata->local->key_mtx);
510 }
511
512 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
513 {
514         struct ieee80211_key *key, *tmp;
515
516         mutex_lock(&sdata->local->key_mtx);
517
518         ieee80211_debugfs_key_remove_mgmt_default(sdata);
519
520         list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
521                 __ieee80211_key_free(key);
522
523         ieee80211_debugfs_key_update_default(sdata);
524
525         mutex_unlock(&sdata->local->key_mtx);
526 }