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