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