ieee80211_crypto.c revision 1.1.1.5 1 1.1 dyoung /*-
2 1.1 dyoung * Copyright (c) 2001 Atsushi Onoe
3 1.1.1.3 dyoung * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 1.1 dyoung * All rights reserved.
5 1.1 dyoung *
6 1.1 dyoung * Redistribution and use in source and binary forms, with or without
7 1.1 dyoung * modification, are permitted provided that the following conditions
8 1.1 dyoung * are met:
9 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
10 1.1 dyoung * notice, this list of conditions and the following disclaimer.
11 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
13 1.1 dyoung * documentation and/or other materials provided with the distribution.
14 1.1 dyoung * 3. The name of the author may not be used to endorse or promote products
15 1.1 dyoung * derived from this software without specific prior written permission.
16 1.1 dyoung *
17 1.1 dyoung * Alternatively, this software may be distributed under the terms of the
18 1.1 dyoung * GNU General Public License ("GPL") version 2 as published by the Free
19 1.1 dyoung * Software Foundation.
20 1.1 dyoung *
21 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 dyoung * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 dyoung * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 dyoung * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 dyoung * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 dyoung * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 dyoung * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 dyoung * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 dyoung * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 dyoung */
32 1.1 dyoung
33 1.1 dyoung #include <sys/cdefs.h>
34 1.1.1.5 skrll __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.12 2005/08/08 18:46:35 sam Exp $");
35 1.1 dyoung
36 1.1.1.3 dyoung /*
37 1.1.1.3 dyoung * IEEE 802.11 generic crypto support.
38 1.1.1.3 dyoung */
39 1.1 dyoung #include <sys/param.h>
40 1.1 dyoung #include <sys/mbuf.h>
41 1.1.1.3 dyoung
42 1.1 dyoung #include <sys/socket.h>
43 1.1 dyoung
44 1.1 dyoung #include <net/if.h>
45 1.1 dyoung #include <net/if_media.h>
46 1.1.1.3 dyoung #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */
47 1.1 dyoung
48 1.1 dyoung #include <net80211/ieee80211_var.h>
49 1.1 dyoung
50 1.1.1.3 dyoung /*
51 1.1.1.3 dyoung * Table of registered cipher modules.
52 1.1.1.3 dyoung */
53 1.1.1.3 dyoung static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
54 1.1.1.3 dyoung
55 1.1.1.3 dyoung static int _ieee80211_crypto_delkey(struct ieee80211com *,
56 1.1.1.3 dyoung struct ieee80211_key *);
57 1.1.1.3 dyoung
58 1.1.1.3 dyoung /*
59 1.1.1.3 dyoung * Default "null" key management routines.
60 1.1.1.3 dyoung */
61 1.1.1.3 dyoung static int
62 1.1.1.5 skrll null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
63 1.1.1.5 skrll ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
64 1.1.1.3 dyoung {
65 1.1.1.4 dyoung if (!(&ic->ic_nw_keys[0] <= k &&
66 1.1.1.4 dyoung k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
67 1.1.1.4 dyoung /*
68 1.1.1.4 dyoung * Not in the global key table, the driver should handle this
69 1.1.1.4 dyoung * by allocating a slot in the h/w key table/cache. In
70 1.1.1.4 dyoung * lieu of that return key slot 0 for any unicast key
71 1.1.1.4 dyoung * request. We disallow the request if this is a group key.
72 1.1.1.4 dyoung * This default policy does the right thing for legacy hardware
73 1.1.1.4 dyoung * with a 4 key table. It also handles devices that pass
74 1.1.1.4 dyoung * packets through untouched when marked with the WEP bit
75 1.1.1.4 dyoung * and key index 0.
76 1.1.1.4 dyoung */
77 1.1.1.5 skrll if (k->wk_flags & IEEE80211_KEY_GROUP)
78 1.1.1.5 skrll return 0;
79 1.1.1.5 skrll *keyix = 0; /* NB: use key index 0 for ucast key */
80 1.1.1.5 skrll } else {
81 1.1.1.5 skrll *keyix = k - ic->ic_nw_keys;
82 1.1.1.4 dyoung }
83 1.1.1.5 skrll *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */
84 1.1.1.5 skrll return 1;
85 1.1.1.3 dyoung }
86 1.1.1.3 dyoung static int
87 1.1.1.3 dyoung null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
88 1.1.1.3 dyoung {
89 1.1.1.3 dyoung return 1;
90 1.1.1.3 dyoung }
91 1.1.1.3 dyoung static int
92 1.1.1.3 dyoung null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
93 1.1.1.3 dyoung const u_int8_t mac[IEEE80211_ADDR_LEN])
94 1.1.1.3 dyoung {
95 1.1.1.3 dyoung return 1;
96 1.1.1.3 dyoung }
97 1.1.1.3 dyoung static void null_key_update(struct ieee80211com *ic) {}
98 1.1.1.3 dyoung
99 1.1.1.3 dyoung /*
100 1.1.1.3 dyoung * Write-arounds for common operations.
101 1.1.1.3 dyoung */
102 1.1.1.3 dyoung static __inline void
103 1.1.1.3 dyoung cipher_detach(struct ieee80211_key *key)
104 1.1.1.3 dyoung {
105 1.1.1.3 dyoung key->wk_cipher->ic_detach(key);
106 1.1.1.3 dyoung }
107 1.1 dyoung
108 1.1.1.3 dyoung static __inline void *
109 1.1.1.3 dyoung cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key)
110 1.1.1.3 dyoung {
111 1.1.1.3 dyoung return key->wk_cipher->ic_attach(ic, key);
112 1.1.1.3 dyoung }
113 1.1 dyoung
114 1.1.1.3 dyoung /*
115 1.1.1.3 dyoung * Wrappers for driver key management methods.
116 1.1.1.3 dyoung */
117 1.1.1.3 dyoung static __inline int
118 1.1.1.3 dyoung dev_key_alloc(struct ieee80211com *ic,
119 1.1.1.5 skrll const struct ieee80211_key *key,
120 1.1.1.5 skrll ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
121 1.1.1.3 dyoung {
122 1.1.1.5 skrll return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix);
123 1.1.1.3 dyoung }
124 1.1 dyoung
125 1.1.1.3 dyoung static __inline int
126 1.1.1.3 dyoung dev_key_delete(struct ieee80211com *ic,
127 1.1.1.3 dyoung const struct ieee80211_key *key)
128 1.1.1.3 dyoung {
129 1.1.1.3 dyoung return ic->ic_crypto.cs_key_delete(ic, key);
130 1.1.1.3 dyoung }
131 1.1.1.3 dyoung
132 1.1.1.3 dyoung static __inline int
133 1.1.1.3 dyoung dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
134 1.1.1.3 dyoung const u_int8_t mac[IEEE80211_ADDR_LEN])
135 1.1.1.3 dyoung {
136 1.1.1.3 dyoung return ic->ic_crypto.cs_key_set(ic, key, mac);
137 1.1.1.3 dyoung }
138 1.1.1.3 dyoung
139 1.1.1.3 dyoung /*
140 1.1.1.3 dyoung * Setup crypto support.
141 1.1.1.3 dyoung */
142 1.1 dyoung void
143 1.1.1.3 dyoung ieee80211_crypto_attach(struct ieee80211com *ic)
144 1.1 dyoung {
145 1.1.1.3 dyoung struct ieee80211_crypto_state *cs = &ic->ic_crypto;
146 1.1.1.3 dyoung int i;
147 1.1 dyoung
148 1.1.1.3 dyoung /* NB: we assume everything is pre-zero'd */
149 1.1.1.3 dyoung cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
150 1.1.1.5 skrll cs->cs_max_keyix = IEEE80211_WEP_NKID;
151 1.1.1.3 dyoung ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
152 1.1.1.3 dyoung for (i = 0; i < IEEE80211_WEP_NKID; i++)
153 1.1.1.3 dyoung ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
154 1.1.1.3 dyoung IEEE80211_KEYIX_NONE);
155 1.1 dyoung /*
156 1.1.1.3 dyoung * Initialize the driver key support routines to noop entries.
157 1.1.1.3 dyoung * This is useful especially for the cipher test modules.
158 1.1 dyoung */
159 1.1.1.3 dyoung cs->cs_key_alloc = null_key_alloc;
160 1.1.1.3 dyoung cs->cs_key_set = null_key_set;
161 1.1.1.3 dyoung cs->cs_key_delete = null_key_delete;
162 1.1.1.3 dyoung cs->cs_key_update_begin = null_key_update;
163 1.1.1.3 dyoung cs->cs_key_update_end = null_key_update;
164 1.1 dyoung }
165 1.1 dyoung
166 1.1.1.3 dyoung /*
167 1.1.1.3 dyoung * Teardown crypto support.
168 1.1.1.3 dyoung */
169 1.1 dyoung void
170 1.1.1.3 dyoung ieee80211_crypto_detach(struct ieee80211com *ic)
171 1.1 dyoung {
172 1.1.1.3 dyoung ieee80211_crypto_delglobalkeys(ic);
173 1.1.1.3 dyoung }
174 1.1 dyoung
175 1.1.1.3 dyoung /*
176 1.1.1.3 dyoung * Register a crypto cipher module.
177 1.1.1.3 dyoung */
178 1.1.1.3 dyoung void
179 1.1.1.3 dyoung ieee80211_crypto_register(const struct ieee80211_cipher *cip)
180 1.1.1.3 dyoung {
181 1.1.1.3 dyoung if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
182 1.1.1.3 dyoung printf("%s: cipher %s has an invalid cipher index %u\n",
183 1.1.1.3 dyoung __func__, cip->ic_name, cip->ic_cipher);
184 1.1.1.3 dyoung return;
185 1.1 dyoung }
186 1.1.1.3 dyoung if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
187 1.1.1.3 dyoung printf("%s: cipher %s registered with a different template\n",
188 1.1.1.3 dyoung __func__, cip->ic_name);
189 1.1.1.3 dyoung return;
190 1.1.1.3 dyoung }
191 1.1.1.3 dyoung ciphers[cip->ic_cipher] = cip;
192 1.1 dyoung }
193 1.1 dyoung
194 1.1.1.3 dyoung /*
195 1.1.1.3 dyoung * Unregister a crypto cipher module.
196 1.1.1.3 dyoung */
197 1.1.1.3 dyoung void
198 1.1.1.3 dyoung ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
199 1.1 dyoung {
200 1.1.1.3 dyoung if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
201 1.1.1.3 dyoung printf("%s: cipher %s has an invalid cipher index %u\n",
202 1.1.1.3 dyoung __func__, cip->ic_name, cip->ic_cipher);
203 1.1.1.3 dyoung return;
204 1.1 dyoung }
205 1.1.1.3 dyoung if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
206 1.1.1.3 dyoung printf("%s: cipher %s registered with a different template\n",
207 1.1.1.3 dyoung __func__, cip->ic_name);
208 1.1.1.3 dyoung return;
209 1.1.1.3 dyoung }
210 1.1.1.3 dyoung /* NB: don't complain about not being registered */
211 1.1.1.3 dyoung /* XXX disallow if references */
212 1.1.1.3 dyoung ciphers[cip->ic_cipher] = NULL;
213 1.1.1.3 dyoung }
214 1.1.1.3 dyoung
215 1.1.1.3 dyoung int
216 1.1.1.3 dyoung ieee80211_crypto_available(u_int cipher)
217 1.1.1.3 dyoung {
218 1.1.1.3 dyoung return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
219 1.1.1.3 dyoung }
220 1.1.1.3 dyoung
221 1.1.1.3 dyoung /* XXX well-known names! */
222 1.1.1.3 dyoung static const char *cipher_modnames[] = {
223 1.1.1.3 dyoung "wlan_wep", /* IEEE80211_CIPHER_WEP */
224 1.1.1.3 dyoung "wlan_tkip", /* IEEE80211_CIPHER_TKIP */
225 1.1.1.3 dyoung "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */
226 1.1.1.3 dyoung "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */
227 1.1.1.3 dyoung "wlan_ckip", /* IEEE80211_CIPHER_CKIP */
228 1.1.1.3 dyoung };
229 1.1.1.3 dyoung
230 1.1.1.3 dyoung /*
231 1.1.1.3 dyoung * Establish a relationship between the specified key and cipher
232 1.1.1.3 dyoung * and, if necessary, allocate a hardware index from the driver.
233 1.1.1.3 dyoung * Note that when a fixed key index is required it must be specified
234 1.1.1.3 dyoung * and we blindly assign it w/o consulting the driver (XXX).
235 1.1.1.3 dyoung *
236 1.1.1.3 dyoung * This must be the first call applied to a key; all the other key
237 1.1.1.3 dyoung * routines assume wk_cipher is setup.
238 1.1.1.3 dyoung *
239 1.1.1.3 dyoung * Locking must be handled by the caller using:
240 1.1.1.3 dyoung * ieee80211_key_update_begin(ic);
241 1.1.1.3 dyoung * ieee80211_key_update_end(ic);
242 1.1.1.3 dyoung */
243 1.1.1.3 dyoung int
244 1.1.1.3 dyoung ieee80211_crypto_newkey(struct ieee80211com *ic,
245 1.1.1.3 dyoung int cipher, int flags, struct ieee80211_key *key)
246 1.1.1.3 dyoung {
247 1.1.1.3 dyoung #define N(a) (sizeof(a) / sizeof(a[0]))
248 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
249 1.1.1.5 skrll ieee80211_keyix keyix, rxkeyix;
250 1.1.1.3 dyoung void *keyctx;
251 1.1.1.3 dyoung int oflags;
252 1.1.1.3 dyoung
253 1.1.1.3 dyoung /*
254 1.1.1.3 dyoung * Validate cipher and set reference to cipher routines.
255 1.1.1.3 dyoung */
256 1.1.1.3 dyoung if (cipher >= IEEE80211_CIPHER_MAX) {
257 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
258 1.1.1.3 dyoung "%s: invalid cipher %u\n", __func__, cipher);
259 1.1.1.3 dyoung ic->ic_stats.is_crypto_badcipher++;
260 1.1.1.3 dyoung return 0;
261 1.1 dyoung }
262 1.1.1.3 dyoung cip = ciphers[cipher];
263 1.1.1.3 dyoung if (cip == NULL) {
264 1.1 dyoung /*
265 1.1.1.3 dyoung * Auto-load cipher module if we have a well-known name
266 1.1.1.3 dyoung * for it. It might be better to use string names rather
267 1.1.1.3 dyoung * than numbers and craft a module name based on the cipher
268 1.1.1.3 dyoung * name; e.g. wlan_cipher_<cipher-name>.
269 1.1 dyoung */
270 1.1.1.3 dyoung if (cipher < N(cipher_modnames)) {
271 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
272 1.1.1.3 dyoung "%s: unregistered cipher %u, load module %s\n",
273 1.1.1.3 dyoung __func__, cipher, cipher_modnames[cipher]);
274 1.1.1.3 dyoung ieee80211_load_module(cipher_modnames[cipher]);
275 1.1.1.3 dyoung /*
276 1.1.1.3 dyoung * If cipher module loaded it should immediately
277 1.1.1.3 dyoung * call ieee80211_crypto_register which will fill
278 1.1.1.3 dyoung * in the entry in the ciphers array.
279 1.1.1.3 dyoung */
280 1.1.1.3 dyoung cip = ciphers[cipher];
281 1.1 dyoung }
282 1.1.1.3 dyoung if (cip == NULL) {
283 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
284 1.1.1.3 dyoung "%s: unable to load cipher %u, module %s\n",
285 1.1.1.3 dyoung __func__, cipher,
286 1.1.1.3 dyoung cipher < N(cipher_modnames) ?
287 1.1.1.3 dyoung cipher_modnames[cipher] : "<unknown>");
288 1.1.1.3 dyoung ic->ic_stats.is_crypto_nocipher++;
289 1.1.1.3 dyoung return 0;
290 1.1 dyoung }
291 1.1.1.3 dyoung }
292 1.1.1.3 dyoung
293 1.1.1.3 dyoung oflags = key->wk_flags;
294 1.1.1.3 dyoung flags &= IEEE80211_KEY_COMMON;
295 1.1.1.3 dyoung /*
296 1.1.1.3 dyoung * If the hardware does not support the cipher then
297 1.1.1.3 dyoung * fallback to a host-based implementation.
298 1.1.1.3 dyoung */
299 1.1.1.3 dyoung if ((ic->ic_caps & (1<<cipher)) == 0) {
300 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
301 1.1.1.3 dyoung "%s: no h/w support for cipher %s, falling back to s/w\n",
302 1.1.1.3 dyoung __func__, cip->ic_name);
303 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWCRYPT;
304 1.1.1.3 dyoung }
305 1.1.1.3 dyoung /*
306 1.1.1.3 dyoung * Hardware TKIP with software MIC is an important
307 1.1.1.3 dyoung * combination; we handle it by flagging each key,
308 1.1.1.3 dyoung * the cipher modules honor it.
309 1.1.1.3 dyoung */
310 1.1.1.3 dyoung if (cipher == IEEE80211_CIPHER_TKIP &&
311 1.1.1.3 dyoung (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
312 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
313 1.1.1.3 dyoung "%s: no h/w support for TKIP MIC, falling back to s/w\n",
314 1.1.1.3 dyoung __func__);
315 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWMIC;
316 1.1.1.3 dyoung }
317 1.1.1.3 dyoung
318 1.1.1.3 dyoung /*
319 1.1.1.3 dyoung * Bind cipher to key instance. Note we do this
320 1.1.1.3 dyoung * after checking the device capabilities so the
321 1.1.1.3 dyoung * cipher module can optimize space usage based on
322 1.1.1.3 dyoung * whether or not it needs to do the cipher work.
323 1.1.1.3 dyoung */
324 1.1.1.3 dyoung if (key->wk_cipher != cip || key->wk_flags != flags) {
325 1.1.1.3 dyoung again:
326 1.1.1.3 dyoung /*
327 1.1.1.3 dyoung * Fillin the flags so cipher modules can see s/w
328 1.1.1.3 dyoung * crypto requirements and potentially allocate
329 1.1.1.3 dyoung * different state and/or attach different method
330 1.1.1.3 dyoung * pointers.
331 1.1.1.3 dyoung *
332 1.1.1.3 dyoung * XXX this is not right when s/w crypto fallback
333 1.1.1.3 dyoung * fails and we try to restore previous state.
334 1.1.1.3 dyoung */
335 1.1.1.3 dyoung key->wk_flags = flags;
336 1.1.1.3 dyoung keyctx = cip->ic_attach(ic, key);
337 1.1.1.3 dyoung if (keyctx == NULL) {
338 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
339 1.1.1.3 dyoung "%s: unable to attach cipher %s\n",
340 1.1.1.3 dyoung __func__, cip->ic_name);
341 1.1.1.3 dyoung key->wk_flags = oflags; /* restore old flags */
342 1.1.1.3 dyoung ic->ic_stats.is_crypto_attachfail++;
343 1.1.1.3 dyoung return 0;
344 1.1 dyoung }
345 1.1.1.3 dyoung cipher_detach(key);
346 1.1.1.3 dyoung key->wk_cipher = cip; /* XXX refcnt? */
347 1.1.1.3 dyoung key->wk_private = keyctx;
348 1.1.1.3 dyoung }
349 1.1.1.3 dyoung /*
350 1.1.1.3 dyoung * Commit to requested usage so driver can see the flags.
351 1.1.1.3 dyoung */
352 1.1.1.3 dyoung key->wk_flags = flags;
353 1.1.1.3 dyoung
354 1.1.1.3 dyoung /*
355 1.1.1.3 dyoung * Ask the driver for a key index if we don't have one.
356 1.1.1.3 dyoung * Note that entries in the global key table always have
357 1.1.1.3 dyoung * an index; this means it's safe to call this routine
358 1.1.1.3 dyoung * for these entries just to setup the reference to the
359 1.1.1.3 dyoung * cipher template. Note also that when using software
360 1.1.1.3 dyoung * crypto we also call the driver to give us a key index.
361 1.1.1.3 dyoung */
362 1.1.1.3 dyoung if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
363 1.1.1.5 skrll if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) {
364 1.1.1.3 dyoung /*
365 1.1.1.3 dyoung * Driver has no room; fallback to doing crypto
366 1.1.1.3 dyoung * in the host. We change the flags and start the
367 1.1.1.3 dyoung * procedure over. If we get back here then there's
368 1.1.1.3 dyoung * no hope and we bail. Note that this can leave
369 1.1.1.3 dyoung * the key in a inconsistent state if the caller
370 1.1.1.3 dyoung * continues to use it.
371 1.1.1.3 dyoung */
372 1.1.1.3 dyoung if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
373 1.1.1.3 dyoung ic->ic_stats.is_crypto_swfallback++;
374 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
375 1.1.1.3 dyoung "%s: no h/w resources for cipher %s, "
376 1.1.1.3 dyoung "falling back to s/w\n", __func__,
377 1.1.1.3 dyoung cip->ic_name);
378 1.1.1.3 dyoung oflags = key->wk_flags;
379 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWCRYPT;
380 1.1.1.3 dyoung if (cipher == IEEE80211_CIPHER_TKIP)
381 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWMIC;
382 1.1.1.3 dyoung goto again;
383 1.1.1.2 dyoung }
384 1.1.1.3 dyoung ic->ic_stats.is_crypto_keyfail++;
385 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
386 1.1.1.3 dyoung "%s: unable to setup cipher %s\n",
387 1.1.1.3 dyoung __func__, cip->ic_name);
388 1.1.1.3 dyoung return 0;
389 1.1 dyoung }
390 1.1.1.5 skrll key->wk_keyix = keyix;
391 1.1.1.5 skrll key->wk_rxkeyix = rxkeyix;
392 1.1.1.3 dyoung }
393 1.1.1.3 dyoung return 1;
394 1.1.1.3 dyoung #undef N
395 1.1.1.3 dyoung }
396 1.1.1.3 dyoung
397 1.1.1.3 dyoung /*
398 1.1.1.3 dyoung * Remove the key (no locking, for internal use).
399 1.1.1.3 dyoung */
400 1.1.1.3 dyoung static int
401 1.1.1.3 dyoung _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
402 1.1.1.3 dyoung {
403 1.1.1.5 skrll ieee80211_keyix keyix;
404 1.1.1.3 dyoung
405 1.1.1.3 dyoung KASSERT(key->wk_cipher != NULL, ("No cipher!"));
406 1.1.1.3 dyoung
407 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
408 1.1.1.3 dyoung "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
409 1.1.1.3 dyoung __func__, key->wk_cipher->ic_name,
410 1.1.1.3 dyoung key->wk_keyix, key->wk_flags,
411 1.1.1.3 dyoung key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
412 1.1.1.3 dyoung
413 1.1.1.3 dyoung keyix = key->wk_keyix;
414 1.1.1.3 dyoung if (keyix != IEEE80211_KEYIX_NONE) {
415 1.1.1.3 dyoung /*
416 1.1.1.3 dyoung * Remove hardware entry.
417 1.1.1.3 dyoung */
418 1.1.1.3 dyoung /* XXX key cache */
419 1.1.1.3 dyoung if (!dev_key_delete(ic, key)) {
420 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
421 1.1.1.3 dyoung "%s: driver did not delete key index %u\n",
422 1.1.1.3 dyoung __func__, keyix);
423 1.1.1.3 dyoung ic->ic_stats.is_crypto_delkey++;
424 1.1.1.3 dyoung /* XXX recovery? */
425 1.1 dyoung }
426 1.1 dyoung }
427 1.1.1.3 dyoung cipher_detach(key);
428 1.1.1.3 dyoung memset(key, 0, sizeof(*key));
429 1.1.1.3 dyoung ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
430 1.1.1.3 dyoung return 1;
431 1.1.1.3 dyoung }
432 1.1 dyoung
433 1.1.1.3 dyoung /*
434 1.1.1.3 dyoung * Remove the specified key.
435 1.1.1.3 dyoung */
436 1.1.1.3 dyoung int
437 1.1.1.3 dyoung ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
438 1.1.1.3 dyoung {
439 1.1.1.3 dyoung int status;
440 1.1.1.3 dyoung
441 1.1.1.3 dyoung ieee80211_key_update_begin(ic);
442 1.1.1.3 dyoung status = _ieee80211_crypto_delkey(ic, key);
443 1.1.1.3 dyoung ieee80211_key_update_end(ic);
444 1.1.1.3 dyoung return status;
445 1.1 dyoung }
446 1.1 dyoung
447 1.1 dyoung /*
448 1.1.1.3 dyoung * Clear the global key table.
449 1.1 dyoung */
450 1.1.1.3 dyoung void
451 1.1.1.3 dyoung ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
452 1.1.1.3 dyoung {
453 1.1.1.3 dyoung int i;
454 1.1 dyoung
455 1.1.1.3 dyoung ieee80211_key_update_begin(ic);
456 1.1.1.3 dyoung for (i = 0; i < IEEE80211_WEP_NKID; i++)
457 1.1.1.3 dyoung (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
458 1.1.1.3 dyoung ieee80211_key_update_end(ic);
459 1.1.1.3 dyoung }
460 1.1 dyoung
461 1.1.1.3 dyoung /*
462 1.1.1.3 dyoung * Set the contents of the specified key.
463 1.1.1.3 dyoung *
464 1.1.1.3 dyoung * Locking must be handled by the caller using:
465 1.1.1.3 dyoung * ieee80211_key_update_begin(ic);
466 1.1.1.3 dyoung * ieee80211_key_update_end(ic);
467 1.1.1.3 dyoung */
468 1.1.1.3 dyoung int
469 1.1.1.3 dyoung ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
470 1.1.1.3 dyoung const u_int8_t macaddr[IEEE80211_ADDR_LEN])
471 1.1 dyoung {
472 1.1.1.3 dyoung const struct ieee80211_cipher *cip = key->wk_cipher;
473 1.1 dyoung
474 1.1.1.3 dyoung KASSERT(cip != NULL, ("No cipher!"));
475 1.1.1.3 dyoung
476 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
477 1.1.1.3 dyoung "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
478 1.1.1.3 dyoung __func__, cip->ic_name, key->wk_keyix,
479 1.1.1.3 dyoung key->wk_flags, ether_sprintf(macaddr),
480 1.1.1.3 dyoung key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
481 1.1.1.3 dyoung
482 1.1.1.3 dyoung /*
483 1.1.1.3 dyoung * Give cipher a chance to validate key contents.
484 1.1.1.3 dyoung * XXX should happen before modifying state.
485 1.1.1.3 dyoung */
486 1.1.1.3 dyoung if (!cip->ic_setkey(key)) {
487 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
488 1.1.1.3 dyoung "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
489 1.1.1.3 dyoung __func__, cip->ic_name, key->wk_keyix,
490 1.1.1.3 dyoung key->wk_keylen, key->wk_flags);
491 1.1.1.3 dyoung ic->ic_stats.is_crypto_setkey_cipher++;
492 1.1.1.3 dyoung return 0;
493 1.1.1.3 dyoung }
494 1.1.1.3 dyoung if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
495 1.1.1.3 dyoung /* XXX nothing allocated, should not happen */
496 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
497 1.1.1.3 dyoung "%s: no key index; should not happen!\n", __func__);
498 1.1.1.3 dyoung ic->ic_stats.is_crypto_setkey_nokey++;
499 1.1.1.3 dyoung return 0;
500 1.1 dyoung }
501 1.1.1.3 dyoung return dev_key_set(ic, key, macaddr);
502 1.1 dyoung }
503 1.1 dyoung
504 1.1 dyoung /*
505 1.1.1.3 dyoung * Add privacy headers appropriate for the specified key.
506 1.1 dyoung */
507 1.1.1.3 dyoung struct ieee80211_key *
508 1.1.1.3 dyoung ieee80211_crypto_encap(struct ieee80211com *ic,
509 1.1.1.3 dyoung struct ieee80211_node *ni, struct mbuf *m)
510 1.1.1.3 dyoung {
511 1.1.1.3 dyoung struct ieee80211_key *k;
512 1.1.1.3 dyoung struct ieee80211_frame *wh;
513 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
514 1.1.1.3 dyoung u_int8_t keyid;
515 1.1 dyoung
516 1.1.1.3 dyoung /*
517 1.1.1.3 dyoung * Multicast traffic always uses the multicast key.
518 1.1.1.3 dyoung * Otherwise if a unicast key is set we use that and
519 1.1.1.3 dyoung * it is always key index 0. When no unicast key is
520 1.1.1.3 dyoung * set we fall back to the default transmit key.
521 1.1.1.3 dyoung */
522 1.1.1.3 dyoung wh = mtod(m, struct ieee80211_frame *);
523 1.1.1.3 dyoung if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
524 1.1.1.3 dyoung ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
525 1.1.1.3 dyoung if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
526 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
527 1.1.1.3 dyoung "[%s] no default transmit key (%s) deftxkey %u\n",
528 1.1.1.3 dyoung ether_sprintf(wh->i_addr1), __func__,
529 1.1.1.3 dyoung ic->ic_def_txkey);
530 1.1.1.3 dyoung ic->ic_stats.is_tx_nodefkey++;
531 1.1.1.3 dyoung return NULL;
532 1.1.1.3 dyoung }
533 1.1.1.3 dyoung keyid = ic->ic_def_txkey;
534 1.1.1.3 dyoung k = &ic->ic_nw_keys[ic->ic_def_txkey];
535 1.1.1.3 dyoung } else {
536 1.1.1.3 dyoung keyid = 0;
537 1.1.1.3 dyoung k = &ni->ni_ucastkey;
538 1.1.1.3 dyoung }
539 1.1.1.3 dyoung cip = k->wk_cipher;
540 1.1.1.3 dyoung return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
541 1.1.1.3 dyoung }
542 1.1.1.3 dyoung
543 1.1.1.3 dyoung /*
544 1.1.1.3 dyoung * Validate and strip privacy headers (and trailer) for a
545 1.1.1.3 dyoung * received frame that has the WEP/Privacy bit set.
546 1.1.1.3 dyoung */
547 1.1.1.3 dyoung struct ieee80211_key *
548 1.1.1.3 dyoung ieee80211_crypto_decap(struct ieee80211com *ic,
549 1.1.1.4 dyoung struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
550 1.1 dyoung {
551 1.1.1.3 dyoung #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
552 1.1.1.3 dyoung #define IEEE80211_WEP_MINLEN \
553 1.1.1.5 skrll (sizeof(struct ieee80211_frame) + \
554 1.1.1.3 dyoung IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
555 1.1.1.3 dyoung struct ieee80211_key *k;
556 1.1.1.3 dyoung struct ieee80211_frame *wh;
557 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
558 1.1.1.3 dyoung const u_int8_t *ivp;
559 1.1.1.3 dyoung u_int8_t keyid;
560 1.1.1.3 dyoung
561 1.1.1.3 dyoung /* NB: this minimum size data frame could be bigger */
562 1.1.1.3 dyoung if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
563 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
564 1.1.1.3 dyoung "%s: WEP data frame too short, len %u\n",
565 1.1.1.3 dyoung __func__, m->m_pkthdr.len);
566 1.1.1.3 dyoung ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */
567 1.1.1.3 dyoung return NULL;
568 1.1.1.3 dyoung }
569 1.1.1.3 dyoung
570 1.1.1.3 dyoung /*
571 1.1.1.3 dyoung * Locate the key. If unicast and there is no unicast
572 1.1.1.3 dyoung * key then we fall back to the key id in the header.
573 1.1.1.3 dyoung * This assumes unicast keys are only configured when
574 1.1.1.3 dyoung * the key id in the header is meaningless (typically 0).
575 1.1.1.3 dyoung */
576 1.1.1.3 dyoung wh = mtod(m, struct ieee80211_frame *);
577 1.1.1.3 dyoung ivp = mtod(m, const u_int8_t *) + hdrlen; /* XXX contig */
578 1.1.1.3 dyoung keyid = ivp[IEEE80211_WEP_IVLEN];
579 1.1.1.3 dyoung if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
580 1.1.1.3 dyoung ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)
581 1.1.1.3 dyoung k = &ic->ic_nw_keys[keyid >> 6];
582 1.1.1.3 dyoung else
583 1.1.1.3 dyoung k = &ni->ni_ucastkey;
584 1.1.1.3 dyoung
585 1.1.1.3 dyoung /*
586 1.1.1.3 dyoung * Insure crypto header is contiguous for all decap work.
587 1.1.1.3 dyoung */
588 1.1.1.3 dyoung cip = k->wk_cipher;
589 1.1.1.3 dyoung if (m->m_len < hdrlen + cip->ic_header &&
590 1.1.1.3 dyoung (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
591 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
592 1.1.1.3 dyoung "[%s] unable to pullup %s header\n",
593 1.1.1.3 dyoung ether_sprintf(wh->i_addr2), cip->ic_name);
594 1.1.1.3 dyoung ic->ic_stats.is_rx_wepfail++; /* XXX */
595 1.1.1.3 dyoung return 0;
596 1.1.1.3 dyoung }
597 1.1 dyoung
598 1.1.1.4 dyoung return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
599 1.1.1.3 dyoung #undef IEEE80211_WEP_MINLEN
600 1.1.1.3 dyoung #undef IEEE80211_WEP_HDRLEN
601 1.1 dyoung }
602