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" |
| 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; |
| 77 | /* only add if not already added */ |
| 78 | if (list_empty(&key->todo)) |
| 79 | list_add(&key->todo, &todo_list); |
| 80 | schedule_work(&todo_work); |
| 81 | spin_unlock(&todo_lock); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * ieee80211_key_lock - lock the mac80211 key operation lock |
| 86 | * |
| 87 | * This locks the (global) mac80211 key operation lock, all |
| 88 | * key operations must be done under this lock. |
| 89 | */ |
| 90 | static void ieee80211_key_lock(void) |
| 91 | { |
| 92 | mutex_lock(&key_mutex); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * ieee80211_key_unlock - unlock the mac80211 key operation lock |
| 97 | */ |
| 98 | static void ieee80211_key_unlock(void) |
| 99 | { |
| 100 | mutex_unlock(&key_mutex); |
| 101 | } |
| 102 | |
| 103 | static void assert_key_lock(void) |
| 104 | { |
| 105 | WARN_ON(!mutex_is_locked(&key_mutex)); |
| 106 | } |
| 107 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 108 | static const u8 *get_mac_for_key(struct ieee80211_key *key) |
| 109 | { |
| 110 | const u8 *addr = bcast_addr; |
| 111 | |
| 112 | /* |
| 113 | * If we're an AP we won't ever receive frames with a non-WEP |
| 114 | * group key so we tell the driver that by using the zero MAC |
| 115 | * address to indicate a transmit-only key. |
| 116 | */ |
| 117 | if (key->conf.alg != ALG_WEP && |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 118 | (key->sdata->vif.type == IEEE80211_IF_TYPE_AP || |
| 119 | key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 120 | addr = zero_addr; |
| 121 | |
| 122 | if (key->sta) |
| 123 | addr = key->sta->addr; |
| 124 | |
| 125 | return addr; |
| 126 | } |
| 127 | |
| 128 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 129 | { |
| 130 | const u8 *addr; |
| 131 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 132 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 133 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 134 | assert_key_lock(); |
| 135 | might_sleep(); |
| 136 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 137 | if (!key->local->ops->set_key) |
| 138 | return; |
| 139 | |
| 140 | addr = get_mac_for_key(key); |
| 141 | |
| 142 | ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY, |
| 143 | key->sdata->dev->dev_addr, addr, |
| 144 | &key->conf); |
| 145 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 146 | if (!ret) { |
| 147 | spin_lock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 148 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 149 | spin_unlock(&todo_lock); |
| 150 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 151 | |
| 152 | if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP) |
| 153 | printk(KERN_ERR "mac80211-%s: failed to set key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 154 | "(%d, %s) to hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 155 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 156 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) |
| 160 | { |
| 161 | const u8 *addr; |
| 162 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 163 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 164 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 165 | assert_key_lock(); |
| 166 | might_sleep(); |
| 167 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 168 | if (!key || !key->local->ops->set_key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 169 | return; |
| 170 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 171 | spin_lock(&todo_lock); |
| 172 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) { |
| 173 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 174 | return; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 175 | } |
| 176 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 177 | |
| 178 | addr = get_mac_for_key(key); |
| 179 | |
| 180 | ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY, |
| 181 | key->sdata->dev->dev_addr, addr, |
| 182 | &key->conf); |
| 183 | |
| 184 | if (ret) |
| 185 | printk(KERN_ERR "mac80211-%s: failed to remove key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 186 | "(%d, %s) from hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 187 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 188 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 189 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 190 | spin_lock(&todo_lock); |
| 191 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
| 192 | spin_unlock(&todo_lock); |
| 193 | } |
| 194 | |
| 195 | static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, |
| 196 | int idx) |
| 197 | { |
| 198 | struct ieee80211_key *key = NULL; |
| 199 | |
| 200 | if (idx >= 0 && idx < NUM_DEFAULT_KEYS) |
| 201 | key = sdata->keys[idx]; |
| 202 | |
| 203 | rcu_assign_pointer(sdata->default_key, key); |
| 204 | |
| 205 | if (key) |
| 206 | add_todo(key, KEY_FLAG_TODO_DEFKEY); |
| 207 | } |
| 208 | |
| 209 | void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx) |
| 210 | { |
| 211 | unsigned long flags; |
| 212 | |
| 213 | spin_lock_irqsave(&sdata->local->sta_lock, flags); |
| 214 | __ieee80211_set_default_key(sdata, idx); |
| 215 | spin_unlock_irqrestore(&sdata->local->sta_lock, flags); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, |
| 220 | struct sta_info *sta, |
| 221 | struct ieee80211_key *old, |
| 222 | struct ieee80211_key *new) |
| 223 | { |
| 224 | int idx, defkey; |
| 225 | |
| 226 | if (new) |
| 227 | list_add(&new->list, &sdata->key_list); |
| 228 | |
| 229 | if (sta) { |
| 230 | rcu_assign_pointer(sta->key, new); |
| 231 | } else { |
| 232 | WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); |
| 233 | |
| 234 | if (old) |
| 235 | idx = old->conf.keyidx; |
| 236 | else |
| 237 | idx = new->conf.keyidx; |
| 238 | |
| 239 | defkey = old && sdata->default_key == old; |
| 240 | |
| 241 | if (defkey && !new) |
| 242 | __ieee80211_set_default_key(sdata, -1); |
| 243 | |
| 244 | rcu_assign_pointer(sdata->keys[idx], new); |
| 245 | if (defkey && new) |
| 246 | __ieee80211_set_default_key(sdata, new->conf.keyidx); |
| 247 | } |
| 248 | |
| 249 | if (old) { |
| 250 | /* |
| 251 | * We'll use an empty list to indicate that the key |
| 252 | * has already been removed. |
| 253 | */ |
| 254 | list_del_init(&old->list); |
| 255 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 258 | struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg, |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 259 | int idx, |
| 260 | size_t key_len, |
| 261 | const u8 *key_data) |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 262 | { |
| 263 | struct ieee80211_key *key; |
| 264 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 265 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 266 | |
| 267 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 268 | if (!key) |
| 269 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Default to software encryption; we'll later upload the |
| 273 | * key to the hardware if possible. |
| 274 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 275 | key->conf.flags = 0; |
| 276 | key->flags = 0; |
| 277 | |
| 278 | key->conf.alg = alg; |
| 279 | key->conf.keyidx = idx; |
| 280 | key->conf.keylen = key_len; |
| 281 | memcpy(key->conf.key, key_data, key_len); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 282 | INIT_LIST_HEAD(&key->list); |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 283 | INIT_LIST_HEAD(&key->todo); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 284 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 285 | if (alg == ALG_CCMP) { |
| 286 | /* |
| 287 | * Initialize AES key state here as an optimization so that |
| 288 | * it does not need to be initialized for every packet. |
| 289 | */ |
| 290 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 291 | if (!key->u.ccmp.tfm) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 292 | kfree(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | } |
| 296 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 297 | return key; |
| 298 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 299 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 300 | void ieee80211_key_link(struct ieee80211_key *key, |
| 301 | struct ieee80211_sub_if_data *sdata, |
| 302 | struct sta_info *sta) |
| 303 | { |
| 304 | struct ieee80211_key *old_key; |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 305 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 306 | int idx; |
| 307 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 308 | BUG_ON(!sdata); |
| 309 | BUG_ON(!key); |
| 310 | |
| 311 | idx = key->conf.keyidx; |
| 312 | key->local = sdata->local; |
| 313 | key->sdata = sdata; |
| 314 | key->sta = sta; |
| 315 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 316 | if (sta) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 317 | /* |
| 318 | * some hardware cannot handle TKIP with QoS, so |
| 319 | * we indicate whether QoS could be in use. |
| 320 | */ |
| 321 | if (sta->flags & WLAN_STA_WME) |
| 322 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
| 323 | } else { |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 324 | if (sdata->vif.type == IEEE80211_IF_TYPE_STA) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 325 | struct sta_info *ap; |
| 326 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 327 | /* |
| 328 | * We're getting a sta pointer in, |
| 329 | * so must be under RCU read lock. |
| 330 | */ |
Johannes Berg | d0709a6 | 2008-02-25 16:27:46 +0100 | [diff] [blame] | 331 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 332 | /* same here, the AP could be using QoS */ |
| 333 | ap = sta_info_get(key->local, key->sdata->u.sta.bssid); |
| 334 | if (ap) { |
| 335 | if (ap->flags & WLAN_STA_WME) |
| 336 | key->conf.flags |= |
| 337 | IEEE80211_KEY_FLAG_WMM_STA; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 338 | } |
| 339 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 342 | spin_lock_irqsave(&sdata->local->sta_lock, flags); |
| 343 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 344 | if (sta) |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 345 | old_key = sta->key; |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 346 | else |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 347 | old_key = sdata->keys[idx]; |
| 348 | |
| 349 | __ieee80211_key_replace(sdata, sta, old_key, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 350 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 351 | spin_unlock_irqrestore(&sdata->local->sta_lock, flags); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 352 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 353 | /* free old key later */ |
| 354 | add_todo(old_key, KEY_FLAG_TODO_DELETE); |
| 355 | |
| 356 | add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS); |
Johannes Berg | e4861829 | 2008-02-27 13:39:00 +0100 | [diff] [blame] | 357 | if (netif_running(sdata->dev)) |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 358 | add_todo(key, KEY_FLAG_TODO_HWACCEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 361 | void ieee80211_key_free(struct ieee80211_key *key) |
| 362 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 363 | unsigned long flags; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 364 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 365 | if (!key) |
| 366 | return; |
| 367 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 368 | /* |
| 369 | * Replace key with nothingness if it was ever used. |
| 370 | */ |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 371 | if (key->sdata) { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 372 | spin_lock_irqsave(&key->sdata->local->sta_lock, flags); |
| 373 | __ieee80211_key_replace(key->sdata, key->sta, |
| 374 | key, NULL); |
| 375 | spin_unlock_irqrestore(&key->sdata->local->sta_lock, flags); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 378 | add_todo(key, KEY_FLAG_TODO_DELETE); |
| 379 | } |
| 380 | |
| 381 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 382 | { |
| 383 | struct ieee80211_key *key; |
| 384 | |
| 385 | might_sleep(); |
| 386 | |
| 387 | if (WARN_ON(!netif_running(sdata->dev))) |
| 388 | return; |
| 389 | |
| 390 | ieee80211_key_lock(); |
| 391 | |
| 392 | list_for_each_entry(key, &sdata->key_list, list) |
| 393 | ieee80211_key_enable_hw_accel(key); |
| 394 | |
| 395 | ieee80211_key_unlock(); |
| 396 | } |
| 397 | |
| 398 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 399 | { |
| 400 | struct ieee80211_key *key; |
| 401 | |
| 402 | might_sleep(); |
| 403 | |
| 404 | ieee80211_key_lock(); |
| 405 | |
| 406 | list_for_each_entry(key, &sdata->key_list, list) |
| 407 | ieee80211_key_disable_hw_accel(key); |
| 408 | |
| 409 | ieee80211_key_unlock(); |
| 410 | } |
| 411 | |
| 412 | static void __ieee80211_key_free(struct ieee80211_key *key) |
| 413 | { |
| 414 | if (!key) |
| 415 | return; |
| 416 | |
| 417 | ieee80211_key_disable_hw_accel(key); |
| 418 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 419 | if (key->conf.alg == ALG_CCMP) |
| 420 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 421 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 422 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 423 | kfree(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 424 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 425 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 426 | static void __ieee80211_key_todo(void) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 427 | { |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 428 | struct ieee80211_key *key; |
| 429 | bool work_done; |
| 430 | u32 todoflags; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 431 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 432 | /* |
| 433 | * NB: sta_info_destroy relies on this! |
| 434 | */ |
| 435 | synchronize_rcu(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 436 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 437 | spin_lock(&todo_lock); |
| 438 | while (!list_empty(&todo_list)) { |
| 439 | key = list_first_entry(&todo_list, struct ieee80211_key, todo); |
| 440 | list_del_init(&key->todo); |
| 441 | todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS | |
| 442 | KEY_FLAG_TODO_DEFKEY | |
| 443 | KEY_FLAG_TODO_HWACCEL | |
| 444 | KEY_FLAG_TODO_DELETE); |
| 445 | key->flags &= ~todoflags; |
| 446 | spin_unlock(&todo_lock); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 447 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 448 | work_done = false; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 449 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 450 | if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) { |
| 451 | ieee80211_debugfs_key_add(key); |
| 452 | work_done = true; |
| 453 | } |
| 454 | if (todoflags & KEY_FLAG_TODO_DEFKEY) { |
| 455 | ieee80211_debugfs_key_remove_default(key->sdata); |
| 456 | ieee80211_debugfs_key_add_default(key->sdata); |
| 457 | work_done = true; |
| 458 | } |
| 459 | if (todoflags & KEY_FLAG_TODO_HWACCEL) { |
| 460 | ieee80211_key_enable_hw_accel(key); |
| 461 | work_done = true; |
| 462 | } |
| 463 | if (todoflags & KEY_FLAG_TODO_DELETE) { |
| 464 | __ieee80211_key_free(key); |
| 465 | work_done = true; |
| 466 | } |
| 467 | |
| 468 | WARN_ON(!work_done); |
| 469 | |
| 470 | spin_lock(&todo_lock); |
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 | spin_unlock(&todo_lock); |
| 473 | } |
| 474 | |
| 475 | void ieee80211_key_todo(void) |
| 476 | { |
| 477 | ieee80211_key_lock(); |
| 478 | __ieee80211_key_todo(); |
| 479 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 483 | { |
| 484 | struct ieee80211_key *key, *tmp; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame] | 485 | LIST_HEAD(tmp_list); |
| 486 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 487 | ieee80211_key_lock(); |
| 488 | |
| 489 | ieee80211_debugfs_key_remove_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 490 | |
| 491 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
| 492 | ieee80211_key_free(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 493 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 494 | __ieee80211_key_todo(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 495 | |
Johannes Berg | 3b96766 | 2008-04-08 17:56:52 +0200 | [diff] [blame^] | 496 | ieee80211_key_unlock(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 497 | } |