]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1f5a7e47 JB |
2 | /* |
3 | * Copyright 2002-2005, Instant802 Networks, Inc. | |
4 | * Copyright 2005-2006, Devicescape Software, Inc. | |
5 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> | |
3b96766f | 6 | * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net> |
d98ad83e | 7 | * Copyright 2013-2014 Intel Mobile Communications GmbH |
fdf7cb41 | 8 | * Copyright 2015-2017 Intel Deutschland GmbH |
1f5a7e47 JB |
9 | */ |
10 | ||
11a843b7 JB |
11 | #include <linux/if_ether.h> |
12 | #include <linux/etherdevice.h> | |
13 | #include <linux/list.h> | |
d4e46a3d | 14 | #include <linux/rcupdate.h> |
db4d1169 | 15 | #include <linux/rtnetlink.h> |
5a0e3ad6 | 16 | #include <linux/slab.h> |
bc3b2d7f | 17 | #include <linux/export.h> |
1f5a7e47 | 18 | #include <net/mac80211.h> |
2bdd713b | 19 | #include <crypto/algapi.h> |
d26ad377 | 20 | #include <asm/unaligned.h> |
1f5a7e47 | 21 | #include "ieee80211_i.h" |
24487981 | 22 | #include "driver-ops.h" |
1f5a7e47 JB |
23 | #include "debugfs_key.h" |
24 | #include "aes_ccm.h" | |
3cfcf6ac | 25 | #include "aes_cmac.h" |
8ade538b | 26 | #include "aes_gmac.h" |
00b9cfa3 | 27 | #include "aes_gcm.h" |
1f5a7e47 | 28 | |
11a843b7 | 29 | |
dbbea671 JB |
30 | /** |
31 | * DOC: Key handling basics | |
11a843b7 JB |
32 | * |
33 | * Key handling in mac80211 is done based on per-interface (sub_if_data) | |
34 | * keys and per-station keys. Since each station belongs to an interface, | |
35 | * each station key also belongs to that interface. | |
36 | * | |
b5c34f66 JB |
37 | * Hardware acceleration is done on a best-effort basis for algorithms |
38 | * that are implemented in software, for each key the hardware is asked | |
39 | * to enable that key for offloading but if it cannot do that the key is | |
40 | * simply kept for software encryption (unless it is for an algorithm | |
41 | * that isn't implemented in software). | |
42 | * There is currently no way of knowing whether a key is handled in SW | |
43 | * or HW except by looking into debugfs. | |
11a843b7 | 44 | * |
b5c34f66 JB |
45 | * All key management is internally protected by a mutex. Within all |
46 | * other parts of mac80211, key references are, just as STA structure | |
47 | * references, protected by RCU. Note, however, that some things are | |
48 | * unprotected, namely the key->sta dereferences within the hardware | |
49 | * acceleration functions. This means that sta_info_destroy() must | |
50 | * remove the key which waits for an RCU grace period. | |
11a843b7 JB |
51 | */ |
52 | ||
53 | static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; | |
11a843b7 | 54 | |
ad0e2b5a | 55 | static void assert_key_lock(struct ieee80211_local *local) |
3b96766f | 56 | { |
46a5ebaf | 57 | lockdep_assert_held(&local->key_mtx); |
3b96766f JB |
58 | } |
59 | ||
f9dca80b MK |
60 | static void |
61 | update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta) | |
62 | { | |
63 | struct ieee80211_sub_if_data *vlan; | |
64 | ||
65 | if (sdata->vif.type != NL80211_IFTYPE_AP) | |
66 | return; | |
67 | ||
51f458d9 JB |
68 | /* crypto_tx_tailroom_needed_cnt is protected by this */ |
69 | assert_key_lock(sdata->local); | |
f9dca80b | 70 | |
51f458d9 JB |
71 | rcu_read_lock(); |
72 | ||
73 | list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list) | |
f9dca80b MK |
74 | vlan->crypto_tx_tailroom_needed_cnt += delta; |
75 | ||
51f458d9 | 76 | rcu_read_unlock(); |
f9dca80b MK |
77 | } |
78 | ||
3bff1865 YAP |
79 | static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata) |
80 | { | |
81 | /* | |
82 | * When this count is zero, SKB resizing for allocating tailroom | |
83 | * for IV or MMIC is skipped. But, this check has created two race | |
84 | * cases in xmit path while transiting from zero count to one: | |
85 | * | |
86 | * 1. SKB resize was skipped because no key was added but just before | |
87 | * the xmit key is added and SW encryption kicks off. | |
88 | * | |
89 | * 2. SKB resize was skipped because all the keys were hw planted but | |
90 | * just before xmit one of the key is deleted and SW encryption kicks | |
91 | * off. | |
92 | * | |
93 | * In both the above case SW encryption will find not enough space for | |
94 | * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c) | |
95 | * | |
96 | * Solution has been explained at | |
97 | * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net | |
98 | */ | |
99 | ||
51f458d9 JB |
100 | assert_key_lock(sdata->local); |
101 | ||
f9dca80b MK |
102 | update_vlan_tailroom_need_count(sdata, 1); |
103 | ||
3bff1865 YAP |
104 | if (!sdata->crypto_tx_tailroom_needed_cnt++) { |
105 | /* | |
106 | * Flush all XMIT packets currently using HW encryption or no | |
107 | * encryption at all if the count transition is from 0 -> 1. | |
108 | */ | |
109 | synchronize_net(); | |
110 | } | |
111 | } | |
112 | ||
f9dca80b MK |
113 | static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata, |
114 | int delta) | |
115 | { | |
51f458d9 JB |
116 | assert_key_lock(sdata->local); |
117 | ||
f9dca80b MK |
118 | WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta); |
119 | ||
120 | update_vlan_tailroom_need_count(sdata, -delta); | |
121 | sdata->crypto_tx_tailroom_needed_cnt -= delta; | |
122 | } | |
123 | ||
3ffc2a90 | 124 | static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
11a843b7 | 125 | { |
db3bdcb9 | 126 | struct ieee80211_sub_if_data *sdata = key->sdata; |
89c91cae | 127 | struct sta_info *sta; |
fa7e1fbc | 128 | int ret = -EOPNOTSUPP; |
11a843b7 | 129 | |
3b96766f JB |
130 | might_sleep(); |
131 | ||
4619194a JB |
132 | if (key->flags & KEY_FLAG_TAINTED) { |
133 | /* If we get here, it's during resume and the key is | |
134 | * tainted so shouldn't be used/programmed any more. | |
135 | * However, its flags may still indicate that it was | |
136 | * programmed into the device (since we're in resume) | |
137 | * so clear that flag now to avoid trying to remove | |
138 | * it again later. | |
139 | */ | |
092c4098 AW |
140 | if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE && |
141 | !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | | |
142 | IEEE80211_KEY_FLAG_PUT_MIC_SPACE | | |
143 | IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) | |
144 | increment_tailroom_need_count(sdata); | |
145 | ||
4619194a | 146 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
27b3eb9c | 147 | return -EINVAL; |
4619194a | 148 | } |
27b3eb9c | 149 | |
e31b8213 | 150 | if (!key->local->ops->set_key) |
3ffc2a90 | 151 | goto out_unsupported; |
11a843b7 | 152 | |
ad0e2b5a JB |
153 | assert_key_lock(key->local); |
154 | ||
89c91cae | 155 | sta = key->sta; |
dc822b5d | 156 | |
e31b8213 JB |
157 | /* |
158 | * If this is a per-STA GTK, check if it | |
159 | * is supported; if not, return. | |
160 | */ | |
161 | if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) && | |
30686bf7 | 162 | !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK)) |
e31b8213 JB |
163 | goto out_unsupported; |
164 | ||
89c91cae JB |
165 | if (sta && !sta->uploaded) |
166 | goto out_unsupported; | |
167 | ||
18890d4b HS |
168 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { |
169 | /* | |
170 | * The driver doesn't know anything about VLAN interfaces. | |
171 | * Hence, don't send GTKs for VLAN interfaces to the driver. | |
172 | */ | |
78ad2341 AW |
173 | if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { |
174 | ret = 1; | |
18890d4b | 175 | goto out_unsupported; |
78ad2341 | 176 | } |
18890d4b | 177 | } |
11a843b7 | 178 | |
89c91cae JB |
179 | ret = drv_set_key(key->local, SET_KEY, sdata, |
180 | sta ? &sta->sta : NULL, &key->conf); | |
11a843b7 | 181 | |
e31b8213 | 182 | if (!ret) { |
11a843b7 | 183 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
3bff1865 | 184 | |
092c4098 AW |
185 | if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | |
186 | IEEE80211_KEY_FLAG_PUT_MIC_SPACE | | |
187 | IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) | |
f9dca80b | 188 | decrease_tailroom_need_count(sdata, 1); |
3bff1865 | 189 | |
077a9154 AN |
190 | WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && |
191 | (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)); | |
192 | ||
9de18d81 DS |
193 | WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) && |
194 | (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)); | |
195 | ||
e31b8213 JB |
196 | return 0; |
197 | } | |
11a843b7 | 198 | |
fa7e1fbc | 199 | if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1) |
bdcbd8e0 | 200 | sdata_err(sdata, |
0fb9a9ec | 201 | "failed to set key (%d, %pM) to hardware (%d)\n", |
89c91cae JB |
202 | key->conf.keyidx, |
203 | sta ? sta->sta.addr : bcast_addr, ret); | |
3ffc2a90 | 204 | |
e31b8213 JB |
205 | out_unsupported: |
206 | switch (key->conf.cipher) { | |
207 | case WLAN_CIPHER_SUITE_WEP40: | |
208 | case WLAN_CIPHER_SUITE_WEP104: | |
209 | case WLAN_CIPHER_SUITE_TKIP: | |
210 | case WLAN_CIPHER_SUITE_CCMP: | |
2b2ba0db | 211 | case WLAN_CIPHER_SUITE_CCMP_256: |
e31b8213 | 212 | case WLAN_CIPHER_SUITE_AES_CMAC: |
56c52da2 | 213 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
8ade538b JM |
214 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
215 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: | |
00b9cfa3 JM |
216 | case WLAN_CIPHER_SUITE_GCMP: |
217 | case WLAN_CIPHER_SUITE_GCMP_256: | |
fa7e1fbc JB |
218 | /* all of these we can do in software - if driver can */ |
219 | if (ret == 1) | |
220 | return 0; | |
78ad2341 | 221 | if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL)) |
fa7e1fbc | 222 | return -EINVAL; |
e31b8213 JB |
223 | return 0; |
224 | default: | |
225 | return -EINVAL; | |
3ffc2a90 | 226 | } |
11a843b7 JB |
227 | } |
228 | ||
229 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) | |
230 | { | |
dc822b5d | 231 | struct ieee80211_sub_if_data *sdata; |
89c91cae | 232 | struct sta_info *sta; |
11a843b7 JB |
233 | int ret; |
234 | ||
3b96766f JB |
235 | might_sleep(); |
236 | ||
db4d1169 | 237 | if (!key || !key->local->ops->set_key) |
11a843b7 JB |
238 | return; |
239 | ||
ad0e2b5a JB |
240 | assert_key_lock(key->local); |
241 | ||
242 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) | |
11a843b7 JB |
243 | return; |
244 | ||
89c91cae | 245 | sta = key->sta; |
dc822b5d JB |
246 | sdata = key->sdata; |
247 | ||
092c4098 AW |
248 | if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | |
249 | IEEE80211_KEY_FLAG_PUT_MIC_SPACE | | |
250 | IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) | |
3bff1865 YAP |
251 | increment_tailroom_need_count(sdata); |
252 | ||
62872a9b | 253 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
12375ef9 | 254 | ret = drv_set_key(key->local, DISABLE_KEY, sdata, |
89c91cae | 255 | sta ? &sta->sta : NULL, &key->conf); |
11a843b7 JB |
256 | |
257 | if (ret) | |
bdcbd8e0 | 258 | sdata_err(sdata, |
0fb9a9ec | 259 | "failed to remove key (%d, %pM) from hardware (%d)\n", |
89c91cae JB |
260 | key->conf.keyidx, |
261 | sta ? sta->sta.addr : bcast_addr, ret); | |
62872a9b | 262 | } |
11a843b7 | 263 | |
96fc6efb AW |
264 | int ieee80211_set_tx_key(struct ieee80211_key *key) |
265 | { | |
266 | struct sta_info *sta = key->sta; | |
267 | struct ieee80211_local *local = key->local; | |
96fc6efb AW |
268 | |
269 | assert_key_lock(local); | |
270 | ||
96fc6efb | 271 | sta->ptk_idx = key->conf.keyidx; |
90cc4bd6 | 272 | |
3e47bf1c | 273 | clear_sta_flag(sta, WLAN_STA_BLOCK_BA); |
96fc6efb AW |
274 | ieee80211_check_fast_xmit(sta); |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
90cc4bd6 AW |
279 | static void ieee80211_pairwise_rekey(struct ieee80211_key *old, |
280 | struct ieee80211_key *new) | |
62872a9b | 281 | { |
90cc4bd6 AW |
282 | struct ieee80211_local *local = new->local; |
283 | struct sta_info *sta = new->sta; | |
284 | int i; | |
62872a9b | 285 | |
90cc4bd6 | 286 | assert_key_lock(local); |
62872a9b | 287 | |
90cc4bd6 AW |
288 | if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) { |
289 | /* Extended Key ID key install, initial one or rekey */ | |
290 | ||
3e47bf1c | 291 | if (sta->ptk_idx != INVALID_PTK_KEYIDX) { |
90cc4bd6 AW |
292 | /* Aggregation Sessions with Extended Key ID must not |
293 | * mix MPDUs with different keyIDs within one A-MPDU. | |
3e47bf1c AW |
294 | * Tear down running Tx aggregation sessions and block |
295 | * new Rx/Tx aggregation requests during rekey to | |
296 | * ensure there are no A-MPDUs for the driver to | |
297 | * aggregate. (Blocking Tx only would be sufficient but | |
298 | * WLAN_STA_BLOCK_BA gets the job done for the few ms | |
299 | * we need it.) | |
90cc4bd6 AW |
300 | */ |
301 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
302 | mutex_lock(&sta->ampdu_mlme.mtx); | |
303 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) | |
304 | ___ieee80211_stop_tx_ba_session(sta, i, | |
305 | AGG_STOP_LOCAL_REQUEST); | |
306 | mutex_unlock(&sta->ampdu_mlme.mtx); | |
307 | } | |
308 | } else if (old) { | |
309 | /* Rekey without Extended Key ID. | |
310 | * Aggregation sessions are OK when running on SW crypto. | |
311 | * A broken remote STA may cause issues not observed with HW | |
312 | * crypto, though. | |
313 | */ | |
314 | if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) | |
315 | return; | |
62872a9b | 316 | |
90cc4bd6 AW |
317 | /* Stop Tx till we are on the new key */ |
318 | old->flags |= KEY_FLAG_TAINTED; | |
62872a9b | 319 | ieee80211_clear_fast_xmit(sta); |
62872a9b AW |
320 | if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) { |
321 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
322 | ieee80211_sta_tear_down_BA_sessions(sta, | |
323 | AGG_STOP_LOCAL_REQUEST); | |
324 | } | |
62872a9b AW |
325 | if (!wiphy_ext_feature_isset(local->hw.wiphy, |
326 | NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) { | |
327 | pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.", | |
328 | sta->sta.addr); | |
329 | /* Flushing the driver queues *may* help prevent | |
330 | * the clear text leaks and freezes. | |
331 | */ | |
90cc4bd6 | 332 | ieee80211_flush_queues(local, old->sdata, false); |
62872a9b AW |
333 | } |
334 | } | |
3b96766f JB |
335 | } |
336 | ||
337 | static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, | |
f7e0104c | 338 | int idx, bool uni, bool multi) |
3b96766f JB |
339 | { |
340 | struct ieee80211_key *key = NULL; | |
341 | ||
ad0e2b5a JB |
342 | assert_key_lock(sdata->local); |
343 | ||
3b96766f | 344 | if (idx >= 0 && idx < NUM_DEFAULT_KEYS) |
40b275b6 | 345 | key = key_mtx_dereference(sdata->local, sdata->keys[idx]); |
3b96766f | 346 | |
de5fad81 | 347 | if (uni) { |
f7e0104c | 348 | rcu_assign_pointer(sdata->default_unicast_key, key); |
17c18bf8 | 349 | ieee80211_check_fast_xmit_iface(sdata); |
ec4efc4a JB |
350 | if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN) |
351 | drv_set_default_unicast_key(sdata->local, sdata, idx); | |
de5fad81 YD |
352 | } |
353 | ||
f7e0104c JB |
354 | if (multi) |
355 | rcu_assign_pointer(sdata->default_multicast_key, key); | |
3b96766f | 356 | |
f7e0104c | 357 | ieee80211_debugfs_key_update_default(sdata); |
3b96766f JB |
358 | } |
359 | ||
f7e0104c JB |
360 | void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx, |
361 | bool uni, bool multi) | |
3b96766f | 362 | { |
ad0e2b5a | 363 | mutex_lock(&sdata->local->key_mtx); |
f7e0104c | 364 | __ieee80211_set_default_key(sdata, idx, uni, multi); |
ad0e2b5a | 365 | mutex_unlock(&sdata->local->key_mtx); |
3b96766f JB |
366 | } |
367 | ||
3cfcf6ac JM |
368 | static void |
369 | __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx) | |
370 | { | |
371 | struct ieee80211_key *key = NULL; | |
372 | ||
ad0e2b5a JB |
373 | assert_key_lock(sdata->local); |
374 | ||
3cfcf6ac JM |
375 | if (idx >= NUM_DEFAULT_KEYS && |
376 | idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) | |
40b275b6 | 377 | key = key_mtx_dereference(sdata->local, sdata->keys[idx]); |
3cfcf6ac JM |
378 | |
379 | rcu_assign_pointer(sdata->default_mgmt_key, key); | |
380 | ||
f7e0104c | 381 | ieee80211_debugfs_key_update_default(sdata); |
3cfcf6ac JM |
382 | } |
383 | ||
384 | void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, | |
385 | int idx) | |
386 | { | |
ad0e2b5a | 387 | mutex_lock(&sdata->local->key_mtx); |
3cfcf6ac | 388 | __ieee80211_set_default_mgmt_key(sdata, idx); |
ad0e2b5a | 389 | mutex_unlock(&sdata->local->key_mtx); |
3cfcf6ac JM |
390 | } |
391 | ||
62872a9b | 392 | static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, |
3b8d9c29 JB |
393 | struct sta_info *sta, |
394 | bool pairwise, | |
395 | struct ieee80211_key *old, | |
396 | struct ieee80211_key *new) | |
3b96766f | 397 | { |
f7e0104c | 398 | int idx; |
90cc4bd6 | 399 | int ret = 0; |
f7e0104c | 400 | bool defunikey, defmultikey, defmgmtkey; |
3b96766f | 401 | |
5282c3ba JB |
402 | /* caller must provide at least one old/new */ |
403 | if (WARN_ON(!new && !old)) | |
62872a9b | 404 | return 0; |
5282c3ba | 405 | |
3b96766f | 406 | if (new) |
ef044763 | 407 | list_add_tail_rcu(&new->list, &sdata->key_list); |
3b96766f | 408 | |
2475b1cc | 409 | WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); |
3b96766f | 410 | |
90cc4bd6 AW |
411 | if (new && sta && pairwise) { |
412 | /* Unicast rekey needs special handling. With Extended Key ID | |
413 | * old is still NULL for the first rekey. | |
414 | */ | |
415 | ieee80211_pairwise_rekey(old, new); | |
416 | } | |
417 | ||
62872a9b | 418 | if (old) { |
2475b1cc | 419 | idx = old->conf.keyidx; |
90cc4bd6 AW |
420 | |
421 | if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { | |
422 | ieee80211_key_disable_hw_accel(old); | |
423 | ||
424 | if (new) | |
425 | ret = ieee80211_key_enable_hw_accel(new); | |
426 | } | |
62872a9b | 427 | } else { |
40b5a0f8 | 428 | /* new must be provided in case old is not */ |
2475b1cc | 429 | idx = new->conf.keyidx; |
40b5a0f8 | 430 | if (!new->local->wowlan) |
62872a9b | 431 | ret = ieee80211_key_enable_hw_accel(new); |
62872a9b AW |
432 | } |
433 | ||
434 | if (ret) | |
435 | return ret; | |
3b96766f | 436 | |
2475b1cc MS |
437 | if (sta) { |
438 | if (pairwise) { | |
439 | rcu_assign_pointer(sta->ptk[idx], new); | |
96fc6efb AW |
440 | if (new && |
441 | !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) { | |
442 | sta->ptk_idx = idx; | |
62872a9b AW |
443 | clear_sta_flag(sta, WLAN_STA_BLOCK_BA); |
444 | ieee80211_check_fast_xmit(sta); | |
445 | } | |
2475b1cc MS |
446 | } else { |
447 | rcu_assign_pointer(sta->gtk[idx], new); | |
2475b1cc | 448 | } |
96fc6efb AW |
449 | /* Only needed for transition from no key -> key. |
450 | * Still triggers unnecessary when using Extended Key ID | |
451 | * and installing the second key ID the first time. | |
452 | */ | |
453 | if (new && !old) | |
62872a9b | 454 | ieee80211_check_fast_rx(sta); |
2475b1cc | 455 | } else { |
40b275b6 JB |
456 | defunikey = old && |
457 | old == key_mtx_dereference(sdata->local, | |
458 | sdata->default_unicast_key); | |
459 | defmultikey = old && | |
460 | old == key_mtx_dereference(sdata->local, | |
461 | sdata->default_multicast_key); | |
462 | defmgmtkey = old && | |
463 | old == key_mtx_dereference(sdata->local, | |
464 | sdata->default_mgmt_key); | |
3b96766f | 465 | |
f7e0104c JB |
466 | if (defunikey && !new) |
467 | __ieee80211_set_default_key(sdata, -1, true, false); | |
468 | if (defmultikey && !new) | |
469 | __ieee80211_set_default_key(sdata, -1, false, true); | |
3cfcf6ac JM |
470 | if (defmgmtkey && !new) |
471 | __ieee80211_set_default_mgmt_key(sdata, -1); | |
3b96766f JB |
472 | |
473 | rcu_assign_pointer(sdata->keys[idx], new); | |
f7e0104c JB |
474 | if (defunikey && new) |
475 | __ieee80211_set_default_key(sdata, new->conf.keyidx, | |
476 | true, false); | |
477 | if (defmultikey && new) | |
478 | __ieee80211_set_default_key(sdata, new->conf.keyidx, | |
479 | false, true); | |
3cfcf6ac JM |
480 | if (defmgmtkey && new) |
481 | __ieee80211_set_default_mgmt_key(sdata, | |
482 | new->conf.keyidx); | |
3b96766f JB |
483 | } |
484 | ||
b5c34f66 | 485 | if (old) |
ef044763 | 486 | list_del_rcu(&old->list); |
62872a9b AW |
487 | |
488 | return 0; | |
11a843b7 JB |
489 | } |
490 | ||
2475b1cc MS |
491 | struct ieee80211_key * |
492 | ieee80211_key_alloc(u32 cipher, int idx, size_t key_len, | |
493 | const u8 *key_data, | |
494 | size_t seq_len, const u8 *seq, | |
495 | const struct ieee80211_cipher_scheme *cs) | |
1f5a7e47 JB |
496 | { |
497 | struct ieee80211_key *key; | |
1ac62ba7 | 498 | int i, j, err; |
1f5a7e47 | 499 | |
8c5bb1fa JB |
500 | if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)) |
501 | return ERR_PTR(-EINVAL); | |
11a843b7 JB |
502 | |
503 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); | |
1f5a7e47 | 504 | if (!key) |
1ac62ba7 | 505 | return ERR_PTR(-ENOMEM); |
11a843b7 JB |
506 | |
507 | /* | |
508 | * Default to software encryption; we'll later upload the | |
509 | * key to the hardware if possible. | |
510 | */ | |
11a843b7 JB |
511 | key->conf.flags = 0; |
512 | key->flags = 0; | |
513 | ||
97359d12 | 514 | key->conf.cipher = cipher; |
11a843b7 JB |
515 | key->conf.keyidx = idx; |
516 | key->conf.keylen = key_len; | |
97359d12 JB |
517 | switch (cipher) { |
518 | case WLAN_CIPHER_SUITE_WEP40: | |
519 | case WLAN_CIPHER_SUITE_WEP104: | |
4325f6ca JB |
520 | key->conf.iv_len = IEEE80211_WEP_IV_LEN; |
521 | key->conf.icv_len = IEEE80211_WEP_ICV_LEN; | |
76708dee | 522 | break; |
97359d12 | 523 | case WLAN_CIPHER_SUITE_TKIP: |
4325f6ca JB |
524 | key->conf.iv_len = IEEE80211_TKIP_IV_LEN; |
525 | key->conf.icv_len = IEEE80211_TKIP_ICV_LEN; | |
9f26a952 | 526 | if (seq) { |
5a306f58 | 527 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { |
faa8fdc8 JM |
528 | key->u.tkip.rx[i].iv32 = |
529 | get_unaligned_le32(&seq[2]); | |
530 | key->u.tkip.rx[i].iv16 = | |
531 | get_unaligned_le16(seq); | |
532 | } | |
533 | } | |
523b02ea | 534 | spin_lock_init(&key->u.tkip.txlock); |
76708dee | 535 | break; |
97359d12 | 536 | case WLAN_CIPHER_SUITE_CCMP: |
4325f6ca JB |
537 | key->conf.iv_len = IEEE80211_CCMP_HDR_LEN; |
538 | key->conf.icv_len = IEEE80211_CCMP_MIC_LEN; | |
9f26a952 | 539 | if (seq) { |
5a306f58 | 540 | for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) |
4325f6ca | 541 | for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++) |
faa8fdc8 | 542 | key->u.ccmp.rx_pn[i][j] = |
4325f6ca | 543 | seq[IEEE80211_CCMP_PN_LEN - j - 1]; |
faa8fdc8 | 544 | } |
11a843b7 JB |
545 | /* |
546 | * Initialize AES key state here as an optimization so that | |
547 | * it does not need to be initialized for every packet. | |
548 | */ | |
2b2ba0db JM |
549 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( |
550 | key_data, key_len, IEEE80211_CCMP_MIC_LEN); | |
551 | if (IS_ERR(key->u.ccmp.tfm)) { | |
552 | err = PTR_ERR(key->u.ccmp.tfm); | |
553 | kfree(key); | |
554 | return ERR_PTR(err); | |
555 | } | |
556 | break; | |
557 | case WLAN_CIPHER_SUITE_CCMP_256: | |
558 | key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN; | |
559 | key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN; | |
560 | for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) | |
561 | for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++) | |
562 | key->u.ccmp.rx_pn[i][j] = | |
563 | seq[IEEE80211_CCMP_256_PN_LEN - j - 1]; | |
564 | /* Initialize AES key state here as an optimization so that | |
565 | * it does not need to be initialized for every packet. | |
566 | */ | |
567 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( | |
568 | key_data, key_len, IEEE80211_CCMP_256_MIC_LEN); | |
1ac62ba7 BH |
569 | if (IS_ERR(key->u.ccmp.tfm)) { |
570 | err = PTR_ERR(key->u.ccmp.tfm); | |
3b96766f | 571 | kfree(key); |
1f951a7f | 572 | return ERR_PTR(err); |
11a843b7 | 573 | } |
60ae0f20 JB |
574 | break; |
575 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
56c52da2 | 576 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
60ae0f20 | 577 | key->conf.iv_len = 0; |
56c52da2 JM |
578 | if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) |
579 | key->conf.icv_len = sizeof(struct ieee80211_mmie); | |
580 | else | |
581 | key->conf.icv_len = sizeof(struct ieee80211_mmie_16); | |
60ae0f20 | 582 | if (seq) |
4325f6ca | 583 | for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++) |
0f927323 | 584 | key->u.aes_cmac.rx_pn[j] = |
4325f6ca | 585 | seq[IEEE80211_CMAC_PN_LEN - j - 1]; |
3cfcf6ac JM |
586 | /* |
587 | * Initialize AES key state here as an optimization so that | |
588 | * it does not need to be initialized for every packet. | |
589 | */ | |
590 | key->u.aes_cmac.tfm = | |
56c52da2 | 591 | ieee80211_aes_cmac_key_setup(key_data, key_len); |
1ac62ba7 BH |
592 | if (IS_ERR(key->u.aes_cmac.tfm)) { |
593 | err = PTR_ERR(key->u.aes_cmac.tfm); | |
3cfcf6ac | 594 | kfree(key); |
1f951a7f | 595 | return ERR_PTR(err); |
3cfcf6ac | 596 | } |
60ae0f20 | 597 | break; |
8ade538b JM |
598 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
599 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: | |
600 | key->conf.iv_len = 0; | |
601 | key->conf.icv_len = sizeof(struct ieee80211_mmie_16); | |
602 | if (seq) | |
603 | for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++) | |
604 | key->u.aes_gmac.rx_pn[j] = | |
605 | seq[IEEE80211_GMAC_PN_LEN - j - 1]; | |
606 | /* Initialize AES key state here as an optimization so that | |
607 | * it does not need to be initialized for every packet. | |
608 | */ | |
609 | key->u.aes_gmac.tfm = | |
610 | ieee80211_aes_gmac_key_setup(key_data, key_len); | |
611 | if (IS_ERR(key->u.aes_gmac.tfm)) { | |
612 | err = PTR_ERR(key->u.aes_gmac.tfm); | |
613 | kfree(key); | |
614 | return ERR_PTR(err); | |
615 | } | |
616 | break; | |
00b9cfa3 JM |
617 | case WLAN_CIPHER_SUITE_GCMP: |
618 | case WLAN_CIPHER_SUITE_GCMP_256: | |
619 | key->conf.iv_len = IEEE80211_GCMP_HDR_LEN; | |
620 | key->conf.icv_len = IEEE80211_GCMP_MIC_LEN; | |
621 | for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) | |
622 | for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++) | |
623 | key->u.gcmp.rx_pn[i][j] = | |
624 | seq[IEEE80211_GCMP_PN_LEN - j - 1]; | |
625 | /* Initialize AES key state here as an optimization so that | |
626 | * it does not need to be initialized for every packet. | |
627 | */ | |
628 | key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data, | |
629 | key_len); | |
630 | if (IS_ERR(key->u.gcmp.tfm)) { | |
631 | err = PTR_ERR(key->u.gcmp.tfm); | |
632 | kfree(key); | |
633 | return ERR_PTR(err); | |
634 | } | |
635 | break; | |
2475b1cc MS |
636 | default: |
637 | if (cs) { | |
e3a55b53 JB |
638 | if (seq_len && seq_len != cs->pn_len) { |
639 | kfree(key); | |
640 | return ERR_PTR(-EINVAL); | |
641 | } | |
2475b1cc MS |
642 | |
643 | key->conf.iv_len = cs->hdr_len; | |
644 | key->conf.icv_len = cs->mic_len; | |
645 | for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) | |
e3a55b53 | 646 | for (j = 0; j < seq_len; j++) |
2475b1cc | 647 | key->u.gen.rx_pn[i][j] = |
e3a55b53 | 648 | seq[seq_len - j - 1]; |
c7ef38e0 | 649 | key->flags |= KEY_FLAG_CIPHER_SCHEME; |
2475b1cc | 650 | } |
3cfcf6ac | 651 | } |
60ae0f20 JB |
652 | memcpy(key->conf.key, key_data, key_len); |
653 | INIT_LIST_HEAD(&key->list); | |
3cfcf6ac | 654 | |
db4d1169 JB |
655 | return key; |
656 | } | |
11a843b7 | 657 | |
79cf2dfa JB |
658 | static void ieee80211_key_free_common(struct ieee80211_key *key) |
659 | { | |
00b9cfa3 JM |
660 | switch (key->conf.cipher) { |
661 | case WLAN_CIPHER_SUITE_CCMP: | |
2b2ba0db | 662 | case WLAN_CIPHER_SUITE_CCMP_256: |
79cf2dfa | 663 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
00b9cfa3 JM |
664 | break; |
665 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
56c52da2 | 666 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
79cf2dfa | 667 | ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm); |
00b9cfa3 | 668 | break; |
8ade538b JM |
669 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
670 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: | |
671 | ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm); | |
672 | break; | |
00b9cfa3 JM |
673 | case WLAN_CIPHER_SUITE_GCMP: |
674 | case WLAN_CIPHER_SUITE_GCMP_256: | |
675 | ieee80211_aes_gcm_key_free(key->u.gcmp.tfm); | |
676 | break; | |
677 | } | |
29c3f9c3 | 678 | kzfree(key); |
79cf2dfa JB |
679 | } |
680 | ||
6d10e46b JB |
681 | static void __ieee80211_key_destroy(struct ieee80211_key *key, |
682 | bool delay_tailroom) | |
ad0e2b5a | 683 | { |
3bff1865 | 684 | if (key->local) { |
8d1f7ecd JB |
685 | struct ieee80211_sub_if_data *sdata = key->sdata; |
686 | ||
32162a4d | 687 | ieee80211_debugfs_key_remove(key); |
8d1f7ecd JB |
688 | |
689 | if (delay_tailroom) { | |
690 | /* see ieee80211_delayed_tailroom_dec */ | |
691 | sdata->crypto_tx_tailroom_pending_dec++; | |
692 | schedule_delayed_work(&sdata->dec_tailroom_needed_wk, | |
693 | HZ/2); | |
694 | } else { | |
f9dca80b | 695 | decrease_tailroom_need_count(sdata, 1); |
8d1f7ecd | 696 | } |
3bff1865 | 697 | } |
ad0e2b5a | 698 | |
79cf2dfa JB |
699 | ieee80211_key_free_common(key); |
700 | } | |
701 | ||
6d10e46b JB |
702 | static void ieee80211_key_destroy(struct ieee80211_key *key, |
703 | bool delay_tailroom) | |
704 | { | |
705 | if (!key) | |
706 | return; | |
707 | ||
708 | /* | |
ef044763 EP |
709 | * Synchronize so the TX path and rcu key iterators |
710 | * can no longer be using this key before we free/remove it. | |
6d10e46b JB |
711 | */ |
712 | synchronize_net(); | |
713 | ||
714 | __ieee80211_key_destroy(key, delay_tailroom); | |
715 | } | |
716 | ||
79cf2dfa JB |
717 | void ieee80211_key_free_unused(struct ieee80211_key *key) |
718 | { | |
719 | WARN_ON(key->sdata || key->local); | |
720 | ieee80211_key_free_common(key); | |
ad0e2b5a JB |
721 | } |
722 | ||
cfbb0d90 JB |
723 | static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata, |
724 | struct ieee80211_key *old, | |
725 | struct ieee80211_key *new) | |
726 | { | |
727 | u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP]; | |
728 | u8 *tk_old, *tk_new; | |
729 | ||
730 | if (!old || new->conf.keylen != old->conf.keylen) | |
731 | return false; | |
732 | ||
733 | tk_old = old->conf.key; | |
734 | tk_new = new->conf.key; | |
735 | ||
736 | /* | |
737 | * In station mode, don't compare the TX MIC key, as it's never used | |
738 | * and offloaded rekeying may not care to send it to the host. This | |
739 | * is the case in iwlwifi, for example. | |
740 | */ | |
741 | if (sdata->vif.type == NL80211_IFTYPE_STATION && | |
742 | new->conf.cipher == WLAN_CIPHER_SUITE_TKIP && | |
743 | new->conf.keylen == WLAN_KEY_LEN_TKIP && | |
744 | !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { | |
745 | memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP); | |
746 | memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP); | |
747 | memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); | |
748 | memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); | |
749 | tk_old = tkip_old; | |
750 | tk_new = tkip_new; | |
751 | } | |
752 | ||
753 | return !crypto_memneq(tk_old, tk_new, new->conf.keylen); | |
754 | } | |
755 | ||
3ffc2a90 JB |
756 | int ieee80211_key_link(struct ieee80211_key *key, |
757 | struct ieee80211_sub_if_data *sdata, | |
758 | struct sta_info *sta) | |
db4d1169 JB |
759 | { |
760 | struct ieee80211_key *old_key; | |
133bf90d MP |
761 | int idx = key->conf.keyidx; |
762 | bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE; | |
763 | /* | |
764 | * We want to delay tailroom updates only for station - in that | |
765 | * case it helps roaming speed, but in other cases it hurts and | |
766 | * can cause warnings to appear. | |
767 | */ | |
768 | bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION; | |
96fc6efb | 769 | int ret = -EOPNOTSUPP; |
db4d1169 | 770 | |
ad0e2b5a | 771 | mutex_lock(&sdata->local->key_mtx); |
3b96766f | 772 | |
96fc6efb AW |
773 | if (sta && pairwise) { |
774 | struct ieee80211_key *alt_key; | |
775 | ||
2475b1cc | 776 | old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]); |
96fc6efb AW |
777 | alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]); |
778 | ||
779 | /* The rekey code assumes that the old and new key are using | |
780 | * the same cipher. Enforce the assumption for pairwise keys. | |
781 | */ | |
782 | if (key && | |
783 | ((alt_key && alt_key->conf.cipher != key->conf.cipher) || | |
784 | (old_key && old_key->conf.cipher != key->conf.cipher))) | |
785 | goto out; | |
786 | } else if (sta) { | |
40b275b6 | 787 | old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]); |
96fc6efb | 788 | } else { |
40b275b6 | 789 | old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]); |
96fc6efb AW |
790 | } |
791 | ||
792 | /* Non-pairwise keys must also not switch the cipher on rekey */ | |
793 | if (!pairwise) { | |
794 | if (key && old_key && old_key->conf.cipher != key->conf.cipher) | |
795 | goto out; | |
796 | } | |
db4d1169 | 797 | |
fdf7cb41 JB |
798 | /* |
799 | * Silently accept key re-installation without really installing the | |
800 | * new version of the key to avoid nonce reuse or replay issues. | |
801 | */ | |
cfbb0d90 | 802 | if (ieee80211_key_identical(sdata, old_key, key)) { |
fdf7cb41 JB |
803 | ieee80211_key_free_unused(key); |
804 | ret = 0; | |
805 | goto out; | |
806 | } | |
807 | ||
808 | key->local = sdata->local; | |
809 | key->sdata = sdata; | |
810 | key->sta = sta; | |
811 | ||
3bff1865 YAP |
812 | increment_tailroom_need_count(sdata); |
813 | ||
62872a9b | 814 | ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key); |
db4d1169 | 815 | |
62872a9b AW |
816 | if (!ret) { |
817 | ieee80211_debugfs_key_add(key); | |
818 | ieee80211_key_destroy(old_key, delay_tailroom); | |
27b3eb9c | 819 | } else { |
62872a9b | 820 | ieee80211_key_free(key, delay_tailroom); |
27b3eb9c | 821 | } |
79cf2dfa | 822 | |
fdf7cb41 | 823 | out: |
ad0e2b5a | 824 | mutex_unlock(&sdata->local->key_mtx); |
3ffc2a90 JB |
825 | |
826 | return ret; | |
1f5a7e47 JB |
827 | } |
828 | ||
3b8d9c29 | 829 | void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom) |
1f5a7e47 | 830 | { |
5c0c3641 JB |
831 | if (!key) |
832 | return; | |
833 | ||
3b96766f JB |
834 | /* |
835 | * Replace key with nothingness if it was ever used. | |
836 | */ | |
3a245766 | 837 | if (key->sdata) |
3b8d9c29 | 838 | ieee80211_key_replace(key->sdata, key->sta, |
e31b8213 JB |
839 | key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, |
840 | key, NULL); | |
3b8d9c29 | 841 | ieee80211_key_destroy(key, delay_tailroom); |
3b96766f | 842 | } |
d4e46a3d | 843 | |
ad0e2b5a | 844 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
3a245766 JB |
845 | { |
846 | struct ieee80211_key *key; | |
f9dca80b | 847 | struct ieee80211_sub_if_data *vlan; |
11a843b7 | 848 | |
3a245766 | 849 | ASSERT_RTNL(); |
11a843b7 | 850 | |
9607e6b6 | 851 | if (WARN_ON(!ieee80211_sdata_running(sdata))) |
3a245766 | 852 | return; |
11a843b7 | 853 | |
ad0e2b5a | 854 | mutex_lock(&sdata->local->key_mtx); |
11a843b7 | 855 | |
f9dca80b MK |
856 | WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt || |
857 | sdata->crypto_tx_tailroom_pending_dec); | |
858 | ||
859 | if (sdata->vif.type == NL80211_IFTYPE_AP) { | |
860 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) | |
861 | WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt || | |
862 | vlan->crypto_tx_tailroom_pending_dec); | |
863 | } | |
3bff1865 YAP |
864 | |
865 | list_for_each_entry(key, &sdata->key_list, list) { | |
866 | increment_tailroom_need_count(sdata); | |
ad0e2b5a | 867 | ieee80211_key_enable_hw_accel(key); |
3bff1865 | 868 | } |
3b96766f | 869 | |
ad0e2b5a | 870 | mutex_unlock(&sdata->local->key_mtx); |
11a843b7 JB |
871 | } |
872 | ||
f9dca80b MK |
873 | void ieee80211_reset_crypto_tx_tailroom(struct ieee80211_sub_if_data *sdata) |
874 | { | |
875 | struct ieee80211_sub_if_data *vlan; | |
876 | ||
877 | mutex_lock(&sdata->local->key_mtx); | |
878 | ||
879 | sdata->crypto_tx_tailroom_needed_cnt = 0; | |
880 | ||
881 | if (sdata->vif.type == NL80211_IFTYPE_AP) { | |
882 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) | |
883 | vlan->crypto_tx_tailroom_needed_cnt = 0; | |
884 | } | |
885 | ||
886 | mutex_unlock(&sdata->local->key_mtx); | |
887 | } | |
888 | ||
830af02f JB |
889 | void ieee80211_iter_keys(struct ieee80211_hw *hw, |
890 | struct ieee80211_vif *vif, | |
891 | void (*iter)(struct ieee80211_hw *hw, | |
892 | struct ieee80211_vif *vif, | |
893 | struct ieee80211_sta *sta, | |
894 | struct ieee80211_key_conf *key, | |
895 | void *data), | |
896 | void *iter_data) | |
897 | { | |
898 | struct ieee80211_local *local = hw_to_local(hw); | |
27b3eb9c | 899 | struct ieee80211_key *key, *tmp; |
830af02f JB |
900 | struct ieee80211_sub_if_data *sdata; |
901 | ||
902 | ASSERT_RTNL(); | |
903 | ||
904 | mutex_lock(&local->key_mtx); | |
905 | if (vif) { | |
906 | sdata = vif_to_sdata(vif); | |
27b3eb9c | 907 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
830af02f JB |
908 | iter(hw, &sdata->vif, |
909 | key->sta ? &key->sta->sta : NULL, | |
910 | &key->conf, iter_data); | |
911 | } else { | |
912 | list_for_each_entry(sdata, &local->interfaces, list) | |
27b3eb9c JB |
913 | list_for_each_entry_safe(key, tmp, |
914 | &sdata->key_list, list) | |
830af02f JB |
915 | iter(hw, &sdata->vif, |
916 | key->sta ? &key->sta->sta : NULL, | |
917 | &key->conf, iter_data); | |
918 | } | |
919 | mutex_unlock(&local->key_mtx); | |
920 | } | |
921 | EXPORT_SYMBOL(ieee80211_iter_keys); | |
922 | ||
ef044763 EP |
923 | static void |
924 | _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, | |
925 | struct ieee80211_sub_if_data *sdata, | |
926 | void (*iter)(struct ieee80211_hw *hw, | |
927 | struct ieee80211_vif *vif, | |
928 | struct ieee80211_sta *sta, | |
929 | struct ieee80211_key_conf *key, | |
930 | void *data), | |
931 | void *iter_data) | |
932 | { | |
933 | struct ieee80211_key *key; | |
934 | ||
935 | list_for_each_entry_rcu(key, &sdata->key_list, list) { | |
936 | /* skip keys of station in removal process */ | |
937 | if (key->sta && key->sta->removed) | |
938 | continue; | |
939 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) | |
940 | continue; | |
941 | ||
942 | iter(hw, &sdata->vif, | |
943 | key->sta ? &key->sta->sta : NULL, | |
944 | &key->conf, iter_data); | |
945 | } | |
946 | } | |
947 | ||
948 | void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, | |
949 | struct ieee80211_vif *vif, | |
950 | void (*iter)(struct ieee80211_hw *hw, | |
951 | struct ieee80211_vif *vif, | |
952 | struct ieee80211_sta *sta, | |
953 | struct ieee80211_key_conf *key, | |
954 | void *data), | |
955 | void *iter_data) | |
956 | { | |
957 | struct ieee80211_local *local = hw_to_local(hw); | |
958 | struct ieee80211_sub_if_data *sdata; | |
959 | ||
960 | if (vif) { | |
961 | sdata = vif_to_sdata(vif); | |
962 | _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); | |
963 | } else { | |
964 | list_for_each_entry_rcu(sdata, &local->interfaces, list) | |
965 | _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); | |
966 | } | |
967 | } | |
968 | EXPORT_SYMBOL(ieee80211_iter_keys_rcu); | |
969 | ||
7907c7d3 JB |
970 | static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata, |
971 | struct list_head *keys) | |
3b96766f JB |
972 | { |
973 | struct ieee80211_key *key, *tmp; | |
3b96766f | 974 | |
f9dca80b MK |
975 | decrease_tailroom_need_count(sdata, |
976 | sdata->crypto_tx_tailroom_pending_dec); | |
8d1f7ecd JB |
977 | sdata->crypto_tx_tailroom_pending_dec = 0; |
978 | ||
3cfcf6ac | 979 | ieee80211_debugfs_key_remove_mgmt_default(sdata); |
3b96766f | 980 | |
6d10e46b JB |
981 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) { |
982 | ieee80211_key_replace(key->sdata, key->sta, | |
983 | key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, | |
984 | key, NULL); | |
7907c7d3 | 985 | list_add_tail(&key->list, keys); |
6d10e46b | 986 | } |
3b96766f | 987 | |
f7e0104c | 988 | ieee80211_debugfs_key_update_default(sdata); |
7907c7d3 | 989 | } |
f7e0104c | 990 | |
7907c7d3 JB |
991 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, |
992 | bool force_synchronize) | |
993 | { | |
994 | struct ieee80211_local *local = sdata->local; | |
995 | struct ieee80211_sub_if_data *vlan; | |
f9dca80b | 996 | struct ieee80211_sub_if_data *master; |
7907c7d3 JB |
997 | struct ieee80211_key *key, *tmp; |
998 | LIST_HEAD(keys); | |
999 | ||
1000 | cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk); | |
1001 | ||
1002 | mutex_lock(&local->key_mtx); | |
1003 | ||
1004 | ieee80211_free_keys_iface(sdata, &keys); | |
1005 | ||
1006 | if (sdata->vif.type == NL80211_IFTYPE_AP) { | |
1007 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) | |
1008 | ieee80211_free_keys_iface(vlan, &keys); | |
6d10e46b JB |
1009 | } |
1010 | ||
7907c7d3 JB |
1011 | if (!list_empty(&keys) || force_synchronize) |
1012 | synchronize_net(); | |
1013 | list_for_each_entry_safe(key, tmp, &keys, list) | |
1014 | __ieee80211_key_destroy(key, false); | |
1015 | ||
f9dca80b MK |
1016 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { |
1017 | if (sdata->bss) { | |
1018 | master = container_of(sdata->bss, | |
1019 | struct ieee80211_sub_if_data, | |
1020 | u.ap); | |
1021 | ||
1022 | WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt != | |
1023 | master->crypto_tx_tailroom_needed_cnt); | |
1024 | } | |
1025 | } else { | |
1026 | WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt || | |
1027 | sdata->crypto_tx_tailroom_pending_dec); | |
1028 | } | |
1029 | ||
7907c7d3 JB |
1030 | if (sdata->vif.type == NL80211_IFTYPE_AP) { |
1031 | list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) | |
1032 | WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt || | |
1033 | vlan->crypto_tx_tailroom_pending_dec); | |
1034 | } | |
8d1f7ecd | 1035 | |
7907c7d3 | 1036 | mutex_unlock(&local->key_mtx); |
11a843b7 | 1037 | } |
c68f4b89 | 1038 | |
6d10e46b JB |
1039 | void ieee80211_free_sta_keys(struct ieee80211_local *local, |
1040 | struct sta_info *sta) | |
1041 | { | |
c8782078 | 1042 | struct ieee80211_key *key; |
6d10e46b JB |
1043 | int i; |
1044 | ||
1045 | mutex_lock(&local->key_mtx); | |
28a9bc68 | 1046 | for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) { |
6d10e46b JB |
1047 | key = key_mtx_dereference(local, sta->gtk[i]); |
1048 | if (!key) | |
1049 | continue; | |
1050 | ieee80211_key_replace(key->sdata, key->sta, | |
1051 | key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, | |
1052 | key, NULL); | |
133bf90d MP |
1053 | __ieee80211_key_destroy(key, key->sdata->vif.type == |
1054 | NL80211_IFTYPE_STATION); | |
6d10e46b JB |
1055 | } |
1056 | ||
2475b1cc MS |
1057 | for (i = 0; i < NUM_DEFAULT_KEYS; i++) { |
1058 | key = key_mtx_dereference(local, sta->ptk[i]); | |
1059 | if (!key) | |
1060 | continue; | |
6d10e46b JB |
1061 | ieee80211_key_replace(key->sdata, key->sta, |
1062 | key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, | |
1063 | key, NULL); | |
133bf90d MP |
1064 | __ieee80211_key_destroy(key, key->sdata->vif.type == |
1065 | NL80211_IFTYPE_STATION); | |
c8782078 | 1066 | } |
6d10e46b JB |
1067 | |
1068 | mutex_unlock(&local->key_mtx); | |
1069 | } | |
1070 | ||
8d1f7ecd JB |
1071 | void ieee80211_delayed_tailroom_dec(struct work_struct *wk) |
1072 | { | |
1073 | struct ieee80211_sub_if_data *sdata; | |
1074 | ||
1075 | sdata = container_of(wk, struct ieee80211_sub_if_data, | |
1076 | dec_tailroom_needed_wk.work); | |
1077 | ||
1078 | /* | |
1079 | * The reason for the delayed tailroom needed decrementing is to | |
1080 | * make roaming faster: during roaming, all keys are first deleted | |
1081 | * and then new keys are installed. The first new key causes the | |
1082 | * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes | |
1083 | * the cost of synchronize_net() (which can be slow). Avoid this | |
1084 | * by deferring the crypto_tx_tailroom_needed_cnt decrementing on | |
1085 | * key removal for a while, so if we roam the value is larger than | |
1086 | * zero and no 0->1 transition happens. | |
1087 | * | |
1088 | * The cost is that if the AP switching was from an AP with keys | |
1089 | * to one without, we still allocate tailroom while it would no | |
1090 | * longer be needed. However, in the typical (fast) roaming case | |
1091 | * within an ESS this usually won't happen. | |
1092 | */ | |
1093 | ||
1094 | mutex_lock(&sdata->local->key_mtx); | |
f9dca80b MK |
1095 | decrease_tailroom_need_count(sdata, |
1096 | sdata->crypto_tx_tailroom_pending_dec); | |
8d1f7ecd JB |
1097 | sdata->crypto_tx_tailroom_pending_dec = 0; |
1098 | mutex_unlock(&sdata->local->key_mtx); | |
1099 | } | |
c68f4b89 JB |
1100 | |
1101 | void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid, | |
1102 | const u8 *replay_ctr, gfp_t gfp) | |
1103 | { | |
1104 | struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); | |
1105 | ||
1106 | trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr); | |
1107 | ||
1108 | cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp); | |
1109 | } | |
1110 | EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify); | |
3ea542d3 | 1111 | |
3ea542d3 JB |
1112 | void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf, |
1113 | int tid, struct ieee80211_key_seq *seq) | |
1114 | { | |
1115 | struct ieee80211_key *key; | |
1116 | const u8 *pn; | |
1117 | ||
1118 | key = container_of(keyconf, struct ieee80211_key, conf); | |
1119 | ||
1120 | switch (key->conf.cipher) { | |
1121 | case WLAN_CIPHER_SUITE_TKIP: | |
5a306f58 | 1122 | if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) |
3ea542d3 JB |
1123 | return; |
1124 | seq->tkip.iv32 = key->u.tkip.rx[tid].iv32; | |
1125 | seq->tkip.iv16 = key->u.tkip.rx[tid].iv16; | |
1126 | break; | |
1127 | case WLAN_CIPHER_SUITE_CCMP: | |
2b2ba0db | 1128 | case WLAN_CIPHER_SUITE_CCMP_256: |
5a306f58 | 1129 | if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) |
3ea542d3 JB |
1130 | return; |
1131 | if (tid < 0) | |
5a306f58 | 1132 | pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; |
3ea542d3 JB |
1133 | else |
1134 | pn = key->u.ccmp.rx_pn[tid]; | |
4325f6ca | 1135 | memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN); |
3ea542d3 JB |
1136 | break; |
1137 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
56c52da2 | 1138 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
3ea542d3 JB |
1139 | if (WARN_ON(tid != 0)) |
1140 | return; | |
1141 | pn = key->u.aes_cmac.rx_pn; | |
4325f6ca | 1142 | memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN); |
3ea542d3 | 1143 | break; |
8ade538b JM |
1144 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
1145 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: | |
1146 | if (WARN_ON(tid != 0)) | |
1147 | return; | |
1148 | pn = key->u.aes_gmac.rx_pn; | |
1149 | memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN); | |
1150 | break; | |
00b9cfa3 JM |
1151 | case WLAN_CIPHER_SUITE_GCMP: |
1152 | case WLAN_CIPHER_SUITE_GCMP_256: | |
1153 | if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) | |
1154 | return; | |
1155 | if (tid < 0) | |
1156 | pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; | |
1157 | else | |
1158 | pn = key->u.gcmp.rx_pn[tid]; | |
1159 | memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN); | |
1160 | break; | |
3ea542d3 JB |
1161 | } |
1162 | } | |
1163 | EXPORT_SYMBOL(ieee80211_get_key_rx_seq); | |
27b3eb9c | 1164 | |
27b3eb9c JB |
1165 | void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf, |
1166 | int tid, struct ieee80211_key_seq *seq) | |
1167 | { | |
1168 | struct ieee80211_key *key; | |
1169 | u8 *pn; | |
1170 | ||
1171 | key = container_of(keyconf, struct ieee80211_key, conf); | |
1172 | ||
1173 | switch (key->conf.cipher) { | |
1174 | case WLAN_CIPHER_SUITE_TKIP: | |
1175 | if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) | |
1176 | return; | |
1177 | key->u.tkip.rx[tid].iv32 = seq->tkip.iv32; | |
1178 | key->u.tkip.rx[tid].iv16 = seq->tkip.iv16; | |
1179 | break; | |
1180 | case WLAN_CIPHER_SUITE_CCMP: | |
2b2ba0db | 1181 | case WLAN_CIPHER_SUITE_CCMP_256: |
27b3eb9c JB |
1182 | if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) |
1183 | return; | |
1184 | if (tid < 0) | |
1185 | pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; | |
1186 | else | |
1187 | pn = key->u.ccmp.rx_pn[tid]; | |
1188 | memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN); | |
1189 | break; | |
1190 | case WLAN_CIPHER_SUITE_AES_CMAC: | |
56c52da2 | 1191 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
27b3eb9c JB |
1192 | if (WARN_ON(tid != 0)) |
1193 | return; | |
1194 | pn = key->u.aes_cmac.rx_pn; | |
1195 | memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN); | |
1196 | break; | |
8ade538b JM |
1197 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
1198 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: | |
1199 | if (WARN_ON(tid != 0)) | |
1200 | return; | |
1201 | pn = key->u.aes_gmac.rx_pn; | |
1202 | memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN); | |
1203 | break; | |
00b9cfa3 JM |
1204 | case WLAN_CIPHER_SUITE_GCMP: |
1205 | case WLAN_CIPHER_SUITE_GCMP_256: | |
1206 | if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) | |
1207 | return; | |
1208 | if (tid < 0) | |
1209 | pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; | |
1210 | else | |
1211 | pn = key->u.gcmp.rx_pn[tid]; | |
1212 | memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN); | |
1213 | break; | |
27b3eb9c JB |
1214 | default: |
1215 | WARN_ON(1); | |
1216 | break; | |
1217 | } | |
1218 | } | |
1219 | EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq); | |
1220 | ||
1221 | void ieee80211_remove_key(struct ieee80211_key_conf *keyconf) | |
1222 | { | |
1223 | struct ieee80211_key *key; | |
1224 | ||
1225 | key = container_of(keyconf, struct ieee80211_key, conf); | |
1226 | ||
1227 | assert_key_lock(key->local); | |
1228 | ||
1229 | /* | |
1230 | * if key was uploaded, we assume the driver will/has remove(d) | |
1231 | * it, so adjust bookkeeping accordingly | |
1232 | */ | |
1233 | if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { | |
1234 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; | |
1235 | ||
092c4098 AW |
1236 | if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | |
1237 | IEEE80211_KEY_FLAG_PUT_MIC_SPACE | | |
1238 | IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) | |
27b3eb9c JB |
1239 | increment_tailroom_need_count(key->sdata); |
1240 | } | |
1241 | ||
1242 | ieee80211_key_free(key, false); | |
1243 | } | |
1244 | EXPORT_SYMBOL_GPL(ieee80211_remove_key); | |
1245 | ||
1246 | struct ieee80211_key_conf * | |
1247 | ieee80211_gtk_rekey_add(struct ieee80211_vif *vif, | |
1248 | struct ieee80211_key_conf *keyconf) | |
1249 | { | |
1250 | struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); | |
1251 | struct ieee80211_local *local = sdata->local; | |
1252 | struct ieee80211_key *key; | |
1253 | int err; | |
1254 | ||
1255 | if (WARN_ON(!local->wowlan)) | |
1256 | return ERR_PTR(-EINVAL); | |
1257 | ||
1258 | if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) | |
1259 | return ERR_PTR(-EINVAL); | |
1260 | ||
1261 | key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx, | |
1262 | keyconf->keylen, keyconf->key, | |
2475b1cc | 1263 | 0, NULL, NULL); |
27b3eb9c | 1264 | if (IS_ERR(key)) |
c5dc164d | 1265 | return ERR_CAST(key); |
27b3eb9c JB |
1266 | |
1267 | if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED) | |
1268 | key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT; | |
1269 | ||
1270 | err = ieee80211_key_link(key, sdata, NULL); | |
1271 | if (err) | |
1272 | return ERR_PTR(err); | |
1273 | ||
1274 | return &key->conf; | |
1275 | } | |
1276 | EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add); |