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