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 | 1f5a7e47 | 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 | 1f5a7e47 | 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" |
| 21 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 22 | |
Johannes Berg | dbbea67 | 2008-02-26 14:34:06 +0100 | [diff] [blame] | 23 | /** |
| 24 | * DOC: Key handling basics |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 25 | * |
| 26 | * Key handling in mac80211 is done based on per-interface (sub_if_data) |
| 27 | * keys and per-station keys. Since each station belongs to an interface, |
| 28 | * each station key also belongs to that interface. |
| 29 | * |
| 30 | * Hardware acceleration is done on a best-effort basis, for each key |
| 31 | * that is eligible the hardware is asked to enable that key but if |
| 32 | * it cannot do that they key is simply kept for software encryption. |
| 33 | * There is currently no way of knowing this except by looking into |
| 34 | * debugfs. |
| 35 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 36 | * All key operations are protected internally so you can call them at |
| 37 | * any time. |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 38 | * |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 39 | * Within mac80211, key references are, just as STA structure references, |
| 40 | * protected by RCU. Note, however, that some things are unprotected, |
| 41 | * namely the key->sta dereferences within the hardware acceleration |
| 42 | * functions. This means that sta_info_destroy() must flush the key todo |
| 43 | * list. |
| 44 | * |
| 45 | * All the direct key list manipulation functions must not sleep because |
| 46 | * they can operate on STA info structs that are protected by RCU. |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 47 | */ |
| 48 | |
| 49 | static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 50 | static const u8 zero_addr[ETH_ALEN]; |
| 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 | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 111 | static const u8 *get_mac_for_key(struct ieee80211_key *key) |
| 112 | { |
| 113 | const u8 *addr = bcast_addr; |
| 114 | |
| 115 | /* |
| 116 | * If we're an AP we won't ever receive frames with a non-WEP |
| 117 | * group key so we tell the driver that by using the zero MAC |
| 118 | * address to indicate a transmit-only key. |
| 119 | */ |
| 120 | if (key->conf.alg != ALG_WEP && |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 121 | (key->sdata->vif.type == NL80211_IFTYPE_AP || |
| 122 | key->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 123 | addr = zero_addr; |
| 124 | |
| 125 | if (key->sta) |
Johannes Berg | 17741cd | 2008-09-11 00:02:02 +0200 | [diff] [blame] | 126 | addr = key->sta->sta.addr; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 127 | |
| 128 | return addr; |
| 129 | } |
| 130 | |
| 131 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 132 | { |
| 133 | const u8 *addr; |
| 134 | int ret; |
| 135 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 136 | assert_key_lock(); |
| 137 | might_sleep(); |
| 138 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 139 | if (!key->local->ops->set_key) |
| 140 | return; |
| 141 | |
| 142 | addr = get_mac_for_key(key); |
| 143 | |
| 144 | ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY, |
| 145 | key->sdata->dev->dev_addr, addr, |
| 146 | &key->conf); |
| 147 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 148 | if (!ret) { |
| 149 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 150 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 151 | spin_unlock(&todo_lock); |
| 152 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 153 | |
| 154 | if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP) |
| 155 | printk(KERN_ERR "mac80211-%s: failed to set key " |
Johannes Berg | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame] | 156 | "(%d, %pM) to hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 157 | wiphy_name(key->local->hw.wiphy), |
Johannes Berg | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame] | 158 | key->conf.keyidx, addr, ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) |
| 162 | { |
| 163 | const u8 *addr; |
| 164 | int ret; |
| 165 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 166 | assert_key_lock(); |
| 167 | might_sleep(); |
| 168 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 169 | if (!key || !key->local->ops->set_key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 170 | return; |
| 171 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 172 | spin_lock(&todo_lock); |
| 173 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) { |
| 174 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 175 | return; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 176 | } |
| 177 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 178 | |
| 179 | addr = get_mac_for_key(key); |
| 180 | |
| 181 | ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY, |
| 182 | key->sdata->dev->dev_addr, addr, |
| 183 | &key->conf); |
| 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 | 0c68ae26 | 2008-10-27 15:56:10 -0700 | [diff] [blame] | 189 | key->conf.keyidx, 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 | |
| 219 | |
| 220 | static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, |
| 221 | struct sta_info *sta, |
| 222 | struct ieee80211_key *old, |
| 223 | struct ieee80211_key *new) |
| 224 | { |
| 225 | int idx, defkey; |
| 226 | |
| 227 | if (new) |
| 228 | list_add(&new->list, &sdata->key_list); |
| 229 | |
| 230 | if (sta) { |
| 231 | rcu_assign_pointer(sta->key, new); |
| 232 | } else { |
| 233 | WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); |
| 234 | |
| 235 | if (old) |
| 236 | idx = old->conf.keyidx; |
| 237 | else |
| 238 | idx = new->conf.keyidx; |
| 239 | |
| 240 | defkey = old && sdata->default_key == old; |
| 241 | |
| 242 | if (defkey && !new) |
| 243 | __ieee80211_set_default_key(sdata, -1); |
| 244 | |
| 245 | rcu_assign_pointer(sdata->keys[idx], new); |
| 246 | if (defkey && new) |
| 247 | __ieee80211_set_default_key(sdata, new->conf.keyidx); |
| 248 | } |
| 249 | |
| 250 | if (old) { |
| 251 | /* |
| 252 | * We'll use an empty list to indicate that the key |
| 253 | * has already been removed. |
| 254 | */ |
| 255 | list_del_init(&old->list); |
| 256 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 259 | struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg, |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 260 | int idx, |
| 261 | size_t key_len, |
| 262 | const u8 *key_data) |
Johannes Berg | 1f5a7e47 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 263 | { |
| 264 | struct ieee80211_key *key; |
| 265 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 266 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 267 | |
| 268 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e47 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 269 | if (!key) |
| 270 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * Default to software encryption; we'll later upload the |
| 274 | * key to the hardware if possible. |
| 275 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 276 | key->conf.flags = 0; |
| 277 | key->flags = 0; |
| 278 | |
| 279 | key->conf.alg = alg; |
| 280 | key->conf.keyidx = idx; |
| 281 | key->conf.keylen = key_len; |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 282 | switch (alg) { |
| 283 | case ALG_WEP: |
| 284 | key->conf.iv_len = WEP_IV_LEN; |
| 285 | key->conf.icv_len = WEP_ICV_LEN; |
| 286 | break; |
| 287 | case ALG_TKIP: |
| 288 | key->conf.iv_len = TKIP_IV_LEN; |
| 289 | key->conf.icv_len = TKIP_ICV_LEN; |
| 290 | break; |
| 291 | case ALG_CCMP: |
| 292 | key->conf.iv_len = CCMP_HDR_LEN; |
| 293 | key->conf.icv_len = CCMP_MIC_LEN; |
| 294 | break; |
| 295 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 296 | memcpy(key->conf.key, key_data, key_len); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 297 | INIT_LIST_HEAD(&key->list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 298 | INIT_LIST_HEAD(&key->todo); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 299 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 300 | if (alg == ALG_CCMP) { |
| 301 | /* |
| 302 | * Initialize AES key state here as an optimization so that |
| 303 | * it does not need to be initialized for every packet. |
| 304 | */ |
| 305 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 306 | if (!key->u.ccmp.tfm) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 307 | kfree(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 308 | return NULL; |
| 309 | } |
| 310 | } |
| 311 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 312 | return key; |
| 313 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 314 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 315 | void ieee80211_key_link(struct ieee80211_key *key, |
| 316 | struct ieee80211_sub_if_data *sdata, |
| 317 | struct sta_info *sta) |
| 318 | { |
| 319 | struct ieee80211_key *old_key; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 320 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 321 | int idx; |
| 322 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 323 | BUG_ON(!sdata); |
| 324 | BUG_ON(!key); |
| 325 | |
| 326 | idx = key->conf.keyidx; |
| 327 | key->local = sdata->local; |
| 328 | key->sdata = sdata; |
| 329 | key->sta = sta; |
| 330 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 331 | if (sta) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 332 | /* |
| 333 | * some hardware cannot handle TKIP with QoS, so |
| 334 | * we indicate whether QoS could be in use. |
| 335 | */ |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 336 | if (test_sta_flags(sta, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 337 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
Ivo van Doorn | c6adbd2 | 2008-04-17 21:11:18 +0200 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * This key is for a specific sta interface, |
| 341 | * inform the driver that it should try to store |
| 342 | * this key as pairwise key. |
| 343 | */ |
| 344 | key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 345 | } else { |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 346 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 347 | struct sta_info *ap; |
| 348 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 349 | /* |
| 350 | * We're getting a sta pointer in, |
| 351 | * so must be under RCU read lock. |
| 352 | */ |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 353 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 354 | /* same here, the AP could be using QoS */ |
| 355 | ap = sta_info_get(key->local, key->sdata->u.sta.bssid); |
| 356 | if (ap) { |
Johannes Berg | 07346f81 | 2008-05-03 01:02:02 +0200 | [diff] [blame] | 357 | if (test_sta_flags(ap, WLAN_STA_WME)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 358 | key->conf.flags |= |
| 359 | IEEE80211_KEY_FLAG_WMM_STA; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 360 | } |
| 361 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 364 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 365 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 366 | if (sta) |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 367 | old_key = sta->key; |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 368 | else |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 369 | old_key = sdata->keys[idx]; |
| 370 | |
| 371 | __ieee80211_key_replace(sdata, sta, old_key, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 372 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 373 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 374 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 375 | /* free old key later */ |
| 376 | add_todo(old_key, KEY_FLAG_TODO_DELETE); |
| 377 | |
| 378 | add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 379 | if (netif_running(sdata->dev)) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 380 | add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD); |
| 381 | } |
| 382 | |
| 383 | static void __ieee80211_key_free(struct ieee80211_key *key) |
| 384 | { |
| 385 | /* |
| 386 | * Replace key with nothingness if it was ever used. |
| 387 | */ |
| 388 | if (key->sdata) |
| 389 | __ieee80211_key_replace(key->sdata, key->sta, |
| 390 | key, NULL); |
| 391 | |
| 392 | add_todo(key, KEY_FLAG_TODO_DELETE); |
Johannes Berg | 1f5a7e47 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Johannes Berg | 1f5a7e47 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 395 | void ieee80211_key_free(struct ieee80211_key *key) |
| 396 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 397 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 398 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 399 | if (!key) |
| 400 | return; |
| 401 | |
Emmanuel Grumbach | 00eb7fe | 2008-06-26 12:13:46 +0300 | [diff] [blame] | 402 | if (!key->sdata) { |
| 403 | /* The key has not been linked yet, simply free it |
| 404 | * and don't Oops */ |
| 405 | if (key->conf.alg == ALG_CCMP) |
| 406 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 407 | kfree(key); |
| 408 | return; |
| 409 | } |
| 410 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 411 | spin_lock_irqsave(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 412 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 413 | spin_unlock_irqrestore(&key->sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 414 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 415 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 416 | /* |
| 417 | * To be safe against concurrent manipulations of the list (which shouldn't |
| 418 | * actually happen) we need to hold the spinlock. But under the spinlock we |
| 419 | * can't actually do much, so we defer processing to the todo list. Then run |
| 420 | * the todo list to be sure the operation and possibly previously pending |
| 421 | * operations are completed. |
| 422 | */ |
| 423 | static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata, |
| 424 | u32 todo_flags) |
| 425 | { |
| 426 | struct ieee80211_key *key; |
| 427 | unsigned long flags; |
| 428 | |
| 429 | might_sleep(); |
| 430 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 431 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 432 | list_for_each_entry(key, &sdata->key_list, list) |
| 433 | add_todo(key, todo_flags); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 434 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 435 | |
| 436 | ieee80211_key_todo(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 440 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 441 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 442 | |
| 443 | if (WARN_ON(!netif_running(sdata->dev))) |
| 444 | return; |
| 445 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 446 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 450 | { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 451 | ASSERT_RTNL(); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 452 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 453 | ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 456 | static void __ieee80211_key_destroy(struct ieee80211_key *key) |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 457 | { |
| 458 | if (!key) |
| 459 | return; |
| 460 | |
| 461 | ieee80211_key_disable_hw_accel(key); |
| 462 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 463 | if (key->conf.alg == ALG_CCMP) |
| 464 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 465 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 466 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 467 | kfree(key); |
Johannes Berg | 1f5a7e47 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 468 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 469 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 470 | static void __ieee80211_key_todo(void) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 471 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 472 | struct ieee80211_key *key; |
| 473 | bool work_done; |
| 474 | u32 todoflags; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 475 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 476 | /* |
| 477 | * NB: sta_info_destroy relies on this! |
| 478 | */ |
| 479 | synchronize_rcu(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 480 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 481 | spin_lock(&todo_lock); |
| 482 | while (!list_empty(&todo_list)) { |
| 483 | key = list_first_entry(&todo_list, struct ieee80211_key, todo); |
| 484 | list_del_init(&key->todo); |
| 485 | todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS | |
| 486 | KEY_FLAG_TODO_DEFKEY | |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 487 | KEY_FLAG_TODO_HWACCEL_ADD | |
| 488 | KEY_FLAG_TODO_HWACCEL_REMOVE | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 489 | KEY_FLAG_TODO_DELETE); |
| 490 | key->flags &= ~todoflags; |
| 491 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 492 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 493 | work_done = false; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 494 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 495 | if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) { |
| 496 | ieee80211_debugfs_key_add(key); |
| 497 | work_done = true; |
| 498 | } |
| 499 | if (todoflags & KEY_FLAG_TODO_DEFKEY) { |
| 500 | ieee80211_debugfs_key_remove_default(key->sdata); |
| 501 | ieee80211_debugfs_key_add_default(key->sdata); |
| 502 | work_done = true; |
| 503 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 504 | if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 505 | ieee80211_key_enable_hw_accel(key); |
| 506 | work_done = true; |
| 507 | } |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 508 | if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) { |
| 509 | ieee80211_key_disable_hw_accel(key); |
| 510 | work_done = true; |
| 511 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 512 | if (todoflags & KEY_FLAG_TODO_DELETE) { |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 513 | __ieee80211_key_destroy(key); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 514 | work_done = true; |
| 515 | } |
| 516 | |
| 517 | WARN_ON(!work_done); |
| 518 | |
| 519 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 520 | } |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 521 | spin_unlock(&todo_lock); |
| 522 | } |
| 523 | |
| 524 | void ieee80211_key_todo(void) |
| 525 | { |
| 526 | ieee80211_key_lock(); |
| 527 | __ieee80211_key_todo(); |
| 528 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 532 | { |
| 533 | struct ieee80211_key *key, *tmp; |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 534 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 535 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 536 | ieee80211_key_lock(); |
| 537 | |
| 538 | ieee80211_debugfs_key_remove_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 539 | |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 540 | spin_lock_irqsave(&sdata->local->key_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 541 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
Johannes Berg | 3a24576 | 2008-04-09 16:45:37 +0200 | [diff] [blame] | 542 | __ieee80211_key_free(key); |
Johannes Berg | b16bd15 | 2008-04-11 21:40:35 +0200 | [diff] [blame] | 543 | spin_unlock_irqrestore(&sdata->local->key_lock, flags); |
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 | __ieee80211_key_todo(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 546 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame] | 547 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 548 | } |