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