cfg80211/nl80211: separate unicast/multicast default TX keys
[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
JB
180static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
181 int idx)
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
190 rcu_assign_pointer(sdata->default_key, key);
191
ad0e2b5a
JB
192 if (key) {
193 ieee80211_debugfs_key_remove_default(key->sdata);
194 ieee80211_debugfs_key_add_default(key->sdata);
195 }
3b96766f
JB
196}
197
198void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
199{
ad0e2b5a 200 mutex_lock(&sdata->local->key_mtx);
3b96766f 201 __ieee80211_set_default_key(sdata, idx);
ad0e2b5a 202 mutex_unlock(&sdata->local->key_mtx);
3b96766f
JB
203}
204
3cfcf6ac
JM
205static void
206__ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
207{
208 struct ieee80211_key *key = NULL;
209
ad0e2b5a
JB
210 assert_key_lock(sdata->local);
211
3cfcf6ac
JM
212 if (idx >= NUM_DEFAULT_KEYS &&
213 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
214 key = sdata->keys[idx];
215
216 rcu_assign_pointer(sdata->default_mgmt_key, key);
217
ad0e2b5a
JB
218 if (key) {
219 ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
220 ieee80211_debugfs_key_add_mgmt_default(key->sdata);
221 }
3cfcf6ac
JM
222}
223
224void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
225 int idx)
226{
ad0e2b5a 227 mutex_lock(&sdata->local->key_mtx);
3cfcf6ac 228 __ieee80211_set_default_mgmt_key(sdata, idx);
ad0e2b5a 229 mutex_unlock(&sdata->local->key_mtx);
3cfcf6ac
JM
230}
231
3b96766f
JB
232
233static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
234 struct sta_info *sta,
e31b8213 235 bool pairwise,
3b96766f
JB
236 struct ieee80211_key *old,
237 struct ieee80211_key *new)
238{
3cfcf6ac 239 int idx, defkey, defmgmtkey;
3b96766f
JB
240
241 if (new)
242 list_add(&new->list, &sdata->key_list);
243
e31b8213
JB
244 if (sta && pairwise) {
245 rcu_assign_pointer(sta->ptk, new);
246 } else if (sta) {
247 if (old)
248 idx = old->conf.keyidx;
249 else
250 idx = new->conf.keyidx;
251 rcu_assign_pointer(sta->gtk[idx], new);
3b96766f
JB
252 } else {
253 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
254
255 if (old)
256 idx = old->conf.keyidx;
257 else
258 idx = new->conf.keyidx;
259
260 defkey = old && sdata->default_key == old;
3cfcf6ac 261 defmgmtkey = old && sdata->default_mgmt_key == old;
3b96766f
JB
262
263 if (defkey && !new)
264 __ieee80211_set_default_key(sdata, -1);
3cfcf6ac
JM
265 if (defmgmtkey && !new)
266 __ieee80211_set_default_mgmt_key(sdata, -1);
3b96766f
JB
267
268 rcu_assign_pointer(sdata->keys[idx], new);
269 if (defkey && new)
270 __ieee80211_set_default_key(sdata, new->conf.keyidx);
3cfcf6ac
JM
271 if (defmgmtkey && new)
272 __ieee80211_set_default_mgmt_key(sdata,
273 new->conf.keyidx);
3b96766f
JB
274 }
275
276 if (old) {
277 /*
278 * We'll use an empty list to indicate that the key
279 * has already been removed.
280 */
281 list_del_init(&old->list);
282 }
11a843b7
JB
283}
284
97359d12 285struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
faa8fdc8
JM
286 const u8 *key_data,
287 size_t seq_len, const u8 *seq)
1f5a7e47
JB
288{
289 struct ieee80211_key *key;
1ac62ba7 290 int i, j, err;
1f5a7e47 291
3cfcf6ac 292 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
11a843b7
JB
293
294 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
1f5a7e47 295 if (!key)
1ac62ba7 296 return ERR_PTR(-ENOMEM);
11a843b7
JB
297
298 /*
299 * Default to software encryption; we'll later upload the
300 * key to the hardware if possible.
301 */
11a843b7
JB
302 key->conf.flags = 0;
303 key->flags = 0;
304
97359d12 305 key->conf.cipher = cipher;
11a843b7
JB
306 key->conf.keyidx = idx;
307 key->conf.keylen = key_len;
97359d12
JB
308 switch (cipher) {
309 case WLAN_CIPHER_SUITE_WEP40:
310 case WLAN_CIPHER_SUITE_WEP104:
76708dee
FF
311 key->conf.iv_len = WEP_IV_LEN;
312 key->conf.icv_len = WEP_ICV_LEN;
313 break;
97359d12 314 case WLAN_CIPHER_SUITE_TKIP:
76708dee
FF
315 key->conf.iv_len = TKIP_IV_LEN;
316 key->conf.icv_len = TKIP_ICV_LEN;
9f26a952 317 if (seq) {
faa8fdc8
JM
318 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
319 key->u.tkip.rx[i].iv32 =
320 get_unaligned_le32(&seq[2]);
321 key->u.tkip.rx[i].iv16 =
322 get_unaligned_le16(seq);
323 }
324 }
76708dee 325 break;
97359d12 326 case WLAN_CIPHER_SUITE_CCMP:
76708dee
FF
327 key->conf.iv_len = CCMP_HDR_LEN;
328 key->conf.icv_len = CCMP_MIC_LEN;
9f26a952 329 if (seq) {
9190252c 330 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
faa8fdc8
JM
331 for (j = 0; j < CCMP_PN_LEN; j++)
332 key->u.ccmp.rx_pn[i][j] =
333 seq[CCMP_PN_LEN - j - 1];
334 }
11a843b7
JB
335 /*
336 * Initialize AES key state here as an optimization so that
337 * it does not need to be initialized for every packet.
338 */
339 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
1ac62ba7
BH
340 if (IS_ERR(key->u.ccmp.tfm)) {
341 err = PTR_ERR(key->u.ccmp.tfm);
3b96766f 342 kfree(key);
1ac62ba7 343 key = ERR_PTR(err);
11a843b7 344 }
60ae0f20
JB
345 break;
346 case WLAN_CIPHER_SUITE_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];
3cfcf6ac
JM
352 /*
353 * Initialize AES key state here as an optimization so that
354 * it does not need to be initialized for every packet.
355 */
356 key->u.aes_cmac.tfm =
357 ieee80211_aes_cmac_key_setup(key_data);
1ac62ba7
BH
358 if (IS_ERR(key->u.aes_cmac.tfm)) {
359 err = PTR_ERR(key->u.aes_cmac.tfm);
3cfcf6ac 360 kfree(key);
1ac62ba7 361 key = ERR_PTR(err);
3cfcf6ac 362 }
60ae0f20 363 break;
3cfcf6ac 364 }
60ae0f20
JB
365 memcpy(key->conf.key, key_data, key_len);
366 INIT_LIST_HEAD(&key->list);
3cfcf6ac 367
db4d1169
JB
368 return key;
369}
11a843b7 370
ad0e2b5a
JB
371static void __ieee80211_key_destroy(struct ieee80211_key *key)
372{
373 if (!key)
374 return;
375
32162a4d
JM
376 if (key->local)
377 ieee80211_key_disable_hw_accel(key);
ad0e2b5a 378
97359d12 379 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
ad0e2b5a 380 ieee80211_aes_key_free(key->u.ccmp.tfm);
97359d12 381 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
ad0e2b5a 382 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
32162a4d
JM
383 if (key->local)
384 ieee80211_debugfs_key_remove(key);
ad0e2b5a
JB
385
386 kfree(key);
387}
388
3ffc2a90
JB
389int ieee80211_key_link(struct ieee80211_key *key,
390 struct ieee80211_sub_if_data *sdata,
391 struct sta_info *sta)
db4d1169
JB
392{
393 struct ieee80211_key *old_key;
3ffc2a90 394 int idx, ret;
e31b8213 395 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
db4d1169 396
db4d1169
JB
397 BUG_ON(!sdata);
398 BUG_ON(!key);
399
400 idx = key->conf.keyidx;
401 key->local = sdata->local;
402 key->sdata = sdata;
403 key->sta = sta;
404
11a843b7 405 if (sta) {
11a843b7
JB
406 /*
407 * some hardware cannot handle TKIP with QoS, so
408 * we indicate whether QoS could be in use.
409 */
07346f81 410 if (test_sta_flags(sta, WLAN_STA_WME))
11a843b7
JB
411 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
412 } else {
05c914fe 413 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
11a843b7
JB
414 struct sta_info *ap;
415
3b96766f
JB
416 /*
417 * We're getting a sta pointer in,
418 * so must be under RCU read lock.
419 */
d0709a65 420
11a843b7 421 /* same here, the AP could be using QoS */
abe60632 422 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
11a843b7 423 if (ap) {
07346f81 424 if (test_sta_flags(ap, WLAN_STA_WME))
11a843b7
JB
425 key->conf.flags |=
426 IEEE80211_KEY_FLAG_WMM_STA;
11a843b7
JB
427 }
428 }
11a843b7
JB
429 }
430
ad0e2b5a 431 mutex_lock(&sdata->local->key_mtx);
3b96766f 432
e31b8213
JB
433 if (sta && pairwise)
434 old_key = sta->ptk;
435 else if (sta)
436 old_key = sta->gtk[idx];
d4e46a3d 437 else
db4d1169
JB
438 old_key = sdata->keys[idx];
439
e31b8213 440 __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
ad0e2b5a 441 __ieee80211_key_destroy(old_key);
d4e46a3d 442
ad0e2b5a 443 ieee80211_debugfs_key_add(key);
db4d1169 444
3ffc2a90 445 ret = ieee80211_key_enable_hw_accel(key);
523d2f69 446
ad0e2b5a 447 mutex_unlock(&sdata->local->key_mtx);
3ffc2a90
JB
448
449 return ret;
1f5a7e47
JB
450}
451
3a245766 452static void __ieee80211_key_free(struct ieee80211_key *key)
1f5a7e47 453{
3b96766f
JB
454 /*
455 * Replace key with nothingness if it was ever used.
456 */
3a245766 457 if (key->sdata)
3b96766f 458 __ieee80211_key_replace(key->sdata, key->sta,
e31b8213
JB
459 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
460 key, NULL);
ad0e2b5a 461 __ieee80211_key_destroy(key);
3b96766f 462}
d4e46a3d 463
32162a4d
JM
464void ieee80211_key_free(struct ieee80211_local *local,
465 struct ieee80211_key *key)
3b96766f 466{
3a245766 467 if (!key)
3b96766f
JB
468 return;
469
ad0e2b5a 470 mutex_lock(&local->key_mtx);
3a245766 471 __ieee80211_key_free(key);
ad0e2b5a 472 mutex_unlock(&local->key_mtx);
3a245766
JB
473}
474
ad0e2b5a 475void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
3a245766
JB
476{
477 struct ieee80211_key *key;
11a843b7 478
3a245766 479 ASSERT_RTNL();
11a843b7 480
9607e6b6 481 if (WARN_ON(!ieee80211_sdata_running(sdata)))
3a245766 482 return;
11a843b7 483
ad0e2b5a 484 mutex_lock(&sdata->local->key_mtx);
11a843b7 485
ad0e2b5a
JB
486 list_for_each_entry(key, &sdata->key_list, list)
487 ieee80211_key_enable_hw_accel(key);
3b96766f 488
ad0e2b5a 489 mutex_unlock(&sdata->local->key_mtx);
11a843b7
JB
490}
491
ad0e2b5a 492void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
11a843b7
JB
493{
494 struct ieee80211_key *key;
495
ad0e2b5a 496 ASSERT_RTNL();
db4d1169 497
ad0e2b5a 498 mutex_lock(&sdata->local->key_mtx);
11a843b7 499
ad0e2b5a
JB
500 list_for_each_entry(key, &sdata->key_list, list)
501 ieee80211_key_disable_hw_accel(key);
11a843b7 502
ad0e2b5a 503 mutex_unlock(&sdata->local->key_mtx);
3b96766f 504}
11a843b7 505
3b96766f
JB
506void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
507{
508 struct ieee80211_key *key, *tmp;
db4d1169 509
ad0e2b5a 510 mutex_lock(&sdata->local->key_mtx);
3b96766f
JB
511
512 ieee80211_debugfs_key_remove_default(sdata);
3cfcf6ac 513 ieee80211_debugfs_key_remove_mgmt_default(sdata);
3b96766f
JB
514
515 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
3a245766 516 __ieee80211_key_free(key);
3b96766f 517
ad0e2b5a 518 mutex_unlock(&sdata->local->key_mtx);
11a843b7 519}
This page took 0.319957 seconds and 5 git commands to generate.