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 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
39 * All key operations are protected internally.
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
44 * functions. This means that sta_info_destroy() must remove the key
45 * which waits for an RCU grace period.
48 static const u8 bcast_addr
[ETH_ALEN
] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
50 static void assert_key_lock(struct ieee80211_local
*local
)
52 lockdep_assert_held(&local
->key_mtx
);
55 static struct ieee80211_sta
*get_sta_for_key(struct ieee80211_key
*key
)
58 return &key
->sta
->sta
;
63 static int ieee80211_key_enable_hw_accel(struct ieee80211_key
*key
)
65 struct ieee80211_sub_if_data
*sdata
;
66 struct ieee80211_sta
*sta
;
71 if (!key
->local
->ops
->set_key
)
74 assert_key_lock(key
->local
);
76 sta
= get_sta_for_key(key
);
79 * If this is a per-STA GTK, check if it
80 * is supported; if not, return.
82 if (sta
&& !(key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
) &&
83 !(key
->local
->hw
.flags
& IEEE80211_HW_SUPPORTS_PER_STA_GTK
))
87 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
88 sdata
= container_of(sdata
->bss
,
89 struct ieee80211_sub_if_data
,
92 ret
= drv_set_key(key
->local
, SET_KEY
, sdata
, sta
, &key
->conf
);
95 key
->flags
|= KEY_FLAG_UPLOADED_TO_HARDWARE
;
99 if (ret
!= -ENOSPC
&& ret
!= -EOPNOTSUPP
)
100 wiphy_err(key
->local
->hw
.wiphy
,
101 "failed to set key (%d, %pM) to hardware (%d)\n",
102 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
105 switch (key
->conf
.cipher
) {
106 case WLAN_CIPHER_SUITE_WEP40
:
107 case WLAN_CIPHER_SUITE_WEP104
:
108 case WLAN_CIPHER_SUITE_TKIP
:
109 case WLAN_CIPHER_SUITE_CCMP
:
110 case WLAN_CIPHER_SUITE_AES_CMAC
:
111 /* all of these we can do in software */
118 static void ieee80211_key_disable_hw_accel(struct ieee80211_key
*key
)
120 struct ieee80211_sub_if_data
*sdata
;
121 struct ieee80211_sta
*sta
;
126 if (!key
|| !key
->local
->ops
->set_key
)
129 assert_key_lock(key
->local
);
131 if (!(key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
134 sta
= get_sta_for_key(key
);
137 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
138 sdata
= container_of(sdata
->bss
,
139 struct ieee80211_sub_if_data
,
142 ret
= drv_set_key(key
->local
, DISABLE_KEY
, sdata
,
146 wiphy_err(key
->local
->hw
.wiphy
,
147 "failed to remove key (%d, %pM) from hardware (%d)\n",
148 key
->conf
.keyidx
, sta
? sta
->addr
: bcast_addr
, ret
);
150 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
153 void ieee80211_key_removed(struct ieee80211_key_conf
*key_conf
)
155 struct ieee80211_key
*key
;
157 key
= container_of(key_conf
, struct ieee80211_key
, conf
);
160 assert_key_lock(key
->local
);
162 key
->flags
&= ~KEY_FLAG_UPLOADED_TO_HARDWARE
;
165 * Flush TX path to avoid attempts to use this key
166 * after this function returns. Until then, drivers
167 * must be prepared to handle the key.
171 EXPORT_SYMBOL_GPL(ieee80211_key_removed
);
173 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
,
176 struct ieee80211_key
*key
= NULL
;
178 assert_key_lock(sdata
->local
);
180 if (idx
>= 0 && idx
< NUM_DEFAULT_KEYS
)
181 key
= sdata
->keys
[idx
];
183 rcu_assign_pointer(sdata
->default_key
, key
);
186 ieee80211_debugfs_key_remove_default(key
->sdata
);
187 ieee80211_debugfs_key_add_default(key
->sdata
);
191 void ieee80211_set_default_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
193 mutex_lock(&sdata
->local
->key_mtx
);
194 __ieee80211_set_default_key(sdata
, idx
);
195 mutex_unlock(&sdata
->local
->key_mtx
);
199 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
, int idx
)
201 struct ieee80211_key
*key
= NULL
;
203 assert_key_lock(sdata
->local
);
205 if (idx
>= NUM_DEFAULT_KEYS
&&
206 idx
< NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
)
207 key
= sdata
->keys
[idx
];
209 rcu_assign_pointer(sdata
->default_mgmt_key
, key
);
212 ieee80211_debugfs_key_remove_mgmt_default(key
->sdata
);
213 ieee80211_debugfs_key_add_mgmt_default(key
->sdata
);
217 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data
*sdata
,
220 mutex_lock(&sdata
->local
->key_mtx
);
221 __ieee80211_set_default_mgmt_key(sdata
, idx
);
222 mutex_unlock(&sdata
->local
->key_mtx
);
226 static void __ieee80211_key_replace(struct ieee80211_sub_if_data
*sdata
,
227 struct sta_info
*sta
,
229 struct ieee80211_key
*old
,
230 struct ieee80211_key
*new)
232 int idx
, defkey
, defmgmtkey
;
235 list_add(&new->list
, &sdata
->key_list
);
237 if (sta
&& pairwise
) {
238 rcu_assign_pointer(sta
->ptk
, new);
241 idx
= old
->conf
.keyidx
;
243 idx
= new->conf
.keyidx
;
244 rcu_assign_pointer(sta
->gtk
[idx
], new);
246 WARN_ON(new && old
&& new->conf
.keyidx
!= old
->conf
.keyidx
);
249 idx
= old
->conf
.keyidx
;
251 idx
= new->conf
.keyidx
;
253 defkey
= old
&& sdata
->default_key
== old
;
254 defmgmtkey
= old
&& sdata
->default_mgmt_key
== old
;
257 __ieee80211_set_default_key(sdata
, -1);
258 if (defmgmtkey
&& !new)
259 __ieee80211_set_default_mgmt_key(sdata
, -1);
261 rcu_assign_pointer(sdata
->keys
[idx
], new);
263 __ieee80211_set_default_key(sdata
, new->conf
.keyidx
);
264 if (defmgmtkey
&& new)
265 __ieee80211_set_default_mgmt_key(sdata
,
271 * We'll use an empty list to indicate that the key
272 * has already been removed.
274 list_del_init(&old
->list
);
278 struct ieee80211_key
*ieee80211_key_alloc(u32 cipher
, int idx
, size_t key_len
,
280 size_t seq_len
, const u8
*seq
)
282 struct ieee80211_key
*key
;
285 BUG_ON(idx
< 0 || idx
>= NUM_DEFAULT_KEYS
+ NUM_DEFAULT_MGMT_KEYS
);
287 key
= kzalloc(sizeof(struct ieee80211_key
) + key_len
, GFP_KERNEL
);
289 return ERR_PTR(-ENOMEM
);
292 * Default to software encryption; we'll later upload the
293 * key to the hardware if possible.
298 key
->conf
.cipher
= cipher
;
299 key
->conf
.keyidx
= idx
;
300 key
->conf
.keylen
= key_len
;
302 case WLAN_CIPHER_SUITE_WEP40
:
303 case WLAN_CIPHER_SUITE_WEP104
:
304 key
->conf
.iv_len
= WEP_IV_LEN
;
305 key
->conf
.icv_len
= WEP_ICV_LEN
;
307 case WLAN_CIPHER_SUITE_TKIP
:
308 key
->conf
.iv_len
= TKIP_IV_LEN
;
309 key
->conf
.icv_len
= TKIP_ICV_LEN
;
311 for (i
= 0; i
< NUM_RX_DATA_QUEUES
; i
++) {
312 key
->u
.tkip
.rx
[i
].iv32
=
313 get_unaligned_le32(&seq
[2]);
314 key
->u
.tkip
.rx
[i
].iv16
=
315 get_unaligned_le16(seq
);
319 case WLAN_CIPHER_SUITE_CCMP
:
320 key
->conf
.iv_len
= CCMP_HDR_LEN
;
321 key
->conf
.icv_len
= CCMP_MIC_LEN
;
323 for (i
= 0; i
< NUM_RX_DATA_QUEUES
+ 1; i
++)
324 for (j
= 0; j
< CCMP_PN_LEN
; j
++)
325 key
->u
.ccmp
.rx_pn
[i
][j
] =
326 seq
[CCMP_PN_LEN
- j
- 1];
329 * Initialize AES key state here as an optimization so that
330 * it does not need to be initialized for every packet.
332 key
->u
.ccmp
.tfm
= ieee80211_aes_key_setup_encrypt(key_data
);
333 if (IS_ERR(key
->u
.ccmp
.tfm
)) {
334 err
= PTR_ERR(key
->u
.ccmp
.tfm
);
339 case WLAN_CIPHER_SUITE_AES_CMAC
:
340 key
->conf
.iv_len
= 0;
341 key
->conf
.icv_len
= sizeof(struct ieee80211_mmie
);
343 for (j
= 0; j
< 6; j
++)
344 key
->u
.aes_cmac
.rx_pn
[j
] = seq
[6 - j
- 1];
346 * Initialize AES key state here as an optimization so that
347 * it does not need to be initialized for every packet.
349 key
->u
.aes_cmac
.tfm
=
350 ieee80211_aes_cmac_key_setup(key_data
);
351 if (IS_ERR(key
->u
.aes_cmac
.tfm
)) {
352 err
= PTR_ERR(key
->u
.aes_cmac
.tfm
);
358 memcpy(key
->conf
.key
, key_data
, key_len
);
359 INIT_LIST_HEAD(&key
->list
);
364 static void __ieee80211_key_destroy(struct ieee80211_key
*key
)
370 ieee80211_key_disable_hw_accel(key
);
372 if (key
->conf
.cipher
== WLAN_CIPHER_SUITE_CCMP
)
373 ieee80211_aes_key_free(key
->u
.ccmp
.tfm
);
374 if (key
->conf
.cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
375 ieee80211_aes_cmac_key_free(key
->u
.aes_cmac
.tfm
);
377 ieee80211_debugfs_key_remove(key
);
382 int ieee80211_key_link(struct ieee80211_key
*key
,
383 struct ieee80211_sub_if_data
*sdata
,
384 struct sta_info
*sta
)
386 struct ieee80211_key
*old_key
;
388 bool pairwise
= key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
;
393 idx
= key
->conf
.keyidx
;
394 key
->local
= sdata
->local
;
400 * some hardware cannot handle TKIP with QoS, so
401 * we indicate whether QoS could be in use.
403 if (test_sta_flags(sta
, WLAN_STA_WME
))
404 key
->conf
.flags
|= IEEE80211_KEY_FLAG_WMM_STA
;
406 if (sdata
->vif
.type
== NL80211_IFTYPE_STATION
) {
410 * We're getting a sta pointer in,
411 * so must be under RCU read lock.
414 /* same here, the AP could be using QoS */
415 ap
= sta_info_get(key
->sdata
, key
->sdata
->u
.mgd
.bssid
);
417 if (test_sta_flags(ap
, WLAN_STA_WME
))
419 IEEE80211_KEY_FLAG_WMM_STA
;
424 mutex_lock(&sdata
->local
->key_mtx
);
429 old_key
= sta
->gtk
[idx
];
431 old_key
= sdata
->keys
[idx
];
433 __ieee80211_key_replace(sdata
, sta
, pairwise
, old_key
, key
);
434 __ieee80211_key_destroy(old_key
);
436 ieee80211_debugfs_key_add(key
);
438 ret
= ieee80211_key_enable_hw_accel(key
);
440 mutex_unlock(&sdata
->local
->key_mtx
);
445 static void __ieee80211_key_free(struct ieee80211_key
*key
)
448 * Replace key with nothingness if it was ever used.
451 __ieee80211_key_replace(key
->sdata
, key
->sta
,
452 key
->conf
.flags
& IEEE80211_KEY_FLAG_PAIRWISE
,
454 __ieee80211_key_destroy(key
);
457 void ieee80211_key_free(struct ieee80211_local
*local
,
458 struct ieee80211_key
*key
)
463 mutex_lock(&local
->key_mtx
);
464 __ieee80211_key_free(key
);
465 mutex_unlock(&local
->key_mtx
);
468 void ieee80211_enable_keys(struct ieee80211_sub_if_data
*sdata
)
470 struct ieee80211_key
*key
;
474 if (WARN_ON(!ieee80211_sdata_running(sdata
)))
477 mutex_lock(&sdata
->local
->key_mtx
);
479 list_for_each_entry(key
, &sdata
->key_list
, list
)
480 ieee80211_key_enable_hw_accel(key
);
482 mutex_unlock(&sdata
->local
->key_mtx
);
485 void ieee80211_disable_keys(struct ieee80211_sub_if_data
*sdata
)
487 struct ieee80211_key
*key
;
491 mutex_lock(&sdata
->local
->key_mtx
);
493 list_for_each_entry(key
, &sdata
->key_list
, list
)
494 ieee80211_key_disable_hw_accel(key
);
496 mutex_unlock(&sdata
->local
->key_mtx
);
499 void ieee80211_free_keys(struct ieee80211_sub_if_data
*sdata
)
501 struct ieee80211_key
*key
, *tmp
;
503 mutex_lock(&sdata
->local
->key_mtx
);
505 ieee80211_debugfs_key_remove_default(sdata
);
506 ieee80211_debugfs_key_remove_mgmt_default(sdata
);
508 list_for_each_entry_safe(key
, tmp
, &sdata
->key_list
, list
)
509 __ieee80211_key_free(key
);
511 mutex_unlock(&sdata
->local
->key_mtx
);