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 | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 16 | #include <net/mac80211.h> |
| 17 | #include "ieee80211_i.h" |
| 18 | #include "debugfs_key.h" |
| 19 | #include "aes_ccm.h" |
| 20 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Key handling basics |
| 24 | * |
| 25 | * Key handling in mac80211 is done based on per-interface (sub_if_data) |
| 26 | * keys and per-station keys. Since each station belongs to an interface, |
| 27 | * each station key also belongs to that interface. |
| 28 | * |
| 29 | * Hardware acceleration is done on a best-effort basis, for each key |
| 30 | * that is eligible the hardware is asked to enable that key but if |
| 31 | * it cannot do that they key is simply kept for software encryption. |
| 32 | * There is currently no way of knowing this except by looking into |
| 33 | * debugfs. |
| 34 | * |
| 35 | * All operations here are called under RTNL so no extra locking is |
| 36 | * required. |
| 37 | */ |
| 38 | |
| 39 | static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 40 | static const u8 zero_addr[ETH_ALEN]; |
| 41 | |
| 42 | static const u8 *get_mac_for_key(struct ieee80211_key *key) |
| 43 | { |
| 44 | const u8 *addr = bcast_addr; |
| 45 | |
| 46 | /* |
| 47 | * If we're an AP we won't ever receive frames with a non-WEP |
| 48 | * group key so we tell the driver that by using the zero MAC |
| 49 | * address to indicate a transmit-only key. |
| 50 | */ |
| 51 | if (key->conf.alg != ALG_WEP && |
| 52 | (key->sdata->type == IEEE80211_IF_TYPE_AP || |
| 53 | key->sdata->type == IEEE80211_IF_TYPE_VLAN)) |
| 54 | addr = zero_addr; |
| 55 | |
| 56 | if (key->sta) |
| 57 | addr = key->sta->addr; |
| 58 | |
| 59 | return addr; |
| 60 | } |
| 61 | |
| 62 | static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key) |
| 63 | { |
| 64 | const u8 *addr; |
| 65 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 66 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 67 | |
| 68 | if (!key->local->ops->set_key) |
| 69 | return; |
| 70 | |
| 71 | addr = get_mac_for_key(key); |
| 72 | |
| 73 | ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY, |
| 74 | key->sdata->dev->dev_addr, addr, |
| 75 | &key->conf); |
| 76 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 77 | if (!ret) |
| 78 | key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; |
| 79 | |
| 80 | if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP) |
| 81 | printk(KERN_ERR "mac80211-%s: failed to set key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 82 | "(%d, %s) to hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 83 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 84 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) |
| 88 | { |
| 89 | const u8 *addr; |
| 90 | int ret; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 91 | DECLARE_MAC_BUF(mac); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 92 | |
| 93 | if (!key->local->ops->set_key) |
| 94 | return; |
| 95 | |
| 96 | if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) |
| 97 | return; |
| 98 | |
| 99 | addr = get_mac_for_key(key); |
| 100 | |
| 101 | ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY, |
| 102 | key->sdata->dev->dev_addr, addr, |
| 103 | &key->conf); |
| 104 | |
| 105 | if (ret) |
| 106 | printk(KERN_ERR "mac80211-%s: failed to remove key " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 107 | "(%d, %s) from hardware (%d)\n", |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 108 | wiphy_name(key->local->hw.wiphy), |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame^] | 109 | key->conf.keyidx, print_mac(mac, addr), ret); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 110 | |
| 111 | key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 114 | struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 115 | struct sta_info *sta, |
| 116 | ieee80211_key_alg alg, |
| 117 | int idx, |
| 118 | size_t key_len, |
| 119 | const u8 *key_data) |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 120 | { |
| 121 | struct ieee80211_key *key; |
| 122 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 123 | BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 124 | BUG_ON(alg == ALG_NONE); |
| 125 | |
| 126 | key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 127 | if (!key) |
| 128 | return NULL; |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Default to software encryption; we'll later upload the |
| 132 | * key to the hardware if possible. |
| 133 | */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 134 | key->conf.flags = 0; |
| 135 | key->flags = 0; |
| 136 | |
| 137 | key->conf.alg = alg; |
| 138 | key->conf.keyidx = idx; |
| 139 | key->conf.keylen = key_len; |
| 140 | memcpy(key->conf.key, key_data, key_len); |
| 141 | |
| 142 | key->local = sdata->local; |
| 143 | key->sdata = sdata; |
| 144 | key->sta = sta; |
| 145 | |
| 146 | if (alg == ALG_CCMP) { |
| 147 | /* |
| 148 | * Initialize AES key state here as an optimization so that |
| 149 | * it does not need to be initialized for every packet. |
| 150 | */ |
| 151 | key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data); |
| 152 | if (!key->u.ccmp.tfm) { |
| 153 | ieee80211_key_free(key); |
| 154 | return NULL; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | ieee80211_debugfs_key_add(key->local, key); |
| 159 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 160 | /* remove key first */ |
| 161 | if (sta) |
| 162 | ieee80211_key_free(sta->key); |
| 163 | else |
| 164 | ieee80211_key_free(sdata->keys[idx]); |
| 165 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 166 | if (sta) { |
| 167 | ieee80211_debugfs_key_sta_link(key, sta); |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 168 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 169 | /* |
| 170 | * some hardware cannot handle TKIP with QoS, so |
| 171 | * we indicate whether QoS could be in use. |
| 172 | */ |
| 173 | if (sta->flags & WLAN_STA_WME) |
| 174 | key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA; |
| 175 | } else { |
| 176 | if (sdata->type == IEEE80211_IF_TYPE_STA) { |
| 177 | struct sta_info *ap; |
| 178 | |
| 179 | /* same here, the AP could be using QoS */ |
| 180 | ap = sta_info_get(key->local, key->sdata->u.sta.bssid); |
| 181 | if (ap) { |
| 182 | if (ap->flags & WLAN_STA_WME) |
| 183 | key->conf.flags |= |
| 184 | IEEE80211_KEY_FLAG_WMM_STA; |
| 185 | sta_info_put(ap); |
| 186 | } |
| 187 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 190 | /* enable hwaccel if appropriate */ |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 191 | if (netif_running(key->sdata->dev)) |
| 192 | ieee80211_key_enable_hw_accel(key); |
| 193 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 194 | if (sta) |
| 195 | rcu_assign_pointer(sta->key, key); |
| 196 | else |
| 197 | rcu_assign_pointer(sdata->keys[idx], key); |
| 198 | |
| 199 | list_add(&key->list, &sdata->key_list); |
| 200 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 201 | return key; |
| 202 | } |
| 203 | |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 204 | void ieee80211_key_free(struct ieee80211_key *key) |
| 205 | { |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 206 | if (!key) |
| 207 | return; |
| 208 | |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 209 | if (key->sta) { |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 210 | rcu_assign_pointer(key->sta->key, NULL); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 211 | } else { |
| 212 | if (key->sdata->default_key == key) |
| 213 | ieee80211_set_default_key(key->sdata, -1); |
| 214 | if (key->conf.keyidx >= 0 && |
| 215 | key->conf.keyidx < NUM_DEFAULT_KEYS) |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 216 | rcu_assign_pointer(key->sdata->keys[key->conf.keyidx], |
| 217 | NULL); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 218 | else |
| 219 | WARN_ON(1); |
| 220 | } |
| 221 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 222 | /* wait for all key users to complete */ |
| 223 | synchronize_rcu(); |
| 224 | |
| 225 | /* remove from hwaccel if appropriate */ |
| 226 | ieee80211_key_disable_hw_accel(key); |
| 227 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 228 | if (key->conf.alg == ALG_CCMP) |
| 229 | ieee80211_aes_key_free(key->u.ccmp.tfm); |
| 230 | ieee80211_debugfs_key_remove(key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 231 | |
| 232 | list_del(&key->list); |
| 233 | |
Johannes Berg | 8f37171 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 234 | kfree(key); |
Johannes Berg | 1f5a7e4 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 235 | } |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 236 | |
| 237 | void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx) |
| 238 | { |
| 239 | struct ieee80211_key *key = NULL; |
| 240 | |
| 241 | if (idx >= 0 && idx < NUM_DEFAULT_KEYS) |
| 242 | key = sdata->keys[idx]; |
| 243 | |
| 244 | if (sdata->default_key != key) { |
| 245 | ieee80211_debugfs_key_remove_default(sdata); |
| 246 | |
Johannes Berg | d4e46a3 | 2007-09-14 11:10:24 -0400 | [diff] [blame] | 247 | rcu_assign_pointer(sdata->default_key, key); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 248 | |
| 249 | if (sdata->default_key) |
| 250 | ieee80211_debugfs_key_add_default(sdata); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata) |
| 255 | { |
| 256 | struct ieee80211_key *key, *tmp; |
| 257 | |
| 258 | list_for_each_entry_safe(key, tmp, &sdata->key_list, list) |
| 259 | ieee80211_key_free(key); |
| 260 | } |
| 261 | |
| 262 | void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata) |
| 263 | { |
| 264 | struct ieee80211_key *key; |
| 265 | |
| 266 | WARN_ON(!netif_running(sdata->dev)); |
| 267 | if (!netif_running(sdata->dev)) |
| 268 | return; |
| 269 | |
| 270 | list_for_each_entry(key, &sdata->key_list, list) |
| 271 | ieee80211_key_enable_hw_accel(key); |
| 272 | } |
| 273 | |
| 274 | void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata) |
| 275 | { |
| 276 | struct ieee80211_key *key; |
| 277 | |
| 278 | list_for_each_entry(key, &sdata->key_list, list) |
| 279 | ieee80211_key_disable_hw_accel(key); |
| 280 | } |