Johannes Berg | 1f5a7e4 | 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 | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 12 | #include <linux/if_ether.h> |
| 13 | #include <linux/etherdevice.h> |
| 14 | #include <linux/list.h> |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 15 | #include <linux/rcupdate.h> |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 16 | #include <linux/rtnetlink.h> |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 17 | #include <net/mac80211.h> |
| 18 | #include "ieee80211_i.h" |
Johannes Berg | 2448798 | 2009-04-23 18:52:52 +0200 | [diff] [blame] | 19 | #include "driver-ops.h" |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 20 | #include "debugfs_key.h" |
| 21 | #include "aes_ccm.h" |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 22 | #include "aes_cmac.h" |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 23 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 24 | |
Johannes Berg | dbbea67 | 2008-02-26 14:34:06 +0100 | [diff] [blame] | 25 | /** |
| 26 | * DOC: Key handling basics |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 27 | * |
| 28 | * Key handling in mac80211 is done based on per-interface (sub_if_data) |
| 29 | * keys and per-station keys. Since each station belongs to an interface, |
| 30 | * each station key also belongs to that interface. |
| 31 | * |
| 32 | * Hardware acceleration is done on a best-effort basis, for each key |
| 33 | * that is eligible the hardware is asked to enable that key but if |
| 34 | * it cannot do that they key is simply kept for software encryption. |
| 35 | * There is currently no way of knowing this except by looking into |
| 36 | * debugfs. |
| 37 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 38 | * All key operations are protected internally so you can call them at |
| 39 | * any time. |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 40 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 41 | * Within mac80211, key references are, just as STA structure references, |
| 42 | * protected by RCU. Note, however, that some things are unprotected, |
| 43 | * namely the key->sta dereferences within the hardware acceleration |
| 44 | * functions. This means that sta_info_destroy() must flush the key todo |
| 45 | * list. |
| 46 | * |
| 47 | * All the direct key list manipulation functions must not sleep because |
| 48 | * they can operate on STA info structs that are protected by RCU. |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | 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] | 52 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 53 | /* key mutex: used to synchronise todo runners */ |
| 54 | static DEFINE_MUTEX(key_mutex); |
| 55 | static DEFINE_SPINLOCK(todo_lock); |
| 56 | static LIST_HEAD(todo_list); |
| 57 | |
| 58 | static void key_todo(struct work_struct *work) |
| 59 | { |
| 60 | ieee80211_key_todo(); |
| 61 | } |
| 62 | |
| 63 | static DECLARE_WORK(todo_work, key_todo); |
| 64 | |
| 65 | /** |
| 66 | * add_todo - add todo item for a key |
| 67 | * |
| 68 | * @key: key to add to do item for |
| 69 | * @flag: todo flag(s) |
| 70 | */ |
| 71 | static void add_todo(struct ieee80211_key *key, u32 flag) |
| 72 | { |
| 73 | if (!key) |
| 74 | return; |
| 75 | |
| 76 | spin_lock(&todo_lock); |
| 77 | key->flags |= flag; |
Johannes Berg | 245cbe7 | 2008-04-13 10:43:50 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Remove again if already on the list so that we move it to the end. |
| 80 | */ |
| 81 | if (!list_empty(&key->todo)) |
| 82 | list_del(&key->todo); |
| 83 | list_add_tail(&key->todo, &todo_list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 84 | schedule_work(&todo_work); |
| 85 | spin_unlock(&todo_lock); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * ieee80211_key_lock - lock the mac80211 key operation lock |
| 90 | * |
| 91 | * This locks the (global) mac80211 key operation lock, all |
| 92 | * key operations must be done under this lock. |
| 93 | */ |
| 94 | static void ieee80211_key_lock(void) |
| 95 | { |
| 96 | mutex_lock(&key_mutex); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * ieee80211_key_unlock - unlock the mac80211 key operation lock |
| 101 | */ |
| 102 | static void ieee80211_key_unlock(void) |
| 103 | { |
| 104 | mutex_unlock(&key_mutex); |
| 105 | } |
| 106 | |
| 107 | static void assert_key_lock(void) |
| 108 | { |
| 109 | WARN_ON(!mutex_is_locked(&key_mutex)); |
| 110 | } |
| 111 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 112 | static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 113 | { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 114 | if (key->sta) |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 115 | return &key->sta->sta; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 116 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 117 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 121 | { |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 122 | struct ieee80211_sub_if_data *sdata; |
| 123 | struct ieee80211_sta *sta; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 124 | int ret; |
| 125 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 126 | assert_key_lock(); |
| 127 | might_sleep(); |
| 128 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 129 | if (!key->local->ops->set_key) |
| 130 | return; |
| 131 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 132 | sta = get_sta_for_key(key); |
| 133 | |
| 134 | sdata = key->sdata; |
| 135 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) |
| 136 | sdata = container_of(sdata->bss, |
| 137 | struct ieee80211_sub_if_data, |
| 138 | u.ap); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 139 | |
Johannes Berg | 2448798 | 2009-04-23 18:52:52 +0200 | [diff] [blame] | 140 | ret = drv_set_key(key->local, SET_KEY, &sdata->vif, sta, &key->conf); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 141 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 142 | if (!ret) { |
| 143 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 144 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 145 | spin_unlock(&todo_lock); |
| 146 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 147 | |
| 148 | if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP) |
| 149 | printk(KERN_ERR "mac80211-%s: failed to set key " |
Johannes Berg | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame] | 150 | "(%d, %pM) to hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 151 | wiphy_name(key->local->hw.wiphy), |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 152 | key->conf.keyidx, sta ? sta->addr : bcast_addr, ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) |
| 156 | { |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 157 | struct ieee80211_sub_if_data *sdata; |
| 158 | struct ieee80211_sta *sta; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 159 | int ret; |
| 160 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 161 | assert_key_lock(); |
| 162 | might_sleep(); |
| 163 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 164 | if (!key || !key->local->ops->set_key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 165 | return; |
| 166 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 167 | spin_lock(&todo_lock); |
| 168 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) { |
| 169 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 170 | return; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 171 | } |
| 172 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 173 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 174 | sta = get_sta_for_key(key); |
| 175 | sdata = key->sdata; |
| 176 | |
| 177 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) |
| 178 | sdata = container_of(sdata->bss, |
| 179 | struct ieee80211_sub_if_data, |
| 180 | u.ap); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 181 | |
Johannes Berg | 2448798 | 2009-04-23 18:52:52 +0200 | [diff] [blame] | 182 | ret = drv_set_key(key->local, DISABLE_KEY, &sdata->vif, |
| 183 | sta, &key->conf); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 184 | |
| 185 | if (ret) |
| 186 | printk(KERN_ERR "mac80211-%s: failed to remove key " |
Johannes Berg | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame] | 187 | "(%d, %pM) from hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 188 | wiphy_name(key->local->hw.wiphy), |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 189 | key->conf.keyidx, sta ? sta->addr : bcast_addr, ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 190 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 191 | spin_lock(&todo_lock); |
| 192 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
| 193 | spin_unlock(&todo_lock); |
| 194 | } |
| 195 | |
| 196 | static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, |
| 197 | int idx) |
| 198 | { |
| 199 | struct ieee80211_key *key = NULL; |
| 200 | |
| 201 | if (idx >= 0 && idx < NUM_DEFAULT_KEYS) |
| 202 | key = sdata->keys[idx]; |
| 203 | |
| 204 | rcu_assign_pointer(sdata->default_key, key); |
| 205 | |
| 206 | if (key) |
| 207 | add_todo(key, KEY_FLAG_TODO_DEFKEY); |
| 208 | } |
| 209 | |
| 210 | void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx) |
| 211 | { |
| 212 | unsigned long flags; |
| 213 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 214 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 215 | __ieee80211_set_default_key(sdata, idx); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 216 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 219 | static void |
| 220 | __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx) |
| 221 | { |
| 222 | struct ieee80211_key *key = NULL; |
| 223 | |
| 224 | if (idx >= NUM_DEFAULT_KEYS && |
| 225 | idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) |
| 226 | key = sdata->keys[idx]; |
| 227 | |
| 228 | rcu_assign_pointer(sdata->default_mgmt_key, key); |
| 229 | |
| 230 | if (key) |
| 231 | add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY); |
| 232 | } |
| 233 | |
| 234 | void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, |
| 235 | int idx) |
| 236 | { |
| 237 | unsigned long flags; |
| 238 | |
| 239 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
| 240 | __ieee80211_set_default_mgmt_key(sdata, idx); |
| 241 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
| 242 | } |
| 243 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 244 | |
| 245 | static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, |
| 246 | struct sta_info *sta, |
| 247 | struct ieee80211_key *old, |
| 248 | struct ieee80211_key *new) |
| 249 | { |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 250 | int idx, defkey, defmgmtkey; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 251 | |
| 252 | if (new) |
| 253 | list_add(&new->list, &sdata->key_list); |
| 254 | |
| 255 | if (sta) { |
| 256 | rcu_assign_pointer(sta->key, new); |
| 257 | } else { |
| 258 | WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); |
| 259 | |
| 260 | if (old) |
| 261 | idx = old->conf.keyidx; |
| 262 | else |
| 263 | idx = new->conf.keyidx; |
| 264 | |
| 265 | defkey = old && sdata->default_key == old; |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 266 | defmgmtkey = old && sdata->default_mgmt_key == old; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 267 | |
| 268 | if (defkey && !new) |
| 269 | __ieee80211_set_default_key(sdata, -1); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 270 | if (defmgmtkey && !new) |
| 271 | __ieee80211_set_default_mgmt_key(sdata, -1); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 272 | |
| 273 | rcu_assign_pointer(sdata->keys[idx], new); |
| 274 | if (defkey && new) |
| 275 | __ieee80211_set_default_key(sdata, new->conf.keyidx); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 276 | if (defmgmtkey && new) |
| 277 | __ieee80211_set_default_mgmt_key(sdata, |
| 278 | new->conf.keyidx); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (old) { |
| 282 | /* |
| 283 | * We'll use an empty list to indicate that the key |
| 284 | * has already been removed. |
| 285 | */ |
| 286 | list_del_init(&old->list); |
| 287 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 290 | struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg, |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 291 | int idx, |
| 292 | size_t key_len, |
Jouni Malinen | faa8fdc | 2009-05-11 21:57:58 +0300 | [diff] [blame^] | 293 | const u8 *key_data, |
| 294 | size_t seq_len, const u8 *seq) |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 295 | { |
| 296 | struct ieee80211_key *key; |
Jouni Malinen | faa8fdc | 2009-05-11 21:57:58 +0300 | [diff] [blame^] | 297 | int i, j; |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 298 | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 299 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 300 | |
| 301 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 302 | if (!key) |
| 303 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Default to software encryption; we'll later upload the |
| 307 | * key to the hardware if possible. |
| 308 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 309 | key->conf.flags = 0; |
| 310 | key->flags = 0; |
| 311 | |
| 312 | key->conf.alg = alg; |
| 313 | key->conf.keyidx = idx; |
| 314 | key->conf.keylen = key_len; |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 315 | switch (alg) { |
| 316 | case ALG_WEP: |
| 317 | key->conf.iv_len = WEP_IV_LEN; |
| 318 | key->conf.icv_len = WEP_ICV_LEN; |
| 319 | break; |
| 320 | case ALG_TKIP: |
| 321 | key->conf.iv_len = TKIP_IV_LEN; |
| 322 | key->conf.icv_len = TKIP_ICV_LEN; |
Jouni Malinen | faa8fdc | 2009-05-11 21:57:58 +0300 | [diff] [blame^] | 323 | if (seq && seq_len == 6) { |
| 324 | for (i = 0; i < NUM_RX_DATA_QUEUES; i++) { |
| 325 | key->u.tkip.rx[i].iv32 = |
| 326 | get_unaligned_le32(&seq[2]); |
| 327 | key->u.tkip.rx[i].iv16 = |
| 328 | get_unaligned_le16(seq); |
| 329 | } |
| 330 | } |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 331 | break; |
| 332 | case ALG_CCMP: |
| 333 | key->conf.iv_len = CCMP_HDR_LEN; |
| 334 | key->conf.icv_len = CCMP_MIC_LEN; |
Jouni Malinen | faa8fdc | 2009-05-11 21:57:58 +0300 | [diff] [blame^] | 335 | if (seq && seq_len == CCMP_PN_LEN) { |
| 336 | for (i = 0; i < NUM_RX_DATA_QUEUES; i++) |
| 337 | for (j = 0; j < CCMP_PN_LEN; j++) |
| 338 | key->u.ccmp.rx_pn[i][j] = |
| 339 | seq[CCMP_PN_LEN - j - 1]; |
| 340 | } |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 341 | break; |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 342 | case ALG_AES_CMAC: |
| 343 | key->conf.iv_len = 0; |
| 344 | key->conf.icv_len = sizeof(struct ieee80211_mmie); |
Jouni Malinen | faa8fdc | 2009-05-11 21:57:58 +0300 | [diff] [blame^] | 345 | if (seq && seq_len == 6) |
| 346 | for (j = 0; j < 6; j++) |
| 347 | key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1]; |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 348 | break; |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 349 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 350 | memcpy(key->conf.key, key_data, key_len); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 351 | INIT_LIST_HEAD(&key->list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 352 | INIT_LIST_HEAD(&key->todo); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 353 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 354 | if (alg == ALG_CCMP) { |
| 355 | /* |
| 356 | * Initialize AES key state here as an optimization so that |
| 357 | * it does not need to be initialized for every packet. |
| 358 | */ |
| 359 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 360 | if (!key->u.ccmp.tfm) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 361 | kfree(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | } |
| 365 | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 366 | if (alg == ALG_AES_CMAC) { |
| 367 | /* |
| 368 | * Initialize AES key state here as an optimization so that |
| 369 | * it does not need to be initialized for every packet. |
| 370 | */ |
| 371 | key->u.aes_cmac.tfm = |
| 372 | ieee80211_aes_cmac_key_setup(key_data); |
| 373 | if (!key->u.aes_cmac.tfm) { |
| 374 | kfree(key); |
| 375 | return NULL; |
| 376 | } |
| 377 | } |
| 378 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 379 | return key; |
| 380 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 381 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 382 | void ieee80211_key_link(struct ieee80211_key *key, |
| 383 | struct ieee80211_sub_if_data *sdata, |
| 384 | struct sta_info *sta) |
| 385 | { |
| 386 | struct ieee80211_key *old_key; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 387 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 388 | int idx; |
| 389 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 390 | BUG_ON(!sdata); |
| 391 | BUG_ON(!key); |
| 392 | |
| 393 | idx = key->conf.keyidx; |
| 394 | key->local = sdata->local; |
| 395 | key->sdata = sdata; |
| 396 | key->sta = sta; |
| 397 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 398 | if (sta) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 399 | /* |
| 400 | * some hardware cannot handle TKIP with QoS, so |
| 401 | * we indicate whether QoS could be in use. |
| 402 | */ |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 403 | if (test_sta_flags(sta, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 404 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
Ivo van Doorn | c6adbd2 | 2008-04-17 21:11:18 +0200 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * This key is for a specific sta interface, |
| 408 | * inform the driver that it should try to store |
| 409 | * this key as pairwise key. |
| 410 | */ |
| 411 | key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 412 | } else { |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 413 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 414 | struct sta_info *ap; |
| 415 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 416 | /* |
| 417 | * We're getting a sta pointer in, |
| 418 | * so must be under RCU read lock. |
| 419 | */ |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 420 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 421 | /* same here, the AP could be using QoS */ |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 422 | ap = sta_info_get(key->local, key->sdata->u.mgd.bssid); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 423 | if (ap) { |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 424 | if (test_sta_flags(ap, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 425 | key->conf.flags |= |
| 426 | IEEE80211_KEY_FLAG_WMM_STA; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 427 | } |
| 428 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 431 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 432 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 433 | if (sta) |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 434 | old_key = sta->key; |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 435 | else |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 436 | old_key = sdata->keys[idx]; |
| 437 | |
| 438 | __ieee80211_key_replace(sdata, sta, old_key, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 439 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 440 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 441 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 442 | /* free old key later */ |
| 443 | add_todo(old_key, KEY_FLAG_TODO_DELETE); |
| 444 | |
| 445 | add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 446 | if (netif_running(sdata->dev)) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 447 | add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD); |
| 448 | } |
| 449 | |
| 450 | static void __ieee80211_key_free(struct ieee80211_key *key) |
| 451 | { |
| 452 | /* |
| 453 | * Replace key with nothingness if it was ever used. |
| 454 | */ |
| 455 | if (key->sdata) |
| 456 | __ieee80211_key_replace(key->sdata, key->sta, |
| 457 | key, NULL); |
| 458 | |
| 459 | add_todo(key, KEY_FLAG_TODO_DELETE); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 462 | void ieee80211_key_free(struct ieee80211_key *key) |
| 463 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 464 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 465 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 466 | if (!key) |
| 467 | return; |
| 468 | |
Emmanuel Grumbach | 00eb7fe | 2008-06-26 12:13:46 +0300 | [diff] [blame] | 469 | if (!key->sdata) { |
| 470 | /* The key has not been linked yet, simply free it |
| 471 | * and don't Oops */ |
| 472 | if (key->conf.alg == ALG_CCMP) |
| 473 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 474 | kfree(key); |
| 475 | return; |
| 476 | } |
| 477 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 478 | spin_lock_irqsave(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 479 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 480 | spin_unlock_irqrestore(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 481 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 482 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 483 | /* |
| 484 | * To be safe against concurrent manipulations of the list (which shouldn't |
| 485 | * actually happen) we need to hold the spinlock. But under the spinlock we |
| 486 | * can't actually do much, so we defer processing to the todo list. Then run |
| 487 | * the todo list to be sure the operation and possibly previously pending |
| 488 | * operations are completed. |
| 489 | */ |
| 490 | static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata, |
| 491 | u32 todo_flags) |
| 492 | { |
| 493 | struct ieee80211_key *key; |
| 494 | unsigned long flags; |
| 495 | |
| 496 | might_sleep(); |
| 497 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 498 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 499 | list_for_each_entry(key, &sdata->key_list, list) |
| 500 | add_todo(key, todo_flags); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 501 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 502 | |
| 503 | ieee80211_key_todo(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 507 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 508 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 509 | |
| 510 | if (WARN_ON(!netif_running(sdata->dev))) |
| 511 | return; |
| 512 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 513 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 517 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 518 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 519 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 520 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 523 | static void __ieee80211_key_destroy(struct ieee80211_key *key) |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 524 | { |
| 525 | if (!key) |
| 526 | return; |
| 527 | |
| 528 | ieee80211_key_disable_hw_accel(key); |
| 529 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 530 | if (key->conf.alg == ALG_CCMP) |
| 531 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 532 | if (key->conf.alg == ALG_AES_CMAC) |
| 533 | ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm); |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 534 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 535 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 536 | kfree(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 537 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 538 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 539 | static void __ieee80211_key_todo(void) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 540 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 541 | struct ieee80211_key *key; |
| 542 | bool work_done; |
| 543 | u32 todoflags; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 544 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 545 | /* |
| 546 | * NB: sta_info_destroy relies on this! |
| 547 | */ |
| 548 | synchronize_rcu(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 549 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 550 | spin_lock(&todo_lock); |
| 551 | while (!list_empty(&todo_list)) { |
| 552 | key = list_first_entry(&todo_list, struct ieee80211_key, todo); |
| 553 | list_del_init(&key->todo); |
| 554 | todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS | |
| 555 | KEY_FLAG_TODO_DEFKEY | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 556 | KEY_FLAG_TODO_DEFMGMTKEY | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 557 | KEY_FLAG_TODO_HWACCEL_ADD | |
| 558 | KEY_FLAG_TODO_HWACCEL_REMOVE | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 559 | KEY_FLAG_TODO_DELETE); |
| 560 | key->flags &= ~todoflags; |
| 561 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 562 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 563 | work_done = false; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 564 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 565 | if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) { |
| 566 | ieee80211_debugfs_key_add(key); |
| 567 | work_done = true; |
| 568 | } |
| 569 | if (todoflags & KEY_FLAG_TODO_DEFKEY) { |
| 570 | ieee80211_debugfs_key_remove_default(key->sdata); |
| 571 | ieee80211_debugfs_key_add_default(key->sdata); |
| 572 | work_done = true; |
| 573 | } |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 574 | if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) { |
| 575 | ieee80211_debugfs_key_remove_mgmt_default(key->sdata); |
| 576 | ieee80211_debugfs_key_add_mgmt_default(key->sdata); |
| 577 | work_done = true; |
| 578 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 579 | if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 580 | ieee80211_key_enable_hw_accel(key); |
| 581 | work_done = true; |
| 582 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 583 | if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) { |
| 584 | ieee80211_key_disable_hw_accel(key); |
| 585 | work_done = true; |
| 586 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 587 | if (todoflags & KEY_FLAG_TODO_DELETE) { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 588 | __ieee80211_key_destroy(key); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 589 | work_done = true; |
| 590 | } |
| 591 | |
| 592 | WARN_ON(!work_done); |
| 593 | |
| 594 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 595 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 596 | spin_unlock(&todo_lock); |
| 597 | } |
| 598 | |
| 599 | void ieee80211_key_todo(void) |
| 600 | { |
| 601 | ieee80211_key_lock(); |
| 602 | __ieee80211_key_todo(); |
| 603 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 607 | { |
| 608 | struct ieee80211_key *key, *tmp; |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 609 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 610 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 611 | ieee80211_key_lock(); |
| 612 | |
| 613 | ieee80211_debugfs_key_remove_default(sdata); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame] | 614 | ieee80211_debugfs_key_remove_mgmt_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 615 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 616 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 617 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 618 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 619 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 620 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 621 | __ieee80211_key_todo(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 622 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 623 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 624 | } |