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