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>
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.
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"
27 * DOC: Key handling basics
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.
33 * Hardware acceleration is done on a best-effort basis for algorithms
34 * that are implemented in software, for each key the hardware is asked
35 * to enable that key for offloading but if it cannot do that the key is
36 * simply kept for software encryption (unless it is for an algorithm
37 * that isn't implemented in software).
38 * There is currently no way of knowing whether a key is handled in SW
39 * or HW except by looking into debugfs.
41 * All key management is internally protected by a mutex. Within all
42 * other parts of mac80211, key references are, just as STA structure
43 * references, protected by RCU. Note, however, that some things are
44 * unprotected, namely the key->sta dereferences within the hardware
45 * acceleration functions. This means that sta_info_destroy() must
46 * remove the key which waits for an RCU grace period.
49 static const u8 bcast_addr
[ETH_ALEN
] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
51 static void assert_key_lock(struct ieee80211_local
*local
)
53 lockdep_assert_held(&local
->key_mtx
);
56 static struct ieee80211_sta
*get_sta_for_key(struct ieee80211_key
*key
)
59 return &key
->sta
->sta
;
64 static int ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
66 struct ieee80211_sub_if_data
*sdata
;
67 struct ieee80211_sta
*sta
;
72 if (!key
->local
->ops
->set_key
)
75 assert_key_lock(key
->local
);
77 sta
= get_sta_for_key(key
);
80 * If this is a per-STA GTK, check if it
81 * is supported; if not, return.
83 if (sta
&& !(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
) &&
84 !(key
->local
->hw
.flags
& IEEE80211_HW_SUPPORTS_PER_STA_GTK
))
88 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
) {
90 * The driver doesn't know anything about VLAN interfaces.
91 * Hence, don't send GTKs for VLAN interfaces to the driver.
93 if (!(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
95 sdata
= container_of(sdata
->bss
,
96 struct ieee80211_sub_if_data
,
100 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
, sta
, &key
->conf
);
103 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
105 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
106 (key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)))
107 key
->local
->crypto_tx_tailroom_needed_cnt
--;
112 if (ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
)
113 wiphy_err(key
->local
->hw
.wiphy
,
114 "failed to set key (%d, %pM) to hardware (%d)\n",
115 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
118 switch (key
->conf
.cipher
) {
119 case WLAN_CIPHER_SUITE_WEP40
:
120 case WLAN_CIPHER_SUITE_WEP104
:
121 case WLAN_CIPHER_SUITE_TKIP
:
122 case WLAN_CIPHER_SUITE_CCMP
:
123 case WLAN_CIPHER_SUITE_AES_CMAC
:
124 /* all of these we can do in software */
131 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
133 struct ieee80211_sub_if_data
*sdata
;
134 struct ieee80211_sta
*sta
;
139 if (!key
|| !key
->local
->ops
->set_key
)
142 assert_key_lock(key
->local
);
144 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
147 sta
= get_sta_for_key(key
);
150 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
151 sdata
= container_of(sdata
->bss
,
152 struct ieee80211_sub_if_data
,
155 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
159 wiphy_err(key
->local
->hw
.wiphy
,
160 "failed to remove key (%d, %pM) from hardware (%d)\n",
161 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
163 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
165 if (!((key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) ||
166 (key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)))
167 key
->local
->crypto_tx_tailroom_needed_cnt
++;
170 void ieee80211_key_removed(struct ieee80211_key_conf
*key_conf
)
172 struct ieee80211_key
*key
;
174 key
= container_of(key_conf
, struct ieee80211_key
, conf
);
177 assert_key_lock(key
->local
);
179 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
182 * Flush TX path to avoid attempts to use this key
183 * after this function returns. Until then, drivers
184 * must be prepared to handle the key.
188 EXPORT_SYMBOL_GPL(ieee80211_key_removed
);
190 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
191 int idx
, bool uni
, bool multi
)
193 struct ieee80211_key
*key
= NULL
;
195 assert_key_lock(sdata
->local
);
197 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
198 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
201 rcu_assign_pointer(sdata
->default_unicast_key
, key
);
203 rcu_assign_pointer(sdata
->default_multicast_key
, key
);
205 ieee80211_debugfs_key_update_default(sdata
);
208 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
,
209 bool uni
, bool multi
)
211 mutex_lock(&sdata
->local
->key_mtx
);
212 __ieee80211_set_default_key(sdata
, idx
, uni
, multi
);
213 mutex_unlock(&sdata
->local
->key_mtx
);
217 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
219 struct ieee80211_key
*key
= NULL
;
221 assert_key_lock(sdata
->local
);
223 if (idx
>= NUM_DEFAULT_KEYS
&&
224 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
225 key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
227 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
229 ieee80211_debugfs_key_update_default(sdata
);
232 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
235 mutex_lock(&sdata
->local
->key_mtx
);
236 __ieee80211_set_default_mgmt_key(sdata
, idx
);
237 mutex_unlock(&sdata
->local
->key_mtx
);
241 static void __ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
242 struct sta_info
*sta
,
244 struct ieee80211_key
*old
,
245 struct ieee80211_key
*new)
248 bool defunikey
, defmultikey
, defmgmtkey
;
251 list_add(&new->list
, &sdata
->key_list
);
253 if (sta
&& pairwise
) {
254 rcu_assign_pointer(sta
->ptk
, new);
257 idx
= old
->conf
.keyidx
;
259 idx
= new->conf
.keyidx
;
260 rcu_assign_pointer(sta
->gtk
[idx
], new);
262 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
265 idx
= old
->conf
.keyidx
;
267 idx
= new->conf
.keyidx
;
270 old
== key_mtx_dereference(sdata
->local
,
271 sdata
->default_unicast_key
);
273 old
== key_mtx_dereference(sdata
->local
,
274 sdata
->default_multicast_key
);
276 old
== key_mtx_dereference(sdata
->local
,
277 sdata
->default_mgmt_key
);
279 if (defunikey
&& !new)
280 __ieee80211_set_default_key(sdata
, -1, true, false);
281 if (defmultikey
&& !new)
282 __ieee80211_set_default_key(sdata
, -1, false, true);
283 if (defmgmtkey
&& !new)
284 __ieee80211_set_default_mgmt_key(sdata
, -1);
286 rcu_assign_pointer(sdata
->keys
[idx
], new);
287 if (defunikey
&& new)
288 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
290 if (defmultikey
&& new)
291 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
,
293 if (defmgmtkey
&& new)
294 __ieee80211_set_default_mgmt_key(sdata
,
299 list_del(&old
->list
);
302 struct ieee80211_key
*ieee80211_key_alloc(u32 cipher
, int idx
, size_t key_len
,
304 size_t seq_len
, const u8
*seq
)
306 struct ieee80211_key
*key
;
309 BUG_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
);
311 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
313 return ERR_PTR(-ENOMEM
);
316 * Default to software encryption; we'll later upload the
317 * key to the hardware if possible.
322 key
->conf
.cipher
= cipher
;
323 key
->conf
.keyidx
= idx
;
324 key
->conf
.keylen
= key_len
;
326 case WLAN_CIPHER_SUITE_WEP40
:
327 case WLAN_CIPHER_SUITE_WEP104
:
328 key
->conf
.iv_len
= WEP_IV_LEN
;
329 key
->conf
.icv_len
= WEP_ICV_LEN
;
331 case WLAN_CIPHER_SUITE_TKIP
:
332 key
->conf
.iv_len
= TKIP_IV_LEN
;
333 key
->conf
.icv_len
= TKIP_ICV_LEN
;
335 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++) {
336 key
->u
.tkip
.rx
[i
].iv32
=
337 get_unaligned_le32(&seq
[2]);
338 key
->u
.tkip
.rx
[i
].iv16
=
339 get_unaligned_le16(seq
);
343 case WLAN_CIPHER_SUITE_CCMP
:
344 key
->conf
.iv_len
= CCMP_HDR_LEN
;
345 key
->conf
.icv_len
= CCMP_MIC_LEN
;
347 for (i
= 0; i
< NUM_RX_DATA_QUEUES
+ 1; i
++)
348 for (j
= 0; j
< CCMP_PN_LEN
; j
++)
349 key
->u
.ccmp
.rx_pn
[i
][j
] =
350 seq
[CCMP_PN_LEN
- j
- 1];
353 * Initialize AES key state here as an optimization so that
354 * it does not need to be initialized for every packet.
356 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(key_data
);
357 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
358 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
363 case WLAN_CIPHER_SUITE_AES_CMAC
:
364 key
->conf
.iv_len
= 0;
365 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
367 for (j
= 0; j
< 6; j
++)
368 key
->u
.aes_cmac
.rx_pn
[j
] = seq
[6 - j
- 1];
370 * Initialize AES key state here as an optimization so that
371 * it does not need to be initialized for every packet.
373 key
->u
.aes_cmac
.tfm
=
374 ieee80211_aes_cmac_key_setup(key_data
);
375 if (IS_ERR(key
->u
.aes_cmac
.tfm
)) {
376 err
= PTR_ERR(key
->u
.aes_cmac
.tfm
);
382 memcpy(key
->conf
.key
, key_data
, key_len
);
383 INIT_LIST_HEAD(&key
->list
);
388 static void __ieee80211_key_destroy(struct ieee80211_key
*key
)
394 * Synchronize so the TX path can no longer be using
395 * this key before we free/remove it.
400 ieee80211_key_disable_hw_accel(key
);
402 if (key
->conf
.cipher
== WLAN_CIPHER_SUITE_CCMP
)
403 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
404 if (key
->conf
.cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
405 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
407 ieee80211_debugfs_key_remove(key
);
408 key
->local
->crypto_tx_tailroom_needed_cnt
--;
414 int ieee80211_key_link(struct ieee80211_key
*key
,
415 struct ieee80211_sub_if_data
*sdata
,
416 struct sta_info
*sta
)
418 struct ieee80211_key
*old_key
;
425 pairwise
= key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
;
426 idx
= key
->conf
.keyidx
;
427 key
->local
= sdata
->local
;
433 * some hardware cannot handle TKIP with QoS, so
434 * we indicate whether QoS could be in use.
436 if (test_sta_flags(sta
, WLAN_STA_WME
))
437 key
->conf
.flags
|= IEEE80211_KEY_FLAG_WMM_STA
;
439 if (sdata
->vif
.type
== NL80211_IFTYPE_STATION
) {
443 * We're getting a sta pointer in, so must be under
444 * appropriate locking for sta_info_get().
447 /* same here, the AP could be using QoS */
448 ap
= sta_info_get(key
->sdata
, key
->sdata
->u
.mgd
.bssid
);
450 if (test_sta_flags(ap
, WLAN_STA_WME
))
452 IEEE80211_KEY_FLAG_WMM_STA
;
457 mutex_lock(&sdata
->local
->key_mtx
);
460 old_key
= key_mtx_dereference(sdata
->local
, sta
->ptk
);
462 old_key
= key_mtx_dereference(sdata
->local
, sta
->gtk
[idx
]);
464 old_key
= key_mtx_dereference(sdata
->local
, sdata
->keys
[idx
]);
466 __ieee80211_key_replace(sdata
, sta
, pairwise
, old_key
, key
);
467 __ieee80211_key_destroy(old_key
);
469 ieee80211_debugfs_key_add(key
);
471 key
->local
->crypto_tx_tailroom_needed_cnt
++;
473 ret
= ieee80211_key_enable_hw_accel(key
);
475 mutex_unlock(&sdata
->local
->key_mtx
);
480 void __ieee80211_key_free(struct ieee80211_key
*key
)
486 * Replace key with nothingness if it was ever used.
489 __ieee80211_key_replace(key
->sdata
, key
->sta
,
490 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
492 __ieee80211_key_destroy(key
);
495 void ieee80211_key_free(struct ieee80211_local
*local
,
496 struct ieee80211_key
*key
)
498 mutex_lock(&local
->key_mtx
);
499 __ieee80211_key_free(key
);
500 mutex_unlock(&local
->key_mtx
);
503 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
505 struct ieee80211_key
*key
;
509 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
512 mutex_lock(&sdata
->local
->key_mtx
);
514 sdata
->local
->crypto_tx_tailroom_needed_cnt
= 0;
516 list_for_each_entry(key
, &sdata
->key_list
, list
) {
517 sdata
->local
->crypto_tx_tailroom_needed_cnt
++;
518 ieee80211_key_enable_hw_accel(key
);
521 mutex_unlock(&sdata
->local
->key_mtx
);
524 void ieee80211_disable_keys(struct ieee80211_sub_if_data
*sdata
)
526 struct ieee80211_key
*key
;
530 mutex_lock(&sdata
->local
->key_mtx
);
532 list_for_each_entry(key
, &sdata
->key_list
, list
)
533 ieee80211_key_disable_hw_accel(key
);
535 mutex_unlock(&sdata
->local
->key_mtx
);
538 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
)
540 struct ieee80211_key
*key
, *tmp
;
542 mutex_lock(&sdata
->local
->key_mtx
);
544 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
546 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
547 __ieee80211_key_free(key
);
549 ieee80211_debugfs_key_update_default(sdata
);
551 mutex_unlock(&sdata
->local
->key_mtx
);