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" |
| 19 | #include "debugfs_key.h" |
| 20 | #include "aes_ccm.h" |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 21 | #include "aes_cmac.h" |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 22 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 23 | |
Johannes Berg | dbbea67 | 2008-02-26 14:34:06 +0100 | [diff] [blame] | 24 | /** |
| 25 | * DOC: Key handling basics |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 26 | * |
| 27 | * Key handling in mac80211 is done based on per-interface (sub_if_data) |
| 28 | * keys and per-station keys. Since each station belongs to an interface, |
| 29 | * each station key also belongs to that interface. |
| 30 | * |
| 31 | * Hardware acceleration is done on a best-effort basis, for each key |
| 32 | * that is eligible the hardware is asked to enable that key but if |
| 33 | * it cannot do that they key is simply kept for software encryption. |
| 34 | * There is currently no way of knowing this except by looking into |
| 35 | * debugfs. |
| 36 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 37 | * All key operations are protected internally so you can call them at |
| 38 | * any time. |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 39 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 40 | * Within mac80211, key references are, just as STA structure references, |
| 41 | * protected by RCU. Note, however, that some things are unprotected, |
| 42 | * namely the key->sta dereferences within the hardware acceleration |
| 43 | * functions. This means that sta_info_destroy() must flush the key todo |
| 44 | * list. |
| 45 | * |
| 46 | * All the direct key list manipulation functions must not sleep because |
| 47 | * they can operate on STA info structs that are protected by RCU. |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | 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] | 51 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 52 | /* key mutex: used to synchronise todo runners */ |
| 53 | static DEFINE_MUTEX(key_mutex); |
| 54 | static DEFINE_SPINLOCK(todo_lock); |
| 55 | static LIST_HEAD(todo_list); |
| 56 | |
| 57 | static void key_todo(struct work_struct *work) |
| 58 | { |
| 59 | ieee80211_key_todo(); |
| 60 | } |
| 61 | |
| 62 | static DECLARE_WORK(todo_work, key_todo); |
| 63 | |
| 64 | /** |
| 65 | * add_todo - add todo item for a key |
| 66 | * |
| 67 | * @key: key to add to do item for |
| 68 | * @flag: todo flag(s) |
| 69 | */ |
| 70 | static void add_todo(struct ieee80211_key *key, u32 flag) |
| 71 | { |
| 72 | if (!key) |
| 73 | return; |
| 74 | |
| 75 | spin_lock(&todo_lock); |
| 76 | key->flags |= flag; |
Johannes Berg | 245cbe7 | 2008-04-13 10:43:50 +0200 | [diff] [blame] | 77 | /* |
| 78 | * Remove again if already on the list so that we move it to the end. |
| 79 | */ |
| 80 | if (!list_empty(&key->todo)) |
| 81 | list_del(&key->todo); |
| 82 | list_add_tail(&key->todo, &todo_list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 83 | schedule_work(&todo_work); |
| 84 | spin_unlock(&todo_lock); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * ieee80211_key_lock - lock the mac80211 key operation lock |
| 89 | * |
| 90 | * This locks the (global) mac80211 key operation lock, all |
| 91 | * key operations must be done under this lock. |
| 92 | */ |
| 93 | static void ieee80211_key_lock(void) |
| 94 | { |
| 95 | mutex_lock(&key_mutex); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * ieee80211_key_unlock - unlock the mac80211 key operation lock |
| 100 | */ |
| 101 | static void ieee80211_key_unlock(void) |
| 102 | { |
| 103 | mutex_unlock(&key_mutex); |
| 104 | } |
| 105 | |
| 106 | static void assert_key_lock(void) |
| 107 | { |
| 108 | WARN_ON(!mutex_is_locked(&key_mutex)); |
| 109 | } |
| 110 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 111 | static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 112 | { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 113 | if (key->sta) |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 114 | return &key->sta->sta; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 115 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 116 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 120 | { |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 121 | struct ieee80211_sub_if_data *sdata; |
| 122 | struct ieee80211_sta *sta; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 123 | int ret; |
| 124 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 125 | assert_key_lock(); |
| 126 | might_sleep(); |
| 127 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 128 | if (!key->local->ops->set_key) |
| 129 | return; |
| 130 | |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 131 | sta = get_sta_for_key(key); |
| 132 | |
| 133 | sdata = key->sdata; |
| 134 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) |
| 135 | sdata = container_of(sdata->bss, |
| 136 | struct ieee80211_sub_if_data, |
| 137 | u.ap); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 138 | |
| 139 | ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY, |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 140 | &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 | |
| 182 | ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY, |
Johannes Berg | dc822b5 | 2008-12-29 12:55:09 +0100 | [diff] [blame] | 183 | &sdata->vif, 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, |
| 293 | const u8 *key_data) |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 294 | { |
| 295 | struct ieee80211_key *key; |
| 296 | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 297 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 298 | |
| 299 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 300 | if (!key) |
| 301 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Default to software encryption; we'll later upload the |
| 305 | * key to the hardware if possible. |
| 306 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 307 | key->conf.flags = 0; |
| 308 | key->flags = 0; |
| 309 | |
| 310 | key->conf.alg = alg; |
| 311 | key->conf.keyidx = idx; |
| 312 | key->conf.keylen = key_len; |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 313 | switch (alg) { |
| 314 | case ALG_WEP: |
| 315 | key->conf.iv_len = WEP_IV_LEN; |
| 316 | key->conf.icv_len = WEP_ICV_LEN; |
| 317 | break; |
| 318 | case ALG_TKIP: |
| 319 | key->conf.iv_len = TKIP_IV_LEN; |
| 320 | key->conf.icv_len = TKIP_ICV_LEN; |
| 321 | break; |
| 322 | case ALG_CCMP: |
| 323 | key->conf.iv_len = CCMP_HDR_LEN; |
| 324 | key->conf.icv_len = CCMP_MIC_LEN; |
| 325 | break; |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 326 | case ALG_AES_CMAC: |
| 327 | key->conf.iv_len = 0; |
| 328 | key->conf.icv_len = sizeof(struct ieee80211_mmie); |
| 329 | break; |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 330 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 331 | memcpy(key->conf.key, key_data, key_len); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 332 | INIT_LIST_HEAD(&key->list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 333 | INIT_LIST_HEAD(&key->todo); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 334 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 335 | if (alg == ALG_CCMP) { |
| 336 | /* |
| 337 | * Initialize AES key state here as an optimization so that |
| 338 | * it does not need to be initialized for every packet. |
| 339 | */ |
| 340 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 341 | if (!key->u.ccmp.tfm) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 342 | kfree(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | } |
| 346 | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 347 | if (alg == ALG_AES_CMAC) { |
| 348 | /* |
| 349 | * Initialize AES key state here as an optimization so that |
| 350 | * it does not need to be initialized for every packet. |
| 351 | */ |
| 352 | key->u.aes_cmac.tfm = |
| 353 | ieee80211_aes_cmac_key_setup(key_data); |
| 354 | if (!key->u.aes_cmac.tfm) { |
| 355 | kfree(key); |
| 356 | return NULL; |
| 357 | } |
| 358 | } |
| 359 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 360 | return key; |
| 361 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 362 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 363 | void ieee80211_key_link(struct ieee80211_key *key, |
| 364 | struct ieee80211_sub_if_data *sdata, |
| 365 | struct sta_info *sta) |
| 366 | { |
| 367 | struct ieee80211_key *old_key; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 368 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 369 | int idx; |
| 370 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 371 | BUG_ON(!sdata); |
| 372 | BUG_ON(!key); |
| 373 | |
| 374 | idx = key->conf.keyidx; |
| 375 | key->local = sdata->local; |
| 376 | key->sdata = sdata; |
| 377 | key->sta = sta; |
| 378 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 379 | if (sta) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 380 | /* |
| 381 | * some hardware cannot handle TKIP with QoS, so |
| 382 | * we indicate whether QoS could be in use. |
| 383 | */ |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 384 | if (test_sta_flags(sta, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 385 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
Ivo van Doorn | c6adbd2 | 2008-04-17 21:11:18 +0200 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * This key is for a specific sta interface, |
| 389 | * inform the driver that it should try to store |
| 390 | * this key as pairwise key. |
| 391 | */ |
| 392 | key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 393 | } else { |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 394 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 395 | struct sta_info *ap; |
| 396 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 397 | /* |
| 398 | * We're getting a sta pointer in, |
| 399 | * so must be under RCU read lock. |
| 400 | */ |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 401 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 402 | /* same here, the AP could be using QoS */ |
| 403 | ap = sta_info_get(key->local, key->sdata->u.sta.bssid); |
| 404 | if (ap) { |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 405 | if (test_sta_flags(ap, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 406 | key->conf.flags |= |
| 407 | IEEE80211_KEY_FLAG_WMM_STA; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 412 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 413 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 414 | if (sta) |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 415 | old_key = sta->key; |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 416 | else |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 417 | old_key = sdata->keys[idx]; |
| 418 | |
| 419 | __ieee80211_key_replace(sdata, sta, old_key, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 420 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 421 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 422 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 423 | /* free old key later */ |
| 424 | add_todo(old_key, KEY_FLAG_TODO_DELETE); |
| 425 | |
| 426 | add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 427 | if (netif_running(sdata->dev)) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 428 | add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD); |
| 429 | } |
| 430 | |
| 431 | static void __ieee80211_key_free(struct ieee80211_key *key) |
| 432 | { |
| 433 | /* |
| 434 | * Replace key with nothingness if it was ever used. |
| 435 | */ |
| 436 | if (key->sdata) |
| 437 | __ieee80211_key_replace(key->sdata, key->sta, |
| 438 | key, NULL); |
| 439 | |
| 440 | add_todo(key, KEY_FLAG_TODO_DELETE); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 443 | void ieee80211_key_free(struct ieee80211_key *key) |
| 444 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 445 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 446 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 447 | if (!key) |
| 448 | return; |
| 449 | |
Emmanuel Grumbach | 00eb7fe | 2008-06-26 12:13:46 +0300 | [diff] [blame] | 450 | if (!key->sdata) { |
| 451 | /* The key has not been linked yet, simply free it |
| 452 | * and don't Oops */ |
| 453 | if (key->conf.alg == ALG_CCMP) |
| 454 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 455 | kfree(key); |
| 456 | return; |
| 457 | } |
| 458 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 459 | spin_lock_irqsave(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 460 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 461 | spin_unlock_irqrestore(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 462 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 463 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 464 | /* |
| 465 | * To be safe against concurrent manipulations of the list (which shouldn't |
| 466 | * actually happen) we need to hold the spinlock. But under the spinlock we |
| 467 | * can't actually do much, so we defer processing to the todo list. Then run |
| 468 | * the todo list to be sure the operation and possibly previously pending |
| 469 | * operations are completed. |
| 470 | */ |
| 471 | static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata, |
| 472 | u32 todo_flags) |
| 473 | { |
| 474 | struct ieee80211_key *key; |
| 475 | unsigned long flags; |
| 476 | |
| 477 | might_sleep(); |
| 478 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 479 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 480 | list_for_each_entry(key, &sdata->key_list, list) |
| 481 | add_todo(key, todo_flags); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 482 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 483 | |
| 484 | ieee80211_key_todo(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 488 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 489 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 490 | |
| 491 | if (WARN_ON(!netif_running(sdata->dev))) |
| 492 | return; |
| 493 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 494 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 498 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 499 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 500 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 501 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 504 | static void __ieee80211_key_destroy(struct ieee80211_key *key) |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 505 | { |
| 506 | if (!key) |
| 507 | return; |
| 508 | |
| 509 | ieee80211_key_disable_hw_accel(key); |
| 510 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 511 | if (key->conf.alg == ALG_CCMP) |
| 512 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 513 | if (key->conf.alg == ALG_AES_CMAC) |
| 514 | ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm); |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 515 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 516 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 517 | kfree(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 518 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 519 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 520 | static void __ieee80211_key_todo(void) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 521 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 522 | struct ieee80211_key *key; |
| 523 | bool work_done; |
| 524 | u32 todoflags; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 525 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 526 | /* |
| 527 | * NB: sta_info_destroy relies on this! |
| 528 | */ |
| 529 | synchronize_rcu(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 530 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 531 | spin_lock(&todo_lock); |
| 532 | while (!list_empty(&todo_list)) { |
| 533 | key = list_first_entry(&todo_list, struct ieee80211_key, todo); |
| 534 | list_del_init(&key->todo); |
| 535 | todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS | |
| 536 | KEY_FLAG_TODO_DEFKEY | |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 537 | KEY_FLAG_TODO_DEFMGMTKEY | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 538 | KEY_FLAG_TODO_HWACCEL_ADD | |
| 539 | KEY_FLAG_TODO_HWACCEL_REMOVE | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 540 | KEY_FLAG_TODO_DELETE); |
| 541 | key->flags &= ~todoflags; |
| 542 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 543 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 544 | work_done = false; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 545 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 546 | if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) { |
| 547 | ieee80211_debugfs_key_add(key); |
| 548 | work_done = true; |
| 549 | } |
| 550 | if (todoflags & KEY_FLAG_TODO_DEFKEY) { |
| 551 | ieee80211_debugfs_key_remove_default(key->sdata); |
| 552 | ieee80211_debugfs_key_add_default(key->sdata); |
| 553 | work_done = true; |
| 554 | } |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 555 | if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) { |
| 556 | ieee80211_debugfs_key_remove_mgmt_default(key->sdata); |
| 557 | ieee80211_debugfs_key_add_mgmt_default(key->sdata); |
| 558 | work_done = true; |
| 559 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 560 | if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 561 | ieee80211_key_enable_hw_accel(key); |
| 562 | work_done = true; |
| 563 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 564 | if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) { |
| 565 | ieee80211_key_disable_hw_accel(key); |
| 566 | work_done = true; |
| 567 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 568 | if (todoflags & KEY_FLAG_TODO_DELETE) { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 569 | __ieee80211_key_destroy(key); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 570 | work_done = true; |
| 571 | } |
| 572 | |
| 573 | WARN_ON(!work_done); |
| 574 | |
| 575 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 576 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 577 | spin_unlock(&todo_lock); |
| 578 | } |
| 579 | |
| 580 | void ieee80211_key_todo(void) |
| 581 | { |
| 582 | ieee80211_key_lock(); |
| 583 | __ieee80211_key_todo(); |
| 584 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 588 | { |
| 589 | struct ieee80211_key *key, *tmp; |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 590 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 591 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 592 | ieee80211_key_lock(); |
| 593 | |
| 594 | ieee80211_debugfs_key_remove_default(sdata); |
Jouni Malinen | 3cfcf6ac | 2009-01-08 13:32:02 +0200 | [diff] [blame^] | 595 | ieee80211_debugfs_key_remove_mgmt_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 596 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 597 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 598 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 599 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 600 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 601 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 602 | __ieee80211_key_todo(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 603 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 604 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 605 | } |