blob: 19e77f626d848a0d532234e6733fbda092b8ddcb [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 Berg11a843b2007-08-28 17:01:55 -04005 * Copyright 2007 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 Berg1f5a7e42007-07-27 15:43:23 +020016#include <net/mac80211.h>
17#include "ieee80211_i.h"
18#include "debugfs_key.h"
19#include "aes_ccm.h"
20
Johannes Berg11a843b2007-08-28 17:01:55 -040021
22/*
23 * Key handling basics
24 *
25 * Key handling in mac80211 is done based on per-interface (sub_if_data)
26 * keys and per-station keys. Since each station belongs to an interface,
27 * each station key also belongs to that interface.
28 *
29 * Hardware acceleration is done on a best-effort basis, for each key
30 * that is eligible the hardware is asked to enable that key but if
31 * it cannot do that they key is simply kept for software encryption.
32 * There is currently no way of knowing this except by looking into
33 * debugfs.
34 *
35 * All operations here are called under RTNL so no extra locking is
36 * required.
37 */
38
39static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
40static const u8 zero_addr[ETH_ALEN];
41
42static const u8 *get_mac_for_key(struct ieee80211_key *key)
43{
44 const u8 *addr = bcast_addr;
45
46 /*
47 * If we're an AP we won't ever receive frames with a non-WEP
48 * group key so we tell the driver that by using the zero MAC
49 * address to indicate a transmit-only key.
50 */
51 if (key->conf.alg != ALG_WEP &&
52 (key->sdata->type == IEEE80211_IF_TYPE_AP ||
53 key->sdata->type == IEEE80211_IF_TYPE_VLAN))
54 addr = zero_addr;
55
56 if (key->sta)
57 addr = key->sta->addr;
58
59 return addr;
60}
61
62static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
63{
64 const u8 *addr;
65 int ret;
66
67 if (!key->local->ops->set_key)
68 return;
69
70 addr = get_mac_for_key(key);
71
72 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
73 key->sdata->dev->dev_addr, addr,
74 &key->conf);
75
76 WARN_ON(!ret && (key->conf.hw_key_idx == HW_KEY_IDX_INVALID));
77
78 if (!ret)
79 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
80
81 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
82 printk(KERN_ERR "mac80211-%s: failed to set key "
83 "(%d, " MAC_FMT ") to hardware (%d)\n",
84 wiphy_name(key->local->hw.wiphy),
85 key->conf.keyidx, MAC_ARG(addr), ret);
86}
87
88static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
89{
90 const u8 *addr;
91 int ret;
92
93 if (!key->local->ops->set_key)
94 return;
95
96 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
97 return;
98
99 addr = get_mac_for_key(key);
100
101 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
102 key->sdata->dev->dev_addr, addr,
103 &key->conf);
104
105 if (ret)
106 printk(KERN_ERR "mac80211-%s: failed to remove key "
107 "(%d, " MAC_FMT ") from hardware (%d)\n",
108 wiphy_name(key->local->hw.wiphy),
109 key->conf.keyidx, MAC_ARG(addr), ret);
110
111 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
112 key->conf.hw_key_idx = HW_KEY_IDX_INVALID;
113}
114
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200115struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg11a843b2007-08-28 17:01:55 -0400116 struct sta_info *sta,
117 ieee80211_key_alg alg,
118 int idx,
119 size_t key_len,
120 const u8 *key_data)
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200121{
122 struct ieee80211_key *key;
123
Johannes Bergd4e46a32007-09-14 11:10:24 -0400124 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
Johannes Berg11a843b2007-08-28 17:01:55 -0400125 BUG_ON(alg == ALG_NONE);
126
127 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200128 if (!key)
129 return NULL;
Johannes Berg11a843b2007-08-28 17:01:55 -0400130
131 /*
132 * Default to software encryption; we'll later upload the
133 * key to the hardware if possible.
134 */
135 key->conf.hw_key_idx = HW_KEY_IDX_INVALID;
136 key->conf.flags = 0;
137 key->flags = 0;
138
139 key->conf.alg = alg;
140 key->conf.keyidx = idx;
141 key->conf.keylen = key_len;
142 memcpy(key->conf.key, key_data, key_len);
143
144 key->local = sdata->local;
145 key->sdata = sdata;
146 key->sta = sta;
147
148 if (alg == ALG_CCMP) {
149 /*
150 * Initialize AES key state here as an optimization so that
151 * it does not need to be initialized for every packet.
152 */
153 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
154 if (!key->u.ccmp.tfm) {
155 ieee80211_key_free(key);
156 return NULL;
157 }
158 }
159
160 ieee80211_debugfs_key_add(key->local, key);
161
Johannes Bergd4e46a32007-09-14 11:10:24 -0400162 /* remove key first */
163 if (sta)
164 ieee80211_key_free(sta->key);
165 else
166 ieee80211_key_free(sdata->keys[idx]);
167
Johannes Berg11a843b2007-08-28 17:01:55 -0400168 if (sta) {
169 ieee80211_debugfs_key_sta_link(key, sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -0400170
Johannes Berg11a843b2007-08-28 17:01:55 -0400171 /*
172 * some hardware cannot handle TKIP with QoS, so
173 * we indicate whether QoS could be in use.
174 */
175 if (sta->flags & WLAN_STA_WME)
176 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
177 } else {
178 if (sdata->type == IEEE80211_IF_TYPE_STA) {
179 struct sta_info *ap;
180
181 /* same here, the AP could be using QoS */
182 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
183 if (ap) {
184 if (ap->flags & WLAN_STA_WME)
185 key->conf.flags |=
186 IEEE80211_KEY_FLAG_WMM_STA;
187 sta_info_put(ap);
188 }
189 }
Johannes Berg11a843b2007-08-28 17:01:55 -0400190 }
191
Johannes Bergd4e46a32007-09-14 11:10:24 -0400192 /* enable hwaccel if appropriate */
Johannes Berg11a843b2007-08-28 17:01:55 -0400193 if (netif_running(key->sdata->dev))
194 ieee80211_key_enable_hw_accel(key);
195
Johannes Bergd4e46a32007-09-14 11:10:24 -0400196 if (sta)
197 rcu_assign_pointer(sta->key, key);
198 else
199 rcu_assign_pointer(sdata->keys[idx], key);
200
201 list_add(&key->list, &sdata->key_list);
202
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200203 return key;
204}
205
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200206void ieee80211_key_free(struct ieee80211_key *key)
207{
Johannes Berg8f371712007-08-28 17:01:54 -0400208 if (!key)
209 return;
210
Johannes Berg11a843b2007-08-28 17:01:55 -0400211 if (key->sta) {
Johannes Bergd4e46a32007-09-14 11:10:24 -0400212 rcu_assign_pointer(key->sta->key, NULL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400213 } else {
214 if (key->sdata->default_key == key)
215 ieee80211_set_default_key(key->sdata, -1);
216 if (key->conf.keyidx >= 0 &&
217 key->conf.keyidx < NUM_DEFAULT_KEYS)
Johannes Bergd4e46a32007-09-14 11:10:24 -0400218 rcu_assign_pointer(key->sdata->keys[key->conf.keyidx],
219 NULL);
Johannes Berg11a843b2007-08-28 17:01:55 -0400220 else
221 WARN_ON(1);
222 }
223
Johannes Bergd4e46a32007-09-14 11:10:24 -0400224 /* wait for all key users to complete */
225 synchronize_rcu();
226
227 /* remove from hwaccel if appropriate */
228 ieee80211_key_disable_hw_accel(key);
229
Johannes Berg8f371712007-08-28 17:01:54 -0400230 if (key->conf.alg == ALG_CCMP)
231 ieee80211_aes_key_free(key->u.ccmp.tfm);
232 ieee80211_debugfs_key_remove(key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400233
234 list_del(&key->list);
235
Johannes Berg8f371712007-08-28 17:01:54 -0400236 kfree(key);
Johannes Berg1f5a7e42007-07-27 15:43:23 +0200237}
Johannes Berg11a843b2007-08-28 17:01:55 -0400238
239void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
240{
241 struct ieee80211_key *key = NULL;
242
243 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
244 key = sdata->keys[idx];
245
246 if (sdata->default_key != key) {
247 ieee80211_debugfs_key_remove_default(sdata);
248
Johannes Bergd4e46a32007-09-14 11:10:24 -0400249 rcu_assign_pointer(sdata->default_key, key);
Johannes Berg11a843b2007-08-28 17:01:55 -0400250
251 if (sdata->default_key)
252 ieee80211_debugfs_key_add_default(sdata);
253
254 if (sdata->local->ops->set_key_idx)
255 sdata->local->ops->set_key_idx(
256 local_to_hw(sdata->local), idx);
257 }
258}
259
260void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
261{
262 struct ieee80211_key *key, *tmp;
263
264 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
265 ieee80211_key_free(key);
266}
267
268void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
269{
270 struct ieee80211_key *key;
271
272 WARN_ON(!netif_running(sdata->dev));
273 if (!netif_running(sdata->dev))
274 return;
275
276 list_for_each_entry(key, &sdata->key_list, list)
277 ieee80211_key_enable_hw_accel(key);
278}
279
280void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
281{
282 struct ieee80211_key *key;
283
284 list_for_each_entry(key, &sdata->key_list, list)
285 ieee80211_key_disable_hw_accel(key);
286}