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