[MAC80211]: remove HW_KEY_IDX_INVALID
[sfrench/cifs-2.6.git] / net / mac80211 / key.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
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
12 #include <linux/if_ether.h>
13 #include <linux/etherdevice.h>
14 #include <linux/list.h>
15 #include <linux/rcupdate.h>
16 #include <net/mac80211.h>
17 #include "ieee80211_i.h"
18 #include "debugfs_key.h"
19 #include "aes_ccm.h"
20
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;
66
67         if (!key->local->ops->set_key)
68                 return;
69
70         addr = get_mac_for_key(key);
71
72         ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
73                                        key->sdata->dev->dev_addr, addr,
74                                        &key->conf);
75
76         if (!ret)
77                 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
78
79         if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
80                 printk(KERN_ERR "mac80211-%s: failed to set key "
81                        "(%d, " MAC_FMT ") to hardware (%d)\n",
82                        wiphy_name(key->local->hw.wiphy),
83                        key->conf.keyidx, MAC_ARG(addr), ret);
84 }
85
86 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
87 {
88         const u8 *addr;
89         int ret;
90
91         if (!key->local->ops->set_key)
92                 return;
93
94         if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
95                 return;
96
97         addr = get_mac_for_key(key);
98
99         ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
100                                        key->sdata->dev->dev_addr, addr,
101                                        &key->conf);
102
103         if (ret)
104                 printk(KERN_ERR "mac80211-%s: failed to remove key "
105                        "(%d, " MAC_FMT ") from hardware (%d)\n",
106                        wiphy_name(key->local->hw.wiphy),
107                        key->conf.keyidx, MAC_ARG(addr), ret);
108
109         key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
110 }
111
112 struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
113                                           struct sta_info *sta,
114                                           ieee80211_key_alg alg,
115                                           int idx,
116                                           size_t key_len,
117                                           const u8 *key_data)
118 {
119         struct ieee80211_key *key;
120
121         BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
122         BUG_ON(alg == ALG_NONE);
123
124         key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
125         if (!key)
126                 return NULL;
127
128         /*
129          * Default to software encryption; we'll later upload the
130          * key to the hardware if possible.
131          */
132         key->conf.flags = 0;
133         key->flags = 0;
134
135         key->conf.alg = alg;
136         key->conf.keyidx = idx;
137         key->conf.keylen = key_len;
138         memcpy(key->conf.key, key_data, key_len);
139
140         key->local = sdata->local;
141         key->sdata = sdata;
142         key->sta = sta;
143
144         if (alg == ALG_CCMP) {
145                 /*
146                  * Initialize AES key state here as an optimization so that
147                  * it does not need to be initialized for every packet.
148                  */
149                 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
150                 if (!key->u.ccmp.tfm) {
151                         ieee80211_key_free(key);
152                         return NULL;
153                 }
154         }
155
156         ieee80211_debugfs_key_add(key->local, key);
157
158         /* remove key first */
159         if (sta)
160                 ieee80211_key_free(sta->key);
161         else
162                 ieee80211_key_free(sdata->keys[idx]);
163
164         if (sta) {
165                 ieee80211_debugfs_key_sta_link(key, sta);
166
167                 /*
168                  * some hardware cannot handle TKIP with QoS, so
169                  * we indicate whether QoS could be in use.
170                  */
171                 if (sta->flags & WLAN_STA_WME)
172                         key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
173         } else {
174                 if (sdata->type == IEEE80211_IF_TYPE_STA) {
175                         struct sta_info *ap;
176
177                         /* same here, the AP could be using QoS */
178                         ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
179                         if (ap) {
180                                 if (ap->flags & WLAN_STA_WME)
181                                         key->conf.flags |=
182                                                 IEEE80211_KEY_FLAG_WMM_STA;
183                                 sta_info_put(ap);
184                         }
185                 }
186         }
187
188         /* enable hwaccel if appropriate */
189         if (netif_running(key->sdata->dev))
190                 ieee80211_key_enable_hw_accel(key);
191
192         if (sta)
193                 rcu_assign_pointer(sta->key, key);
194         else
195                 rcu_assign_pointer(sdata->keys[idx], key);
196
197         list_add(&key->list, &sdata->key_list);
198
199         return key;
200 }
201
202 void ieee80211_key_free(struct ieee80211_key *key)
203 {
204         if (!key)
205                 return;
206
207         if (key->sta) {
208                 rcu_assign_pointer(key->sta->key, NULL);
209         } else {
210                 if (key->sdata->default_key == key)
211                         ieee80211_set_default_key(key->sdata, -1);
212                 if (key->conf.keyidx >= 0 &&
213                     key->conf.keyidx < NUM_DEFAULT_KEYS)
214                         rcu_assign_pointer(key->sdata->keys[key->conf.keyidx],
215                                            NULL);
216                 else
217                         WARN_ON(1);
218         }
219
220         /* wait for all key users to complete */
221         synchronize_rcu();
222
223         /* remove from hwaccel if appropriate */
224         ieee80211_key_disable_hw_accel(key);
225
226         if (key->conf.alg == ALG_CCMP)
227                 ieee80211_aes_key_free(key->u.ccmp.tfm);
228         ieee80211_debugfs_key_remove(key);
229
230         list_del(&key->list);
231
232         kfree(key);
233 }
234
235 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
236 {
237         struct ieee80211_key *key = NULL;
238
239         if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
240                 key = sdata->keys[idx];
241
242         if (sdata->default_key != key) {
243                 ieee80211_debugfs_key_remove_default(sdata);
244
245                 rcu_assign_pointer(sdata->default_key, key);
246
247                 if (sdata->default_key)
248                         ieee80211_debugfs_key_add_default(sdata);
249         }
250 }
251
252 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
253 {
254         struct ieee80211_key *key, *tmp;
255
256         list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
257                 ieee80211_key_free(key);
258 }
259
260 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
261 {
262         struct ieee80211_key *key;
263
264         WARN_ON(!netif_running(sdata->dev));
265         if (!netif_running(sdata->dev))
266                 return;
267
268         list_for_each_entry(key, &sdata->key_list, list)
269                 ieee80211_key_enable_hw_accel(key);
270 }
271
272 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
273 {
274         struct ieee80211_key *key;
275
276         list_for_each_entry(key, &sdata->key_list, list)
277                 ieee80211_key_disable_hw_accel(key);
278 }