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