mac80211: split ieee80211_txrx_data
[deliverable/linux.git] / net / mac80211 / key.c
CommitLineData
1f5a7e47
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
11a843b7 5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
1f5a7e47
JB
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
11a843b7
JB
12#include <linux/if_ether.h>
13#include <linux/etherdevice.h>
14#include <linux/list.h>
d4e46a3d 15#include <linux/rcupdate.h>
db4d1169 16#include <linux/rtnetlink.h>
1f5a7e47
JB
17#include <net/mac80211.h>
18#include "ieee80211_i.h"
19#include "debugfs_key.h"
20#include "aes_ccm.h"
21
11a843b7
JB
22
23/*
24 * Key handling basics
25 *
26 * Key handling in mac80211 is done based on per-interface (sub_if_data)
27 * keys and per-station keys. Since each station belongs to an interface,
28 * each station key also belongs to that interface.
29 *
30 * Hardware acceleration is done on a best-effort basis, for each key
31 * that is eligible the hardware is asked to enable that key but if
32 * it cannot do that they key is simply kept for software encryption.
33 * There is currently no way of knowing this except by looking into
34 * debugfs.
35 *
36 * All operations here are called under RTNL so no extra locking is
37 * required.
db4d1169
JB
38 *
39 * NOTE: This code requires that sta info *destruction* is done under
40 * RTNL, otherwise it can try to access already freed STA structs
41 * when a STA key is being freed.
11a843b7
JB
42 */
43
44static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
45static const u8 zero_addr[ETH_ALEN];
46
47static const u8 *get_mac_for_key(struct ieee80211_key *key)
48{
49 const u8 *addr = bcast_addr;
50
51 /*
52 * If we're an AP we won't ever receive frames with a non-WEP
53 * group key so we tell the driver that by using the zero MAC
54 * address to indicate a transmit-only key.
55 */
56 if (key->conf.alg != ALG_WEP &&
51fb61e7
JB
57 (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
58 key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
11a843b7
JB
59 addr = zero_addr;
60
61 if (key->sta)
62 addr = key->sta->addr;
63
64 return addr;
65}
66
67static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
68{
69 const u8 *addr;
70 int ret;
0795af57 71 DECLARE_MAC_BUF(mac);
11a843b7
JB
72
73 if (!key->local->ops->set_key)
74 return;
75
76 addr = get_mac_for_key(key);
77
78 ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
79 key->sdata->dev->dev_addr, addr,
80 &key->conf);
81
11a843b7
JB
82 if (!ret)
83 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
84
85 if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
86 printk(KERN_ERR "mac80211-%s: failed to set key "
0795af57 87 "(%d, %s) to hardware (%d)\n",
11a843b7 88 wiphy_name(key->local->hw.wiphy),
0795af57 89 key->conf.keyidx, print_mac(mac, addr), ret);
11a843b7
JB
90}
91
db4d1169
JB
92static void ieee80211_key_mark_hw_accel_off(struct ieee80211_key *key)
93{
94 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
95 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
96 key->flags |= KEY_FLAG_REMOVE_FROM_HARDWARE;
97 }
98}
99
11a843b7
JB
100static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
101{
102 const u8 *addr;
103 int ret;
0795af57 104 DECLARE_MAC_BUF(mac);
11a843b7 105
db4d1169 106 if (!key || !key->local->ops->set_key)
11a843b7
JB
107 return;
108
db4d1169
JB
109 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
110 !(key->flags & KEY_FLAG_REMOVE_FROM_HARDWARE))
11a843b7
JB
111 return;
112
113 addr = get_mac_for_key(key);
114
115 ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
116 key->sdata->dev->dev_addr, addr,
117 &key->conf);
118
119 if (ret)
120 printk(KERN_ERR "mac80211-%s: failed to remove key "
0795af57 121 "(%d, %s) from hardware (%d)\n",
11a843b7 122 wiphy_name(key->local->hw.wiphy),
0795af57 123 key->conf.keyidx, print_mac(mac, addr), ret);
11a843b7 124
db4d1169
JB
125 key->flags &= ~(KEY_FLAG_UPLOADED_TO_HARDWARE |
126 KEY_FLAG_REMOVE_FROM_HARDWARE);
11a843b7
JB
127}
128
db4d1169 129struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
11a843b7
JB
130 int idx,
131 size_t key_len,
132 const u8 *key_data)
1f5a7e47
JB
133{
134 struct ieee80211_key *key;
135
d4e46a3d 136 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
11a843b7
JB
137
138 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
1f5a7e47
JB
139 if (!key)
140 return NULL;
11a843b7
JB
141
142 /*
143 * Default to software encryption; we'll later upload the
144 * key to the hardware if possible.
145 */
11a843b7
JB
146 key->conf.flags = 0;
147 key->flags = 0;
148
149 key->conf.alg = alg;
150 key->conf.keyidx = idx;
151 key->conf.keylen = key_len;
152 memcpy(key->conf.key, key_data, key_len);
e4861829 153 INIT_LIST_HEAD(&key->list);
11a843b7 154
11a843b7
JB
155 if (alg == ALG_CCMP) {
156 /*
157 * Initialize AES key state here as an optimization so that
158 * it does not need to be initialized for every packet.
159 */
160 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
161 if (!key->u.ccmp.tfm) {
162 ieee80211_key_free(key);
163 return NULL;
164 }
165 }
166
db4d1169
JB
167 return key;
168}
11a843b7 169
db4d1169
JB
170static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
171 struct sta_info *sta,
172 struct ieee80211_key *key,
173 struct ieee80211_key *new)
174{
175 int idx, defkey;
176
177 if (sta) {
178 rcu_assign_pointer(sta->key, new);
179 } else {
180 WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx);
181
182 if (key)
183 idx = key->conf.keyidx;
184 else
185 idx = new->conf.keyidx;
186
187 defkey = key && sdata->default_key == key;
188
189 if (defkey && !new)
190 ieee80211_set_default_key(sdata, -1);
191
192 rcu_assign_pointer(sdata->keys[idx], new);
e4861829
JB
193 if (new)
194 list_add(&new->list, &sdata->key_list);
db4d1169
JB
195
196 if (defkey && new)
197 ieee80211_set_default_key(sdata, new->conf.keyidx);
198 }
199
200 if (key) {
201 ieee80211_key_mark_hw_accel_off(key);
e4861829
JB
202 /*
203 * We'll use an empty list to indicate that the key
204 * has already been removed.
205 */
206 list_del_init(&key->list);
db4d1169
JB
207 }
208}
209
210void ieee80211_key_link(struct ieee80211_key *key,
211 struct ieee80211_sub_if_data *sdata,
212 struct sta_info *sta)
213{
214 struct ieee80211_key *old_key;
215 int idx;
216
217 ASSERT_RTNL();
218 might_sleep();
219
220 BUG_ON(!sdata);
221 BUG_ON(!key);
222
223 idx = key->conf.keyidx;
224 key->local = sdata->local;
225 key->sdata = sdata;
226 key->sta = sta;
227
228 ieee80211_debugfs_key_add(key->local, key);
d4e46a3d 229
11a843b7
JB
230 if (sta) {
231 ieee80211_debugfs_key_sta_link(key, sta);
d4e46a3d 232
11a843b7
JB
233 /*
234 * some hardware cannot handle TKIP with QoS, so
235 * we indicate whether QoS could be in use.
236 */
237 if (sta->flags & WLAN_STA_WME)
238 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
239 } else {
51fb61e7 240 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
11a843b7
JB
241 struct sta_info *ap;
242
243 /* same here, the AP could be using QoS */
244 ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
245 if (ap) {
246 if (ap->flags & WLAN_STA_WME)
247 key->conf.flags |=
248 IEEE80211_KEY_FLAG_WMM_STA;
249 sta_info_put(ap);
250 }
251 }
11a843b7
JB
252 }
253
d4e46a3d 254 if (sta)
db4d1169 255 old_key = sta->key;
d4e46a3d 256 else
db4d1169
JB
257 old_key = sdata->keys[idx];
258
259 __ieee80211_key_replace(sdata, sta, old_key, key);
d4e46a3d 260
e4861829
JB
261 if (old_key) {
262 synchronize_rcu();
263 ieee80211_key_free(old_key);
264 }
db4d1169 265
e4861829
JB
266 if (netif_running(sdata->dev))
267 ieee80211_key_enable_hw_accel(key);
1f5a7e47
JB
268}
269
8f37171a 270void ieee80211_key_free(struct ieee80211_key *key)
1f5a7e47 271{
db4d1169
JB
272 ASSERT_RTNL();
273 might_sleep();
274
8f37171a
JB
275 if (!key)
276 return;
1f5a7e47 277
db4d1169
JB
278 if (key->sdata) {
279 /*
280 * Replace key with nothingness.
281 *
282 * Because other code may have key reference (RCU protected)
283 * right now, we then wait for a grace period before freeing
284 * it.
e4861829
JB
285 * An empty list indicates it was never added to the key list
286 * or has been removed already. It may, however, still be in
287 * hardware for acceleration.
db4d1169 288 */
e4861829
JB
289 if (!list_empty(&key->list))
290 __ieee80211_key_replace(key->sdata, key->sta,
291 key, NULL);
11a843b7 292
db4d1169 293 synchronize_rcu();
d4e46a3d 294
db4d1169
JB
295 /*
296 * Remove from hwaccel if appropriate, this will
297 * only happen when the key is actually unlinked,
298 * it will already be done when the key was replaced.
299 */
300 ieee80211_key_disable_hw_accel(key);
301 }
d4e46a3d 302
8f20fc24 303 if (key->conf.alg == ALG_CCMP)
1f5a7e47
JB
304 ieee80211_aes_key_free(key->u.ccmp.tfm);
305 ieee80211_debugfs_key_remove(key);
11a843b7 306
1f5a7e47
JB
307 kfree(key);
308}
11a843b7
JB
309
310void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
311{
312 struct ieee80211_key *key = NULL;
313
314 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
315 key = sdata->keys[idx];
316
317 if (sdata->default_key != key) {
318 ieee80211_debugfs_key_remove_default(sdata);
319
d4e46a3d 320 rcu_assign_pointer(sdata->default_key, key);
11a843b7
JB
321
322 if (sdata->default_key)
323 ieee80211_debugfs_key_add_default(sdata);
11a843b7
JB
324 }
325}
326
327void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
328{
329 struct ieee80211_key *key, *tmp;
db4d1169
JB
330 LIST_HEAD(tmp_list);
331
332 ASSERT_RTNL();
333 might_sleep();
11a843b7
JB
334
335 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
336 ieee80211_key_free(key);
337}
338
339void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
340{
341 struct ieee80211_key *key;
342
db4d1169
JB
343 ASSERT_RTNL();
344 might_sleep();
345
346 if (WARN_ON(!netif_running(sdata->dev)))
11a843b7
JB
347 return;
348
349 list_for_each_entry(key, &sdata->key_list, list)
350 ieee80211_key_enable_hw_accel(key);
351}
352
353void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
354{
355 struct ieee80211_key *key;
356
db4d1169
JB
357 ASSERT_RTNL();
358 might_sleep();
359
11a843b7
JB
360 list_for_each_entry(key, &sdata->key_list, list)
361 ieee80211_key_disable_hw_accel(key);
362}
This page took 0.123493 seconds and 5 git commands to generate.