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