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