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 | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 5 | * Copyright 2007 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 | |
| 23 | /* |
| 24 | * Key handling basics |
| 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 | * |
| 36 | * All operations here are called under RTNL so no extra locking is |
| 37 | * required. |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 38 | * |
| 39 | * NOTE: This code requires that sta info *destruction* is done under |
| 40 | * RTNL, otherwise it can try to access already freed STA structs |
| 41 | * when a STA key is being freed. |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 45 | static const u8 zero_addr[ETH_ALEN]; |
| 46 | |
| 47 | static const u8 *get_mac_for_key(struct ieee80211_key *key) |
| 48 | { |
| 49 | const u8 *addr = bcast_addr; |
| 50 | |
| 51 | /* |
| 52 | * If we're an AP we won't ever receive frames with a non-WEP |
| 53 | * group key so we tell the driver that by using the zero MAC |
| 54 | * address to indicate a transmit-only key. |
| 55 | */ |
| 56 | if (key->conf.alg != ALG_WEP && |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 57 | (key->sdata->vif.type == IEEE80211_IF_TYPE_AP || |
| 58 | key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 59 | addr = zero_addr; |
| 60 | |
| 61 | if (key->sta) |
| 62 | addr = key->sta->addr; |
| 63 | |
| 64 | return addr; |
| 65 | } |
| 66 | |
| 67 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 68 | { |
| 69 | const u8 *addr; |
| 70 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 71 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 72 | |
| 73 | if (!key->local->ops->set_key) |
| 74 | return; |
| 75 | |
| 76 | addr = get_mac_for_key(key); |
| 77 | |
| 78 | ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY, |
| 79 | key->sdata->dev->dev_addr, addr, |
| 80 | &key->conf); |
| 81 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 82 | if (!ret) |
| 83 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
| 84 | |
| 85 | if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP) |
| 86 | printk(KERN_ERR "mac80211-%s: failed to set key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 87 | "(%d, %s) to hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 88 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 89 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 92 | static void ieee80211_key_mark_hw_accel_off(struct ieee80211_key *key) |
| 93 | { |
| 94 | if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { |
| 95 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
| 96 | key->flags |= KEY_FLAG_REMOVE_FROM_HARDWARE; |
| 97 | } |
| 98 | } |
| 99 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 100 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) |
| 101 | { |
| 102 | const u8 *addr; |
| 103 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 104 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 105 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 106 | if (!key || !key->local->ops->set_key) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 107 | return; |
| 108 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 109 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) && |
| 110 | !(key->flags & KEY_FLAG_REMOVE_FROM_HARDWARE)) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 111 | return; |
| 112 | |
| 113 | addr = get_mac_for_key(key); |
| 114 | |
| 115 | ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY, |
| 116 | key->sdata->dev->dev_addr, addr, |
| 117 | &key->conf); |
| 118 | |
| 119 | if (ret) |
| 120 | printk(KERN_ERR "mac80211-%s: failed to remove key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 121 | "(%d, %s) from hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 122 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 123 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 124 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 125 | key->flags &= ~(KEY_FLAG_UPLOADED_TO_HARDWARE | |
| 126 | KEY_FLAG_REMOVE_FROM_HARDWARE); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 129 | struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg, |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 130 | int idx, |
| 131 | size_t key_len, |
| 132 | const u8 *key_data) |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 133 | { |
| 134 | struct ieee80211_key *key; |
| 135 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 136 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 137 | |
| 138 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 139 | if (!key) |
| 140 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Default to software encryption; we'll later upload the |
| 144 | * key to the hardware if possible. |
| 145 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 146 | key->conf.flags = 0; |
| 147 | key->flags = 0; |
| 148 | |
| 149 | key->conf.alg = alg; |
| 150 | key->conf.keyidx = idx; |
| 151 | key->conf.keylen = key_len; |
| 152 | memcpy(key->conf.key, key_data, key_len); |
| 153 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 154 | if (alg == ALG_CCMP) { |
| 155 | /* |
| 156 | * Initialize AES key state here as an optimization so that |
| 157 | * it does not need to be initialized for every packet. |
| 158 | */ |
| 159 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 160 | if (!key->u.ccmp.tfm) { |
| 161 | ieee80211_key_free(key); |
| 162 | return NULL; |
| 163 | } |
| 164 | } |
| 165 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 166 | return key; |
| 167 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 168 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 169 | static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, |
| 170 | struct sta_info *sta, |
| 171 | struct ieee80211_key *key, |
| 172 | struct ieee80211_key *new) |
| 173 | { |
| 174 | int idx, defkey; |
| 175 | |
| 176 | if (sta) { |
| 177 | rcu_assign_pointer(sta->key, new); |
| 178 | } else { |
| 179 | WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx); |
| 180 | |
| 181 | if (key) |
| 182 | idx = key->conf.keyidx; |
| 183 | else |
| 184 | idx = new->conf.keyidx; |
| 185 | |
| 186 | defkey = key && sdata->default_key == key; |
| 187 | |
| 188 | if (defkey && !new) |
| 189 | ieee80211_set_default_key(sdata, -1); |
| 190 | |
| 191 | rcu_assign_pointer(sdata->keys[idx], new); |
| 192 | |
| 193 | if (defkey && new) |
| 194 | ieee80211_set_default_key(sdata, new->conf.keyidx); |
| 195 | } |
| 196 | |
| 197 | if (key) { |
| 198 | ieee80211_key_mark_hw_accel_off(key); |
| 199 | list_del(&key->list); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void ieee80211_key_link(struct ieee80211_key *key, |
| 204 | struct ieee80211_sub_if_data *sdata, |
| 205 | struct sta_info *sta) |
| 206 | { |
| 207 | struct ieee80211_key *old_key; |
| 208 | int idx; |
| 209 | |
| 210 | ASSERT_RTNL(); |
| 211 | might_sleep(); |
| 212 | |
| 213 | BUG_ON(!sdata); |
| 214 | BUG_ON(!key); |
| 215 | |
| 216 | idx = key->conf.keyidx; |
| 217 | key->local = sdata->local; |
| 218 | key->sdata = sdata; |
| 219 | key->sta = sta; |
| 220 | |
| 221 | ieee80211_debugfs_key_add(key->local, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 222 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 223 | if (sta) { |
| 224 | ieee80211_debugfs_key_sta_link(key, sta); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 225 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 226 | /* |
| 227 | * some hardware cannot handle TKIP with QoS, so |
| 228 | * we indicate whether QoS could be in use. |
| 229 | */ |
| 230 | if (sta->flags & WLAN_STA_WME) |
| 231 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
| 232 | } else { |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 233 | if (sdata->vif.type == IEEE80211_IF_TYPE_STA) { |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 234 | struct sta_info *ap; |
| 235 | |
| 236 | /* same here, the AP could be using QoS */ |
| 237 | ap = sta_info_get(key->local, key->sdata->u.sta.bssid); |
| 238 | if (ap) { |
| 239 | if (ap->flags & WLAN_STA_WME) |
| 240 | key->conf.flags |= |
| 241 | IEEE80211_KEY_FLAG_WMM_STA; |
| 242 | sta_info_put(ap); |
| 243 | } |
| 244 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 247 | if (sta) |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 248 | old_key = sta->key; |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 249 | else |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 250 | old_key = sdata->keys[idx]; |
| 251 | |
| 252 | __ieee80211_key_replace(sdata, sta, old_key, key); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 253 | |
| 254 | list_add(&key->list, &sdata->key_list); |
| 255 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 256 | synchronize_rcu(); |
| 257 | |
| 258 | ieee80211_key_free(old_key); |
| 259 | ieee80211_key_enable_hw_accel(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 262 | void ieee80211_key_free(struct ieee80211_key *key) |
| 263 | { |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 264 | ASSERT_RTNL(); |
| 265 | might_sleep(); |
| 266 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 267 | if (!key) |
| 268 | return; |
| 269 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 270 | if (key->sdata) { |
| 271 | /* |
| 272 | * Replace key with nothingness. |
| 273 | * |
| 274 | * Because other code may have key reference (RCU protected) |
| 275 | * right now, we then wait for a grace period before freeing |
| 276 | * it. |
| 277 | */ |
| 278 | __ieee80211_key_replace(key->sdata, key->sta, key, NULL); |
| 279 | |
| 280 | synchronize_rcu(); |
| 281 | |
| 282 | /* |
| 283 | * Remove from hwaccel if appropriate, this will |
| 284 | * only happen when the key is actually unlinked, |
| 285 | * it will already be done when the key was replaced. |
| 286 | */ |
| 287 | ieee80211_key_disable_hw_accel(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 290 | if (key->conf.alg == ALG_CCMP) |
| 291 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 292 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 293 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 294 | kfree(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 295 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 296 | |
| 297 | void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx) |
| 298 | { |
| 299 | struct ieee80211_key *key = NULL; |
| 300 | |
| 301 | if (idx >= 0 && idx < NUM_DEFAULT_KEYS) |
| 302 | key = sdata->keys[idx]; |
| 303 | |
| 304 | if (sdata->default_key != key) { |
| 305 | ieee80211_debugfs_key_remove_default(sdata); |
| 306 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 307 | rcu_assign_pointer(sdata->default_key, key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 308 | |
| 309 | if (sdata->default_key) |
| 310 | ieee80211_debugfs_key_add_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 315 | { |
| 316 | struct ieee80211_key *key, *tmp; |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 317 | LIST_HEAD(tmp_list); |
| 318 | |
| 319 | ASSERT_RTNL(); |
| 320 | might_sleep(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 321 | |
| 322 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
| 323 | ieee80211_key_free(key); |
| 324 | } |
| 325 | |
| 326 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 327 | { |
| 328 | struct ieee80211_key *key; |
| 329 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 330 | ASSERT_RTNL(); |
| 331 | might_sleep(); |
| 332 | |
| 333 | if (WARN_ON(!netif_running(sdata->dev))) |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 334 | return; |
| 335 | |
| 336 | list_for_each_entry(key, &sdata->key_list, list) |
| 337 | ieee80211_key_enable_hw_accel(key); |
| 338 | } |
| 339 | |
| 340 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 341 | { |
| 342 | struct ieee80211_key *key; |
| 343 | |
Johannes Berg | db4d116 | 2008-02-25 16:27:45 +0100 | [diff] [blame^] | 344 | ASSERT_RTNL(); |
| 345 | might_sleep(); |
| 346 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 347 | list_for_each_entry(key, &sdata->key_list, list) |
| 348 | ieee80211_key_disable_hw_accel(key); |
| 349 | } |