ath9k_htc: Fix packet injection
[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>
3b96766f 5 * Copyright 2007-2008 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>
5a0e3ad6 17#include <linux/slab.h>
1f5a7e47
JB
18#include <net/mac80211.h>
19#include "ieee80211_i.h"
24487981 20#include "driver-ops.h"
1f5a7e47
JB
21#include "debugfs_key.h"
22#include "aes_ccm.h"
3cfcf6ac 23#include "aes_cmac.h"
1f5a7e47 24
11a843b7 25
dbbea671
JB
26/**
27 * DOC: Key handling basics
11a843b7
JB
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 *
ad0e2b5a 39 * All key operations are protected internally.
db4d1169 40 *
3b96766f
JB
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
ad0e2b5a
JB
44 * functions. This means that sta_info_destroy() must remove the key
45 * which waits for an RCU grace period.
11a843b7
JB
46 */
47
48static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
11a843b7 49
ad0e2b5a 50static void assert_key_lock(struct ieee80211_local *local)
3b96766f 51{
46a5ebaf 52 lockdep_assert_held(&local->key_mtx);
3b96766f
JB
53}
54
dc822b5d 55static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
11a843b7 56{
11a843b7 57 if (key->sta)
dc822b5d 58 return &key->sta->sta;
11a843b7 59
dc822b5d 60 return NULL;
11a843b7
JB
61}
62
3ffc2a90 63static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
11a843b7 64{
dc822b5d
JB
65 struct ieee80211_sub_if_data *sdata;
66 struct ieee80211_sta *sta;
11a843b7
JB
67 int ret;
68
3b96766f
JB
69 might_sleep();
70
e31b8213 71 if (!key->local->ops->set_key)
3ffc2a90 72 goto out_unsupported;
11a843b7 73
ad0e2b5a
JB
74 assert_key_lock(key->local);
75
dc822b5d
JB
76 sta = get_sta_for_key(key);
77
e31b8213
JB
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
dc822b5d 86 sdata = key->sdata;
18890d4b
HS
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;
dc822b5d
JB
94 sdata = container_of(sdata->bss,
95 struct ieee80211_sub_if_data,
96 u.ap);
18890d4b 97 }
11a843b7 98
12375ef9 99 ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
11a843b7 100
e31b8213 101 if (!ret) {
11a843b7 102 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
e31b8213
JB
103 return 0;
104 }
11a843b7 105
e31b8213 106 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
0fb9a9ec
JP
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);
3ffc2a90 110
e31b8213
JB
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;
3ffc2a90 122 }
11a843b7
JB
123}
124
125static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
126{
dc822b5d
JB
127 struct ieee80211_sub_if_data *sdata;
128 struct ieee80211_sta *sta;
11a843b7
JB
129 int ret;
130
3b96766f
JB
131 might_sleep();
132
db4d1169 133 if (!key || !key->local->ops->set_key)
11a843b7
JB
134 return;
135
ad0e2b5a
JB
136 assert_key_lock(key->local);
137
138 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
11a843b7
JB
139 return;
140
dc822b5d
JB
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);
11a843b7 148
12375ef9 149 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
24487981 150 sta, &key->conf);
11a843b7
JB
151
152 if (ret)
0fb9a9ec
JP
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);
11a843b7 156
3b96766f 157 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
3b96766f
JB
158}
159
e31b8213
JB
160void 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}
178EXPORT_SYMBOL_GPL(ieee80211_key_removed);
179
3b96766f 180static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
f7e0104c 181 int idx, bool uni, bool multi)
3b96766f
JB
182{
183 struct ieee80211_key *key = NULL;
184
ad0e2b5a
JB
185 assert_key_lock(sdata->local);
186
3b96766f
JB
187 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
188 key = sdata->keys[idx];
189
f7e0104c
JB
190 if (uni)
191 rcu_assign_pointer(sdata->default_unicast_key, key);
192 if (multi)
193 rcu_assign_pointer(sdata->default_multicast_key, key);
3b96766f 194
f7e0104c 195 ieee80211_debugfs_key_update_default(sdata);
3b96766f
JB
196}
197
f7e0104c
JB
198void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
199 bool uni, bool multi)
3b96766f 200{
ad0e2b5a 201 mutex_lock(&sdata->local->key_mtx);
f7e0104c 202 __ieee80211_set_default_key(sdata, idx, uni, multi);
ad0e2b5a 203 mutex_unlock(&sdata->local->key_mtx);
3b96766f
JB
204}
205
3cfcf6ac
JM
206static void
207__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
208{
209 struct ieee80211_key *key = NULL;
210
ad0e2b5a
JB
211 assert_key_lock(sdata->local);
212
3cfcf6ac
JM
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
f7e0104c 219 ieee80211_debugfs_key_update_default(sdata);
3cfcf6ac
JM
220}
221
222void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
223 int idx)
224{
ad0e2b5a 225 mutex_lock(&sdata->local->key_mtx);
3cfcf6ac 226 __ieee80211_set_default_mgmt_key(sdata, idx);
ad0e2b5a 227 mutex_unlock(&sdata->local->key_mtx);
3cfcf6ac
JM
228}
229
3b96766f
JB
230
231static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
232 struct sta_info *sta,
e31b8213 233 bool pairwise,
3b96766f
JB
234 struct ieee80211_key *old,
235 struct ieee80211_key *new)
236{
f7e0104c
JB
237 int idx;
238 bool defunikey, defmultikey, defmgmtkey;
3b96766f
JB
239
240 if (new)
241 list_add(&new->list, &sdata->key_list);
242
e31b8213
JB
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);
3b96766f
JB
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
f7e0104c
JB
259 defunikey = old && sdata->default_unicast_key == old;
260 defmultikey = old && sdata->default_multicast_key == old;
3cfcf6ac 261 defmgmtkey = old && sdata->default_mgmt_key == old;
3b96766f 262
f7e0104c
JB
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);
3cfcf6ac
JM
267 if (defmgmtkey && !new)
268 __ieee80211_set_default_mgmt_key(sdata, -1);
3b96766f
JB
269
270 rcu_assign_pointer(sdata->keys[idx], new);
f7e0104c
JB
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);
3cfcf6ac
JM
277 if (defmgmtkey && new)
278 __ieee80211_set_default_mgmt_key(sdata,
279 new->conf.keyidx);
3b96766f
JB
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 }
11a843b7
JB
289}
290
97359d12 291struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
faa8fdc8
JM
292 const u8 *key_data,
293 size_t seq_len, const u8 *seq)
1f5a7e47
JB
294{
295 struct ieee80211_key *key;
1ac62ba7 296 int i, j, err;
1f5a7e47 297
3cfcf6ac 298 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
11a843b7
JB
299
300 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
1f5a7e47 301 if (!key)
1ac62ba7 302 return ERR_PTR(-ENOMEM);
11a843b7
JB
303
304 /*
305 * Default to software encryption; we'll later upload the
306 * key to the hardware if possible.
307 */
11a843b7
JB
308 key->conf.flags = 0;
309 key->flags = 0;
310
97359d12 311 key->conf.cipher = cipher;
11a843b7
JB
312 key->conf.keyidx = idx;
313 key->conf.keylen = key_len;
97359d12
JB
314 switch (cipher) {
315 case WLAN_CIPHER_SUITE_WEP40:
316 case WLAN_CIPHER_SUITE_WEP104:
76708dee
FF
317 key->conf.iv_len = WEP_IV_LEN;
318 key->conf.icv_len = WEP_ICV_LEN;
319 break;
97359d12 320 case WLAN_CIPHER_SUITE_TKIP:
76708dee
FF
321 key->conf.iv_len = TKIP_IV_LEN;
322 key->conf.icv_len = TKIP_ICV_LEN;
9f26a952 323 if (seq) {
faa8fdc8
JM
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 }
76708dee 331 break;
97359d12 332 case WLAN_CIPHER_SUITE_CCMP:
76708dee
FF
333 key->conf.iv_len = CCMP_HDR_LEN;
334 key->conf.icv_len = CCMP_MIC_LEN;
9f26a952 335 if (seq) {
9190252c 336 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
faa8fdc8
JM
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 }
11a843b7
JB
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);
1ac62ba7
BH
346 if (IS_ERR(key->u.ccmp.tfm)) {
347 err = PTR_ERR(key->u.ccmp.tfm);
3b96766f 348 kfree(key);
1ac62ba7 349 key = ERR_PTR(err);
11a843b7 350 }
60ae0f20
JB
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];
3cfcf6ac
JM
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);
1ac62ba7
BH
364 if (IS_ERR(key->u.aes_cmac.tfm)) {
365 err = PTR_ERR(key->u.aes_cmac.tfm);
3cfcf6ac 366 kfree(key);
1ac62ba7 367 key = ERR_PTR(err);
3cfcf6ac 368 }
60ae0f20 369 break;
3cfcf6ac 370 }
60ae0f20
JB
371 memcpy(key->conf.key, key_data, key_len);
372 INIT_LIST_HEAD(&key->list);
3cfcf6ac 373
db4d1169
JB
374 return key;
375}
11a843b7 376
ad0e2b5a
JB
377static void __ieee80211_key_destroy(struct ieee80211_key *key)
378{
379 if (!key)
380 return;
381
32162a4d
JM
382 if (key->local)
383 ieee80211_key_disable_hw_accel(key);
ad0e2b5a 384
97359d12 385 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
ad0e2b5a 386 ieee80211_aes_key_free(key->u.ccmp.tfm);
97359d12 387 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
ad0e2b5a 388 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
32162a4d
JM
389 if (key->local)
390 ieee80211_debugfs_key_remove(key);
ad0e2b5a
JB
391
392 kfree(key);
393}
394
3ffc2a90
JB
395int ieee80211_key_link(struct ieee80211_key *key,
396 struct ieee80211_sub_if_data *sdata,
397 struct sta_info *sta)
db4d1169
JB
398{
399 struct ieee80211_key *old_key;
3ffc2a90 400 int idx, ret;
e31b8213 401 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
db4d1169 402
db4d1169
JB
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
11a843b7 411 if (sta) {
11a843b7
JB
412 /*
413 * some hardware cannot handle TKIP with QoS, so
414 * we indicate whether QoS could be in use.
415 */
07346f81 416 if (test_sta_flags(sta, WLAN_STA_WME))
11a843b7
JB
417 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
418 } else {
05c914fe 419 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
11a843b7
JB
420 struct sta_info *ap;
421
3b96766f
JB
422 /*
423 * We're getting a sta pointer in,
424 * so must be under RCU read lock.
425 */
d0709a65 426
11a843b7 427 /* same here, the AP could be using QoS */
abe60632 428 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
11a843b7 429 if (ap) {
07346f81 430 if (test_sta_flags(ap, WLAN_STA_WME))
11a843b7
JB
431 key->conf.flags |=
432 IEEE80211_KEY_FLAG_WMM_STA;
11a843b7
JB
433 }
434 }
11a843b7
JB
435 }
436
ad0e2b5a 437 mutex_lock(&sdata->local->key_mtx);
3b96766f 438
e31b8213
JB
439 if (sta && pairwise)
440 old_key = sta->ptk;
441 else if (sta)
442 old_key = sta->gtk[idx];
d4e46a3d 443 else
db4d1169
JB
444 old_key = sdata->keys[idx];
445
e31b8213 446 __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
ad0e2b5a 447 __ieee80211_key_destroy(old_key);
d4e46a3d 448
ad0e2b5a 449 ieee80211_debugfs_key_add(key);
db4d1169 450
3ffc2a90 451 ret = ieee80211_key_enable_hw_accel(key);
523d2f69 452
ad0e2b5a 453 mutex_unlock(&sdata->local->key_mtx);
3ffc2a90
JB
454
455 return ret;
1f5a7e47
JB
456}
457
3a245766 458static void __ieee80211_key_free(struct ieee80211_key *key)
1f5a7e47 459{
3b96766f
JB
460 /*
461 * Replace key with nothingness if it was ever used.
462 */
3a245766 463 if (key->sdata)
3b96766f 464 __ieee80211_key_replace(key->sdata, key->sta,
e31b8213
JB
465 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
466 key, NULL);
ad0e2b5a 467 __ieee80211_key_destroy(key);
3b96766f 468}
d4e46a3d 469
32162a4d
JM
470void ieee80211_key_free(struct ieee80211_local *local,
471 struct ieee80211_key *key)
3b96766f 472{
3a245766 473 if (!key)
3b96766f
JB
474 return;
475
ad0e2b5a 476 mutex_lock(&local->key_mtx);
3a245766 477 __ieee80211_key_free(key);
ad0e2b5a 478 mutex_unlock(&local->key_mtx);
3a245766
JB
479}
480
ad0e2b5a 481void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
3a245766
JB
482{
483 struct ieee80211_key *key;
11a843b7 484
3a245766 485 ASSERT_RTNL();
11a843b7 486
9607e6b6 487 if (WARN_ON(!ieee80211_sdata_running(sdata)))
3a245766 488 return;
11a843b7 489
ad0e2b5a 490 mutex_lock(&sdata->local->key_mtx);
11a843b7 491
ad0e2b5a
JB
492 list_for_each_entry(key, &sdata->key_list, list)
493 ieee80211_key_enable_hw_accel(key);
3b96766f 494
ad0e2b5a 495 mutex_unlock(&sdata->local->key_mtx);
11a843b7
JB
496}
497
ad0e2b5a 498void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
11a843b7
JB
499{
500 struct ieee80211_key *key;
501
ad0e2b5a 502 ASSERT_RTNL();
db4d1169 503
ad0e2b5a 504 mutex_lock(&sdata->local->key_mtx);
11a843b7 505
ad0e2b5a
JB
506 list_for_each_entry(key, &sdata->key_list, list)
507 ieee80211_key_disable_hw_accel(key);
11a843b7 508
ad0e2b5a 509 mutex_unlock(&sdata->local->key_mtx);
3b96766f 510}
11a843b7 511
3b96766f
JB
512void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
513{
514 struct ieee80211_key *key, *tmp;
db4d1169 515
ad0e2b5a 516 mutex_lock(&sdata->local->key_mtx);
3b96766f 517
3cfcf6ac 518 ieee80211_debugfs_key_remove_mgmt_default(sdata);
3b96766f
JB
519
520 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
3a245766 521 __ieee80211_key_free(key);
3b96766f 522
f7e0104c
JB
523 ieee80211_debugfs_key_update_default(sdata);
524
ad0e2b5a 525 mutex_unlock(&sdata->local->key_mtx);
11a843b7 526}
This page took 0.345276 seconds and 5 git commands to generate.