net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.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 2017       Intel Deutschland 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 "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "debugfs_key.h"
24 #include "aes_ccm.h"
25 #include "aes_cmac.h"
26
27
28 /**
29  * DOC: Key handling basics
30  *
31  * Key handling in mac80211 is done based on per-interface (sub_if_data)
32  * keys and per-station keys. Since each station belongs to an interface,
33  * each station key also belongs to that interface.
34  *
35  * Hardware acceleration is done on a best-effort basis for algorithms
36  * that are implemented in software,  for each key the hardware is asked
37  * to enable that key for offloading but if it cannot do that the key is
38  * simply kept for software encryption (unless it is for an algorithm
39  * that isn't implemented in software).
40  * There is currently no way of knowing whether a key is handled in SW
41  * or HW except by looking into debugfs.
42  *
43  * All key management is internally protected by a mutex. Within all
44  * other parts of mac80211, key references are, just as STA structure
45  * references, protected by RCU. Note, however, that some things are
46  * unprotected, namely the key->sta dereferences within the hardware
47  * acceleration functions. This means that sta_info_destroy() must
48  * remove the key which waits for an RCU grace period.
49  */
50
51 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
52
53 static void assert_key_lock(struct ieee80211_local *local)
54 {
55         lockdep_assert_held(&local->key_mtx);
56 }
57
58 static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
59 {
60         if (key->sta)
61                 return &key->sta->sta;
62
63         return NULL;
64 }
65
66 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
67 {
68         /*
69          * When this count is zero, SKB resizing for allocating tailroom
70          * for IV or MMIC is skipped. But, this check has created two race
71          * cases in xmit path while transiting from zero count to one:
72          *
73          * 1. SKB resize was skipped because no key was added but just before
74          * the xmit key is added and SW encryption kicks off.
75          *
76          * 2. SKB resize was skipped because all the keys were hw planted but
77          * just before xmit one of the key is deleted and SW encryption kicks
78          * off.
79          *
80          * In both the above case SW encryption will find not enough space for
81          * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c)
82          *
83          * Solution has been explained at
84          * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net
85          */
86
87         if (!sdata->crypto_tx_tailroom_needed_cnt++) {
88                 /*
89                  * Flush all XMIT packets currently using HW encryption or no
90                  * encryption at all if the count transition is from 0 -> 1.
91                  */
92                 synchronize_net();
93         }
94 }
95
96 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
97 {
98         struct ieee80211_sub_if_data *sdata;
99         struct ieee80211_sta *sta;
100         int ret;
101
102         might_sleep();
103
104         if (!key->local->ops->set_key)
105                 goto out_unsupported;
106
107         assert_key_lock(key->local);
108
109         sta = get_sta_for_key(key);
110
111         /*
112          * If this is a per-STA GTK, check if it
113          * is supported; if not, return.
114          */
115         if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
116             !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
117                 goto out_unsupported;
118
119         sdata = key->sdata;
120         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
121                 /*
122                  * The driver doesn't know anything about VLAN interfaces.
123                  * Hence, don't send GTKs for VLAN interfaces to the driver.
124                  */
125                 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
126                         goto out_unsupported;
127                 sdata = container_of(sdata->bss,
128                                      struct ieee80211_sub_if_data,
129                                      u.ap);
130         }
131
132         ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
133
134         if (!ret) {
135                 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
136
137                 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
138                       (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
139                         sdata->crypto_tx_tailroom_needed_cnt--;
140
141                 return 0;
142         }
143
144         if (ret != -ENOSPC && ret != -EOPNOTSUPP)
145                 wiphy_err(key->local->hw.wiphy,
146                           "failed to set key (%d, %pM) to hardware (%d)\n",
147                           key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
148
149  out_unsupported:
150         switch (key->conf.cipher) {
151         case WLAN_CIPHER_SUITE_WEP40:
152         case WLAN_CIPHER_SUITE_WEP104:
153         case WLAN_CIPHER_SUITE_TKIP:
154         case WLAN_CIPHER_SUITE_CCMP:
155         case WLAN_CIPHER_SUITE_AES_CMAC:
156                 /* all of these we can do in software */
157                 return 0;
158         default:
159                 return -EINVAL;
160         }
161 }
162
163 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
164 {
165         struct ieee80211_sub_if_data *sdata;
166         struct ieee80211_sta *sta;
167         int ret;
168
169         might_sleep();
170
171         if (!key || !key->local->ops->set_key)
172                 return;
173
174         assert_key_lock(key->local);
175
176         if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
177                 return;
178
179         sta = get_sta_for_key(key);
180         sdata = key->sdata;
181
182         if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
183               (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
184                 increment_tailroom_need_count(sdata);
185
186         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
187                 sdata = container_of(sdata->bss,
188                                      struct ieee80211_sub_if_data,
189                                      u.ap);
190
191         ret = drv_set_key(key->local, DISABLE_KEY, sdata,
192                           sta, &key->conf);
193
194         if (ret)
195                 wiphy_err(key->local->hw.wiphy,
196                           "failed to remove key (%d, %pM) from hardware (%d)\n",
197                           key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
198
199         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
200 }
201
202 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
203 {
204         struct ieee80211_key *key;
205
206         key = container_of(key_conf, struct ieee80211_key, conf);
207
208         might_sleep();
209         assert_key_lock(key->local);
210
211         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
212
213         /*
214          * Flush TX path to avoid attempts to use this key
215          * after this function returns. Until then, drivers
216          * must be prepared to handle the key.
217          */
218         synchronize_rcu();
219 }
220 EXPORT_SYMBOL_GPL(ieee80211_key_removed);
221
222 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
223                                         int idx, bool uni, bool multi)
224 {
225         struct ieee80211_key *key = NULL;
226
227         assert_key_lock(sdata->local);
228
229         if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
230                 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
231
232         if (uni)
233                 rcu_assign_pointer(sdata->default_unicast_key, key);
234         if (multi)
235                 rcu_assign_pointer(sdata->default_multicast_key, key);
236
237         ieee80211_debugfs_key_update_default(sdata);
238 }
239
240 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
241                                bool uni, bool multi)
242 {
243         mutex_lock(&sdata->local->key_mtx);
244         __ieee80211_set_default_key(sdata, idx, uni, multi);
245         mutex_unlock(&sdata->local->key_mtx);
246 }
247
248 static void
249 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
250 {
251         struct ieee80211_key *key = NULL;
252
253         assert_key_lock(sdata->local);
254
255         if (idx >= NUM_DEFAULT_KEYS &&
256             idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
257                 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
258
259         rcu_assign_pointer(sdata->default_mgmt_key, key);
260
261         ieee80211_debugfs_key_update_default(sdata);
262 }
263
264 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
265                                     int idx)
266 {
267         mutex_lock(&sdata->local->key_mtx);
268         __ieee80211_set_default_mgmt_key(sdata, idx);
269         mutex_unlock(&sdata->local->key_mtx);
270 }
271
272
273 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
274                                     struct sta_info *sta,
275                                     bool pairwise,
276                                     struct ieee80211_key *old,
277                                     struct ieee80211_key *new)
278 {
279         int idx;
280         bool defunikey, defmultikey, defmgmtkey;
281
282         if (new)
283                 list_add_tail(&new->list, &sdata->key_list);
284
285         if (sta && pairwise) {
286                 rcu_assign_pointer(sta->ptk, new);
287         } else if (sta) {
288                 if (old)
289                         idx = old->conf.keyidx;
290                 else
291                         idx = new->conf.keyidx;
292                 rcu_assign_pointer(sta->gtk[idx], new);
293         } else {
294                 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
295
296                 if (old)
297                         idx = old->conf.keyidx;
298                 else
299                         idx = new->conf.keyidx;
300
301                 defunikey = old &&
302                         old == key_mtx_dereference(sdata->local,
303                                                 sdata->default_unicast_key);
304                 defmultikey = old &&
305                         old == key_mtx_dereference(sdata->local,
306                                                 sdata->default_multicast_key);
307                 defmgmtkey = old &&
308                         old == key_mtx_dereference(sdata->local,
309                                                 sdata->default_mgmt_key);
310
311                 if (defunikey && !new)
312                         __ieee80211_set_default_key(sdata, -1, true, false);
313                 if (defmultikey && !new)
314                         __ieee80211_set_default_key(sdata, -1, false, true);
315                 if (defmgmtkey && !new)
316                         __ieee80211_set_default_mgmt_key(sdata, -1);
317
318                 rcu_assign_pointer(sdata->keys[idx], new);
319                 if (defunikey && new)
320                         __ieee80211_set_default_key(sdata, new->conf.keyidx,
321                                                     true, false);
322                 if (defmultikey && new)
323                         __ieee80211_set_default_key(sdata, new->conf.keyidx,
324                                                     false, true);
325                 if (defmgmtkey && new)
326                         __ieee80211_set_default_mgmt_key(sdata,
327                                                          new->conf.keyidx);
328         }
329
330         if (old)
331                 list_del(&old->list);
332 }
333
334 struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
335                                           const u8 *key_data,
336                                           size_t seq_len, const u8 *seq)
337 {
338         struct ieee80211_key *key;
339         int i, j, err;
340
341         BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
342
343         key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
344         if (!key)
345                 return ERR_PTR(-ENOMEM);
346
347         /*
348          * Default to software encryption; we'll later upload the
349          * key to the hardware if possible.
350          */
351         key->conf.flags = 0;
352         key->flags = 0;
353
354         key->conf.cipher = cipher;
355         key->conf.keyidx = idx;
356         key->conf.keylen = key_len;
357         switch (cipher) {
358         case WLAN_CIPHER_SUITE_WEP40:
359         case WLAN_CIPHER_SUITE_WEP104:
360                 key->conf.iv_len = WEP_IV_LEN;
361                 key->conf.icv_len = WEP_ICV_LEN;
362                 break;
363         case WLAN_CIPHER_SUITE_TKIP:
364                 key->conf.iv_len = TKIP_IV_LEN;
365                 key->conf.icv_len = TKIP_ICV_LEN;
366                 if (seq) {
367                         for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
368                                 key->u.tkip.rx[i].iv32 =
369                                         get_unaligned_le32(&seq[2]);
370                                 key->u.tkip.rx[i].iv16 =
371                                         get_unaligned_le16(seq);
372                         }
373                 }
374                 spin_lock_init(&key->u.tkip.txlock);
375                 break;
376         case WLAN_CIPHER_SUITE_CCMP:
377                 key->conf.iv_len = CCMP_HDR_LEN;
378                 key->conf.icv_len = CCMP_MIC_LEN;
379                 if (seq) {
380                         for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
381                                 for (j = 0; j < CCMP_PN_LEN; j++)
382                                         key->u.ccmp.rx_pn[i][j] =
383                                                 seq[CCMP_PN_LEN - j - 1];
384                 }
385                 /*
386                  * Initialize AES key state here as an optimization so that
387                  * it does not need to be initialized for every packet.
388                  */
389                 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
390                 if (IS_ERR(key->u.ccmp.tfm)) {
391                         err = PTR_ERR(key->u.ccmp.tfm);
392                         kfree(key);
393                         return ERR_PTR(err);
394                 }
395                 break;
396         case WLAN_CIPHER_SUITE_AES_CMAC:
397                 key->conf.iv_len = 0;
398                 key->conf.icv_len = sizeof(struct ieee80211_mmie);
399                 if (seq)
400                         for (j = 0; j < 6; j++)
401                                 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
402                 /*
403                  * Initialize AES key state here as an optimization so that
404                  * it does not need to be initialized for every packet.
405                  */
406                 key->u.aes_cmac.tfm =
407                         ieee80211_aes_cmac_key_setup(key_data);
408                 if (IS_ERR(key->u.aes_cmac.tfm)) {
409                         err = PTR_ERR(key->u.aes_cmac.tfm);
410                         kfree(key);
411                         return ERR_PTR(err);
412                 }
413                 break;
414         }
415         memcpy(key->conf.key, key_data, key_len);
416         INIT_LIST_HEAD(&key->list);
417
418         return key;
419 }
420
421 static void __ieee80211_key_destroy(struct ieee80211_key *key)
422 {
423         if (!key)
424                 return;
425
426         /*
427          * Synchronize so the TX path can no longer be using
428          * this key before we free/remove it.
429          */
430         synchronize_rcu();
431
432         if (key->local)
433                 ieee80211_key_disable_hw_accel(key);
434
435         if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
436                 ieee80211_aes_key_free(key->u.ccmp.tfm);
437         if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
438                 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
439         if (key->local) {
440                 ieee80211_debugfs_key_remove(key);
441                 key->sdata->crypto_tx_tailroom_needed_cnt--;
442         }
443
444         kfree(key);
445 }
446
447 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata,
448                                     struct ieee80211_key *old,
449                                     struct ieee80211_key *new)
450 {
451         u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP];
452         u8 *tk_old, *tk_new;
453
454         if (!old || new->conf.keylen != old->conf.keylen)
455                 return false;
456
457         tk_old = old->conf.key;
458         tk_new = new->conf.key;
459
460         /*
461          * In station mode, don't compare the TX MIC key, as it's never used
462          * and offloaded rekeying may not care to send it to the host. This
463          * is the case in iwlwifi, for example.
464          */
465         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
466             new->conf.cipher == WLAN_CIPHER_SUITE_TKIP &&
467             new->conf.keylen == WLAN_KEY_LEN_TKIP &&
468             !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
469                 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP);
470                 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP);
471                 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
472                 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8);
473                 tk_old = tkip_old;
474                 tk_new = tkip_new;
475         }
476
477         return !memcmp(tk_old, tk_new, new->conf.keylen);
478 }
479
480 int ieee80211_key_link(struct ieee80211_key *key,
481                        struct ieee80211_sub_if_data *sdata,
482                        struct sta_info *sta)
483 {
484         struct ieee80211_key *old_key;
485         int idx, ret;
486         bool pairwise;
487
488         BUG_ON(!sdata);
489         BUG_ON(!key);
490
491         pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
492         idx = key->conf.keyidx;
493
494         if (sta) {
495                 /*
496                  * some hardware cannot handle TKIP with QoS, so
497                  * we indicate whether QoS could be in use.
498                  */
499                 if (test_sta_flag(sta, WLAN_STA_WME))
500                         key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
501         } else {
502                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
503                         struct sta_info *ap;
504
505                         /*
506                          * We're getting a sta pointer in, so must be under
507                          * appropriate locking for sta_info_get().
508                          */
509
510                         /* same here, the AP could be using QoS */
511                         ap = sta_info_get(sdata, sdata->u.mgd.bssid);
512                         if (ap) {
513                                 if (test_sta_flag(ap, WLAN_STA_WME))
514                                         key->conf.flags |=
515                                                 IEEE80211_KEY_FLAG_WMM_STA;
516                         }
517                 }
518         }
519
520         mutex_lock(&sdata->local->key_mtx);
521
522         if (sta && pairwise)
523                 old_key = key_mtx_dereference(sdata->local, sta->ptk);
524         else if (sta)
525                 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
526         else
527                 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
528
529         /*
530          * Silently accept key re-installation without really installing the
531          * new version of the key to avoid nonce reuse or replay issues.
532          */
533         if (ieee80211_key_identical(sdata, old_key, key)) {
534                 __ieee80211_key_free(key);
535                 ret = 0;
536                 goto out;
537         }
538
539         key->local = sdata->local;
540         key->sdata = sdata;
541         key->sta = sta;
542
543         increment_tailroom_need_count(sdata);
544
545         __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
546         __ieee80211_key_destroy(old_key);
547
548         ieee80211_debugfs_key_add(key);
549
550         ret = ieee80211_key_enable_hw_accel(key);
551
552  out:
553         mutex_unlock(&sdata->local->key_mtx);
554
555         return ret;
556 }
557
558 void __ieee80211_key_free(struct ieee80211_key *key)
559 {
560         if (!key)
561                 return;
562
563         /*
564          * Replace key with nothingness if it was ever used.
565          */
566         if (key->sdata)
567                 __ieee80211_key_replace(key->sdata, key->sta,
568                                 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
569                                 key, NULL);
570         __ieee80211_key_destroy(key);
571 }
572
573 void ieee80211_key_free(struct ieee80211_local *local,
574                         struct ieee80211_key *key)
575 {
576         mutex_lock(&local->key_mtx);
577         __ieee80211_key_free(key);
578         mutex_unlock(&local->key_mtx);
579 }
580
581 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
582 {
583         struct ieee80211_key *key;
584
585         ASSERT_RTNL();
586
587         if (WARN_ON(!ieee80211_sdata_running(sdata)))
588                 return;
589
590         mutex_lock(&sdata->local->key_mtx);
591
592         sdata->crypto_tx_tailroom_needed_cnt = 0;
593
594         list_for_each_entry(key, &sdata->key_list, list) {
595                 increment_tailroom_need_count(sdata);
596                 ieee80211_key_enable_hw_accel(key);
597         }
598
599         mutex_unlock(&sdata->local->key_mtx);
600 }
601
602 void ieee80211_iter_keys(struct ieee80211_hw *hw,
603                          struct ieee80211_vif *vif,
604                          void (*iter)(struct ieee80211_hw *hw,
605                                       struct ieee80211_vif *vif,
606                                       struct ieee80211_sta *sta,
607                                       struct ieee80211_key_conf *key,
608                                       void *data),
609                          void *iter_data)
610 {
611         struct ieee80211_local *local = hw_to_local(hw);
612         struct ieee80211_key *key;
613         struct ieee80211_sub_if_data *sdata;
614
615         ASSERT_RTNL();
616
617         mutex_lock(&local->key_mtx);
618         if (vif) {
619                 sdata = vif_to_sdata(vif);
620                 list_for_each_entry(key, &sdata->key_list, list)
621                         iter(hw, &sdata->vif,
622                              key->sta ? &key->sta->sta : NULL,
623                              &key->conf, iter_data);
624         } else {
625                 list_for_each_entry(sdata, &local->interfaces, list)
626                         list_for_each_entry(key, &sdata->key_list, list)
627                                 iter(hw, &sdata->vif,
628                                      key->sta ? &key->sta->sta : NULL,
629                                      &key->conf, iter_data);
630         }
631         mutex_unlock(&local->key_mtx);
632 }
633 EXPORT_SYMBOL(ieee80211_iter_keys);
634
635 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
636 {
637         struct ieee80211_key *key;
638
639         ASSERT_RTNL();
640
641         mutex_lock(&sdata->local->key_mtx);
642
643         list_for_each_entry(key, &sdata->key_list, list)
644                 ieee80211_key_disable_hw_accel(key);
645
646         mutex_unlock(&sdata->local->key_mtx);
647 }
648
649 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
650 {
651         struct ieee80211_key *key, *tmp;
652
653         mutex_lock(&sdata->local->key_mtx);
654
655         ieee80211_debugfs_key_remove_mgmt_default(sdata);
656
657         list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
658                 __ieee80211_key_free(key);
659
660         ieee80211_debugfs_key_update_default(sdata);
661
662         mutex_unlock(&sdata->local->key_mtx);
663 }
664
665
666 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
667                                 const u8 *replay_ctr, gfp_t gfp)
668 {
669         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
670
671         trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
672
673         cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
674 }
675 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
676
677 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
678                               struct ieee80211_key_seq *seq)
679 {
680         struct ieee80211_key *key;
681         u64 pn64;
682
683         if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
684                 return;
685
686         key = container_of(keyconf, struct ieee80211_key, conf);
687
688         switch (key->conf.cipher) {
689         case WLAN_CIPHER_SUITE_TKIP:
690                 seq->tkip.iv32 = key->u.tkip.tx.iv32;
691                 seq->tkip.iv16 = key->u.tkip.tx.iv16;
692                 break;
693         case WLAN_CIPHER_SUITE_CCMP:
694                 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
695                 seq->ccmp.pn[5] = pn64;
696                 seq->ccmp.pn[4] = pn64 >> 8;
697                 seq->ccmp.pn[3] = pn64 >> 16;
698                 seq->ccmp.pn[2] = pn64 >> 24;
699                 seq->ccmp.pn[1] = pn64 >> 32;
700                 seq->ccmp.pn[0] = pn64 >> 40;
701                 break;
702         case WLAN_CIPHER_SUITE_AES_CMAC:
703                 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
704                 seq->ccmp.pn[5] = pn64;
705                 seq->ccmp.pn[4] = pn64 >> 8;
706                 seq->ccmp.pn[3] = pn64 >> 16;
707                 seq->ccmp.pn[2] = pn64 >> 24;
708                 seq->ccmp.pn[1] = pn64 >> 32;
709                 seq->ccmp.pn[0] = pn64 >> 40;
710                 break;
711         default:
712                 WARN_ON(1);
713         }
714 }
715 EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
716
717 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
718                               int tid, struct ieee80211_key_seq *seq)
719 {
720         struct ieee80211_key *key;
721         const u8 *pn;
722
723         key = container_of(keyconf, struct ieee80211_key, conf);
724
725         switch (key->conf.cipher) {
726         case WLAN_CIPHER_SUITE_TKIP:
727                 if (WARN_ON(tid < 0 || tid >= NUM_RX_DATA_QUEUES))
728                         return;
729                 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
730                 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
731                 break;
732         case WLAN_CIPHER_SUITE_CCMP:
733                 if (WARN_ON(tid < -1 || tid >= NUM_RX_DATA_QUEUES))
734                         return;
735                 if (tid < 0)
736                         pn = key->u.ccmp.rx_pn[NUM_RX_DATA_QUEUES];
737                 else
738                         pn = key->u.ccmp.rx_pn[tid];
739                 memcpy(seq->ccmp.pn, pn, CCMP_PN_LEN);
740                 break;
741         case WLAN_CIPHER_SUITE_AES_CMAC:
742                 if (WARN_ON(tid != 0))
743                         return;
744                 pn = key->u.aes_cmac.rx_pn;
745                 memcpy(seq->aes_cmac.pn, pn, CMAC_PN_LEN);
746                 break;
747         }
748 }
749 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);