ieee80211_crypto.c revision 1.1.1.3 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.3 dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.8 2005/04/12 17:55:13 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.3 dyoung null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
63 1.1.1.3 dyoung {
64 1.1.1.3 dyoung return IEEE80211_KEYIX_NONE;
65 1.1.1.3 dyoung }
66 1.1.1.3 dyoung static int
67 1.1.1.3 dyoung null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
68 1.1.1.3 dyoung {
69 1.1.1.3 dyoung return 1;
70 1.1.1.3 dyoung }
71 1.1.1.3 dyoung static int
72 1.1.1.3 dyoung null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
73 1.1.1.3 dyoung const u_int8_t mac[IEEE80211_ADDR_LEN])
74 1.1.1.3 dyoung {
75 1.1.1.3 dyoung return 1;
76 1.1.1.3 dyoung }
77 1.1.1.3 dyoung static void null_key_update(struct ieee80211com *ic) {}
78 1.1.1.3 dyoung
79 1.1.1.3 dyoung /*
80 1.1.1.3 dyoung * Write-arounds for common operations.
81 1.1.1.3 dyoung */
82 1.1.1.3 dyoung static __inline void
83 1.1.1.3 dyoung cipher_detach(struct ieee80211_key *key)
84 1.1.1.3 dyoung {
85 1.1.1.3 dyoung key->wk_cipher->ic_detach(key);
86 1.1.1.3 dyoung }
87 1.1 dyoung
88 1.1.1.3 dyoung static __inline void *
89 1.1.1.3 dyoung cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key)
90 1.1.1.3 dyoung {
91 1.1.1.3 dyoung return key->wk_cipher->ic_attach(ic, key);
92 1.1.1.3 dyoung }
93 1.1 dyoung
94 1.1.1.3 dyoung /*
95 1.1.1.3 dyoung * Wrappers for driver key management methods.
96 1.1.1.3 dyoung */
97 1.1.1.3 dyoung static __inline int
98 1.1.1.3 dyoung dev_key_alloc(struct ieee80211com *ic,
99 1.1.1.3 dyoung const struct ieee80211_key *key)
100 1.1.1.3 dyoung {
101 1.1.1.3 dyoung return ic->ic_crypto.cs_key_alloc(ic, key);
102 1.1.1.3 dyoung }
103 1.1 dyoung
104 1.1.1.3 dyoung static __inline int
105 1.1.1.3 dyoung dev_key_delete(struct ieee80211com *ic,
106 1.1.1.3 dyoung const struct ieee80211_key *key)
107 1.1.1.3 dyoung {
108 1.1.1.3 dyoung return ic->ic_crypto.cs_key_delete(ic, key);
109 1.1.1.3 dyoung }
110 1.1.1.3 dyoung
111 1.1.1.3 dyoung static __inline int
112 1.1.1.3 dyoung dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
113 1.1.1.3 dyoung const u_int8_t mac[IEEE80211_ADDR_LEN])
114 1.1.1.3 dyoung {
115 1.1.1.3 dyoung return ic->ic_crypto.cs_key_set(ic, key, mac);
116 1.1.1.3 dyoung }
117 1.1.1.3 dyoung
118 1.1.1.3 dyoung /*
119 1.1.1.3 dyoung * Setup crypto support.
120 1.1.1.3 dyoung */
121 1.1 dyoung void
122 1.1.1.3 dyoung ieee80211_crypto_attach(struct ieee80211com *ic)
123 1.1 dyoung {
124 1.1.1.3 dyoung struct ieee80211_crypto_state *cs = &ic->ic_crypto;
125 1.1.1.3 dyoung int i;
126 1.1 dyoung
127 1.1.1.3 dyoung /* NB: we assume everything is pre-zero'd */
128 1.1.1.3 dyoung cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
129 1.1.1.3 dyoung ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
130 1.1.1.3 dyoung for (i = 0; i < IEEE80211_WEP_NKID; i++)
131 1.1.1.3 dyoung ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
132 1.1.1.3 dyoung IEEE80211_KEYIX_NONE);
133 1.1 dyoung /*
134 1.1.1.3 dyoung * Initialize the driver key support routines to noop entries.
135 1.1.1.3 dyoung * This is useful especially for the cipher test modules.
136 1.1 dyoung */
137 1.1.1.3 dyoung cs->cs_key_alloc = null_key_alloc;
138 1.1.1.3 dyoung cs->cs_key_set = null_key_set;
139 1.1.1.3 dyoung cs->cs_key_delete = null_key_delete;
140 1.1.1.3 dyoung cs->cs_key_update_begin = null_key_update;
141 1.1.1.3 dyoung cs->cs_key_update_end = null_key_update;
142 1.1 dyoung }
143 1.1 dyoung
144 1.1.1.3 dyoung /*
145 1.1.1.3 dyoung * Teardown crypto support.
146 1.1.1.3 dyoung */
147 1.1 dyoung void
148 1.1.1.3 dyoung ieee80211_crypto_detach(struct ieee80211com *ic)
149 1.1 dyoung {
150 1.1.1.3 dyoung ieee80211_crypto_delglobalkeys(ic);
151 1.1.1.3 dyoung }
152 1.1 dyoung
153 1.1.1.3 dyoung /*
154 1.1.1.3 dyoung * Register a crypto cipher module.
155 1.1.1.3 dyoung */
156 1.1.1.3 dyoung void
157 1.1.1.3 dyoung ieee80211_crypto_register(const struct ieee80211_cipher *cip)
158 1.1.1.3 dyoung {
159 1.1.1.3 dyoung if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
160 1.1.1.3 dyoung printf("%s: cipher %s has an invalid cipher index %u\n",
161 1.1.1.3 dyoung __func__, cip->ic_name, cip->ic_cipher);
162 1.1.1.3 dyoung return;
163 1.1 dyoung }
164 1.1.1.3 dyoung if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
165 1.1.1.3 dyoung printf("%s: cipher %s registered with a different template\n",
166 1.1.1.3 dyoung __func__, cip->ic_name);
167 1.1.1.3 dyoung return;
168 1.1.1.3 dyoung }
169 1.1.1.3 dyoung ciphers[cip->ic_cipher] = cip;
170 1.1 dyoung }
171 1.1 dyoung
172 1.1.1.3 dyoung /*
173 1.1.1.3 dyoung * Unregister a crypto cipher module.
174 1.1.1.3 dyoung */
175 1.1.1.3 dyoung void
176 1.1.1.3 dyoung ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
177 1.1 dyoung {
178 1.1.1.3 dyoung if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
179 1.1.1.3 dyoung printf("%s: cipher %s has an invalid cipher index %u\n",
180 1.1.1.3 dyoung __func__, cip->ic_name, cip->ic_cipher);
181 1.1.1.3 dyoung return;
182 1.1 dyoung }
183 1.1.1.3 dyoung if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
184 1.1.1.3 dyoung printf("%s: cipher %s registered with a different template\n",
185 1.1.1.3 dyoung __func__, cip->ic_name);
186 1.1.1.3 dyoung return;
187 1.1.1.3 dyoung }
188 1.1.1.3 dyoung /* NB: don't complain about not being registered */
189 1.1.1.3 dyoung /* XXX disallow if references */
190 1.1.1.3 dyoung ciphers[cip->ic_cipher] = NULL;
191 1.1.1.3 dyoung }
192 1.1.1.3 dyoung
193 1.1.1.3 dyoung int
194 1.1.1.3 dyoung ieee80211_crypto_available(u_int cipher)
195 1.1.1.3 dyoung {
196 1.1.1.3 dyoung return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
197 1.1.1.3 dyoung }
198 1.1.1.3 dyoung
199 1.1.1.3 dyoung /* XXX well-known names! */
200 1.1.1.3 dyoung static const char *cipher_modnames[] = {
201 1.1.1.3 dyoung "wlan_wep", /* IEEE80211_CIPHER_WEP */
202 1.1.1.3 dyoung "wlan_tkip", /* IEEE80211_CIPHER_TKIP */
203 1.1.1.3 dyoung "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */
204 1.1.1.3 dyoung "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */
205 1.1.1.3 dyoung "wlan_ckip", /* IEEE80211_CIPHER_CKIP */
206 1.1.1.3 dyoung };
207 1.1.1.3 dyoung
208 1.1.1.3 dyoung /*
209 1.1.1.3 dyoung * Establish a relationship between the specified key and cipher
210 1.1.1.3 dyoung * and, if necessary, allocate a hardware index from the driver.
211 1.1.1.3 dyoung * Note that when a fixed key index is required it must be specified
212 1.1.1.3 dyoung * and we blindly assign it w/o consulting the driver (XXX).
213 1.1.1.3 dyoung *
214 1.1.1.3 dyoung * This must be the first call applied to a key; all the other key
215 1.1.1.3 dyoung * routines assume wk_cipher is setup.
216 1.1.1.3 dyoung *
217 1.1.1.3 dyoung * Locking must be handled by the caller using:
218 1.1.1.3 dyoung * ieee80211_key_update_begin(ic);
219 1.1.1.3 dyoung * ieee80211_key_update_end(ic);
220 1.1.1.3 dyoung */
221 1.1.1.3 dyoung int
222 1.1.1.3 dyoung ieee80211_crypto_newkey(struct ieee80211com *ic,
223 1.1.1.3 dyoung int cipher, int flags, struct ieee80211_key *key)
224 1.1.1.3 dyoung {
225 1.1.1.3 dyoung #define N(a) (sizeof(a) / sizeof(a[0]))
226 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
227 1.1.1.3 dyoung void *keyctx;
228 1.1.1.3 dyoung int oflags;
229 1.1.1.3 dyoung
230 1.1.1.3 dyoung /*
231 1.1.1.3 dyoung * Validate cipher and set reference to cipher routines.
232 1.1.1.3 dyoung */
233 1.1.1.3 dyoung if (cipher >= IEEE80211_CIPHER_MAX) {
234 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
235 1.1.1.3 dyoung "%s: invalid cipher %u\n", __func__, cipher);
236 1.1.1.3 dyoung ic->ic_stats.is_crypto_badcipher++;
237 1.1.1.3 dyoung return 0;
238 1.1 dyoung }
239 1.1.1.3 dyoung cip = ciphers[cipher];
240 1.1.1.3 dyoung if (cip == NULL) {
241 1.1 dyoung /*
242 1.1.1.3 dyoung * Auto-load cipher module if we have a well-known name
243 1.1.1.3 dyoung * for it. It might be better to use string names rather
244 1.1.1.3 dyoung * than numbers and craft a module name based on the cipher
245 1.1.1.3 dyoung * name; e.g. wlan_cipher_<cipher-name>.
246 1.1 dyoung */
247 1.1.1.3 dyoung if (cipher < N(cipher_modnames)) {
248 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
249 1.1.1.3 dyoung "%s: unregistered cipher %u, load module %s\n",
250 1.1.1.3 dyoung __func__, cipher, cipher_modnames[cipher]);
251 1.1.1.3 dyoung ieee80211_load_module(cipher_modnames[cipher]);
252 1.1.1.3 dyoung /*
253 1.1.1.3 dyoung * If cipher module loaded it should immediately
254 1.1.1.3 dyoung * call ieee80211_crypto_register which will fill
255 1.1.1.3 dyoung * in the entry in the ciphers array.
256 1.1.1.3 dyoung */
257 1.1.1.3 dyoung cip = ciphers[cipher];
258 1.1 dyoung }
259 1.1.1.3 dyoung if (cip == NULL) {
260 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
261 1.1.1.3 dyoung "%s: unable to load cipher %u, module %s\n",
262 1.1.1.3 dyoung __func__, cipher,
263 1.1.1.3 dyoung cipher < N(cipher_modnames) ?
264 1.1.1.3 dyoung cipher_modnames[cipher] : "<unknown>");
265 1.1.1.3 dyoung ic->ic_stats.is_crypto_nocipher++;
266 1.1.1.3 dyoung return 0;
267 1.1 dyoung }
268 1.1.1.3 dyoung }
269 1.1.1.3 dyoung
270 1.1.1.3 dyoung oflags = key->wk_flags;
271 1.1.1.3 dyoung flags &= IEEE80211_KEY_COMMON;
272 1.1.1.3 dyoung /*
273 1.1.1.3 dyoung * If the hardware does not support the cipher then
274 1.1.1.3 dyoung * fallback to a host-based implementation.
275 1.1.1.3 dyoung */
276 1.1.1.3 dyoung if ((ic->ic_caps & (1<<cipher)) == 0) {
277 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
278 1.1.1.3 dyoung "%s: no h/w support for cipher %s, falling back to s/w\n",
279 1.1.1.3 dyoung __func__, cip->ic_name);
280 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWCRYPT;
281 1.1.1.3 dyoung }
282 1.1.1.3 dyoung /*
283 1.1.1.3 dyoung * Hardware TKIP with software MIC is an important
284 1.1.1.3 dyoung * combination; we handle it by flagging each key,
285 1.1.1.3 dyoung * the cipher modules honor it.
286 1.1.1.3 dyoung */
287 1.1.1.3 dyoung if (cipher == IEEE80211_CIPHER_TKIP &&
288 1.1.1.3 dyoung (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
289 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
290 1.1.1.3 dyoung "%s: no h/w support for TKIP MIC, falling back to s/w\n",
291 1.1.1.3 dyoung __func__);
292 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWMIC;
293 1.1.1.3 dyoung }
294 1.1.1.3 dyoung
295 1.1.1.3 dyoung /*
296 1.1.1.3 dyoung * Bind cipher to key instance. Note we do this
297 1.1.1.3 dyoung * after checking the device capabilities so the
298 1.1.1.3 dyoung * cipher module can optimize space usage based on
299 1.1.1.3 dyoung * whether or not it needs to do the cipher work.
300 1.1.1.3 dyoung */
301 1.1.1.3 dyoung if (key->wk_cipher != cip || key->wk_flags != flags) {
302 1.1.1.3 dyoung again:
303 1.1.1.3 dyoung /*
304 1.1.1.3 dyoung * Fillin the flags so cipher modules can see s/w
305 1.1.1.3 dyoung * crypto requirements and potentially allocate
306 1.1.1.3 dyoung * different state and/or attach different method
307 1.1.1.3 dyoung * pointers.
308 1.1.1.3 dyoung *
309 1.1.1.3 dyoung * XXX this is not right when s/w crypto fallback
310 1.1.1.3 dyoung * fails and we try to restore previous state.
311 1.1.1.3 dyoung */
312 1.1.1.3 dyoung key->wk_flags = flags;
313 1.1.1.3 dyoung keyctx = cip->ic_attach(ic, key);
314 1.1.1.3 dyoung if (keyctx == NULL) {
315 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
316 1.1.1.3 dyoung "%s: unable to attach cipher %s\n",
317 1.1.1.3 dyoung __func__, cip->ic_name);
318 1.1.1.3 dyoung key->wk_flags = oflags; /* restore old flags */
319 1.1.1.3 dyoung ic->ic_stats.is_crypto_attachfail++;
320 1.1.1.3 dyoung return 0;
321 1.1 dyoung }
322 1.1.1.3 dyoung cipher_detach(key);
323 1.1.1.3 dyoung key->wk_cipher = cip; /* XXX refcnt? */
324 1.1.1.3 dyoung key->wk_private = keyctx;
325 1.1.1.3 dyoung }
326 1.1.1.3 dyoung /*
327 1.1.1.3 dyoung * Commit to requested usage so driver can see the flags.
328 1.1.1.3 dyoung */
329 1.1.1.3 dyoung key->wk_flags = flags;
330 1.1.1.3 dyoung
331 1.1.1.3 dyoung /*
332 1.1.1.3 dyoung * Ask the driver for a key index if we don't have one.
333 1.1.1.3 dyoung * Note that entries in the global key table always have
334 1.1.1.3 dyoung * an index; this means it's safe to call this routine
335 1.1.1.3 dyoung * for these entries just to setup the reference to the
336 1.1.1.3 dyoung * cipher template. Note also that when using software
337 1.1.1.3 dyoung * crypto we also call the driver to give us a key index.
338 1.1.1.3 dyoung */
339 1.1.1.3 dyoung if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
340 1.1.1.3 dyoung key->wk_keyix = dev_key_alloc(ic, key);
341 1.1.1.3 dyoung if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
342 1.1.1.3 dyoung /*
343 1.1.1.3 dyoung * Driver has no room; fallback to doing crypto
344 1.1.1.3 dyoung * in the host. We change the flags and start the
345 1.1.1.3 dyoung * procedure over. If we get back here then there's
346 1.1.1.3 dyoung * no hope and we bail. Note that this can leave
347 1.1.1.3 dyoung * the key in a inconsistent state if the caller
348 1.1.1.3 dyoung * continues to use it.
349 1.1.1.3 dyoung */
350 1.1.1.3 dyoung if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
351 1.1.1.3 dyoung ic->ic_stats.is_crypto_swfallback++;
352 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
353 1.1.1.3 dyoung "%s: no h/w resources for cipher %s, "
354 1.1.1.3 dyoung "falling back to s/w\n", __func__,
355 1.1.1.3 dyoung cip->ic_name);
356 1.1.1.3 dyoung oflags = key->wk_flags;
357 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWCRYPT;
358 1.1.1.3 dyoung if (cipher == IEEE80211_CIPHER_TKIP)
359 1.1.1.3 dyoung flags |= IEEE80211_KEY_SWMIC;
360 1.1.1.3 dyoung goto again;
361 1.1.1.2 dyoung }
362 1.1.1.3 dyoung ic->ic_stats.is_crypto_keyfail++;
363 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
364 1.1.1.3 dyoung "%s: unable to setup cipher %s\n",
365 1.1.1.3 dyoung __func__, cip->ic_name);
366 1.1.1.3 dyoung return 0;
367 1.1 dyoung }
368 1.1.1.3 dyoung }
369 1.1.1.3 dyoung return 1;
370 1.1.1.3 dyoung #undef N
371 1.1.1.3 dyoung }
372 1.1.1.3 dyoung
373 1.1.1.3 dyoung /*
374 1.1.1.3 dyoung * Remove the key (no locking, for internal use).
375 1.1.1.3 dyoung */
376 1.1.1.3 dyoung static int
377 1.1.1.3 dyoung _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
378 1.1.1.3 dyoung {
379 1.1.1.3 dyoung u_int16_t keyix;
380 1.1.1.3 dyoung
381 1.1.1.3 dyoung KASSERT(key->wk_cipher != NULL, ("No cipher!"));
382 1.1.1.3 dyoung
383 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
384 1.1.1.3 dyoung "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
385 1.1.1.3 dyoung __func__, key->wk_cipher->ic_name,
386 1.1.1.3 dyoung key->wk_keyix, key->wk_flags,
387 1.1.1.3 dyoung key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
388 1.1.1.3 dyoung
389 1.1.1.3 dyoung keyix = key->wk_keyix;
390 1.1.1.3 dyoung if (keyix != IEEE80211_KEYIX_NONE) {
391 1.1.1.3 dyoung /*
392 1.1.1.3 dyoung * Remove hardware entry.
393 1.1.1.3 dyoung */
394 1.1.1.3 dyoung /* XXX key cache */
395 1.1.1.3 dyoung if (!dev_key_delete(ic, key)) {
396 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
397 1.1.1.3 dyoung "%s: driver did not delete key index %u\n",
398 1.1.1.3 dyoung __func__, keyix);
399 1.1.1.3 dyoung ic->ic_stats.is_crypto_delkey++;
400 1.1.1.3 dyoung /* XXX recovery? */
401 1.1 dyoung }
402 1.1 dyoung }
403 1.1.1.3 dyoung cipher_detach(key);
404 1.1.1.3 dyoung memset(key, 0, sizeof(*key));
405 1.1.1.3 dyoung ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
406 1.1.1.3 dyoung return 1;
407 1.1.1.3 dyoung }
408 1.1 dyoung
409 1.1.1.3 dyoung /*
410 1.1.1.3 dyoung * Remove the specified key.
411 1.1.1.3 dyoung */
412 1.1.1.3 dyoung int
413 1.1.1.3 dyoung ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
414 1.1.1.3 dyoung {
415 1.1.1.3 dyoung int status;
416 1.1.1.3 dyoung
417 1.1.1.3 dyoung ieee80211_key_update_begin(ic);
418 1.1.1.3 dyoung status = _ieee80211_crypto_delkey(ic, key);
419 1.1.1.3 dyoung ieee80211_key_update_end(ic);
420 1.1.1.3 dyoung return status;
421 1.1 dyoung }
422 1.1 dyoung
423 1.1 dyoung /*
424 1.1.1.3 dyoung * Clear the global key table.
425 1.1 dyoung */
426 1.1.1.3 dyoung void
427 1.1.1.3 dyoung ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
428 1.1.1.3 dyoung {
429 1.1.1.3 dyoung int i;
430 1.1 dyoung
431 1.1.1.3 dyoung ieee80211_key_update_begin(ic);
432 1.1.1.3 dyoung for (i = 0; i < IEEE80211_WEP_NKID; i++)
433 1.1.1.3 dyoung (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
434 1.1.1.3 dyoung ieee80211_key_update_end(ic);
435 1.1.1.3 dyoung }
436 1.1 dyoung
437 1.1.1.3 dyoung /*
438 1.1.1.3 dyoung * Set the contents of the specified key.
439 1.1.1.3 dyoung *
440 1.1.1.3 dyoung * Locking must be handled by the caller using:
441 1.1.1.3 dyoung * ieee80211_key_update_begin(ic);
442 1.1.1.3 dyoung * ieee80211_key_update_end(ic);
443 1.1.1.3 dyoung */
444 1.1.1.3 dyoung int
445 1.1.1.3 dyoung ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
446 1.1.1.3 dyoung const u_int8_t macaddr[IEEE80211_ADDR_LEN])
447 1.1 dyoung {
448 1.1.1.3 dyoung const struct ieee80211_cipher *cip = key->wk_cipher;
449 1.1 dyoung
450 1.1.1.3 dyoung KASSERT(cip != NULL, ("No cipher!"));
451 1.1.1.3 dyoung
452 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
453 1.1.1.3 dyoung "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
454 1.1.1.3 dyoung __func__, cip->ic_name, key->wk_keyix,
455 1.1.1.3 dyoung key->wk_flags, ether_sprintf(macaddr),
456 1.1.1.3 dyoung key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
457 1.1.1.3 dyoung
458 1.1.1.3 dyoung /*
459 1.1.1.3 dyoung * Give cipher a chance to validate key contents.
460 1.1.1.3 dyoung * XXX should happen before modifying state.
461 1.1.1.3 dyoung */
462 1.1.1.3 dyoung if (!cip->ic_setkey(key)) {
463 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
464 1.1.1.3 dyoung "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
465 1.1.1.3 dyoung __func__, cip->ic_name, key->wk_keyix,
466 1.1.1.3 dyoung key->wk_keylen, key->wk_flags);
467 1.1.1.3 dyoung ic->ic_stats.is_crypto_setkey_cipher++;
468 1.1.1.3 dyoung return 0;
469 1.1.1.3 dyoung }
470 1.1.1.3 dyoung if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
471 1.1.1.3 dyoung /* XXX nothing allocated, should not happen */
472 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
473 1.1.1.3 dyoung "%s: no key index; should not happen!\n", __func__);
474 1.1.1.3 dyoung ic->ic_stats.is_crypto_setkey_nokey++;
475 1.1.1.3 dyoung return 0;
476 1.1 dyoung }
477 1.1.1.3 dyoung return dev_key_set(ic, key, macaddr);
478 1.1 dyoung }
479 1.1 dyoung
480 1.1 dyoung /*
481 1.1.1.3 dyoung * Add privacy headers appropriate for the specified key.
482 1.1 dyoung */
483 1.1.1.3 dyoung struct ieee80211_key *
484 1.1.1.3 dyoung ieee80211_crypto_encap(struct ieee80211com *ic,
485 1.1.1.3 dyoung struct ieee80211_node *ni, struct mbuf *m)
486 1.1.1.3 dyoung {
487 1.1.1.3 dyoung struct ieee80211_key *k;
488 1.1.1.3 dyoung struct ieee80211_frame *wh;
489 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
490 1.1.1.3 dyoung u_int8_t keyid;
491 1.1 dyoung
492 1.1.1.3 dyoung /*
493 1.1.1.3 dyoung * Multicast traffic always uses the multicast key.
494 1.1.1.3 dyoung * Otherwise if a unicast key is set we use that and
495 1.1.1.3 dyoung * it is always key index 0. When no unicast key is
496 1.1.1.3 dyoung * set we fall back to the default transmit key.
497 1.1.1.3 dyoung */
498 1.1.1.3 dyoung wh = mtod(m, struct ieee80211_frame *);
499 1.1.1.3 dyoung if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
500 1.1.1.3 dyoung ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
501 1.1.1.3 dyoung if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
502 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
503 1.1.1.3 dyoung "[%s] no default transmit key (%s) deftxkey %u\n",
504 1.1.1.3 dyoung ether_sprintf(wh->i_addr1), __func__,
505 1.1.1.3 dyoung ic->ic_def_txkey);
506 1.1.1.3 dyoung ic->ic_stats.is_tx_nodefkey++;
507 1.1.1.3 dyoung return NULL;
508 1.1.1.3 dyoung }
509 1.1.1.3 dyoung keyid = ic->ic_def_txkey;
510 1.1.1.3 dyoung k = &ic->ic_nw_keys[ic->ic_def_txkey];
511 1.1.1.3 dyoung } else {
512 1.1.1.3 dyoung keyid = 0;
513 1.1.1.3 dyoung k = &ni->ni_ucastkey;
514 1.1.1.3 dyoung }
515 1.1.1.3 dyoung cip = k->wk_cipher;
516 1.1.1.3 dyoung return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
517 1.1.1.3 dyoung }
518 1.1.1.3 dyoung
519 1.1.1.3 dyoung /*
520 1.1.1.3 dyoung * Validate and strip privacy headers (and trailer) for a
521 1.1.1.3 dyoung * received frame that has the WEP/Privacy bit set.
522 1.1.1.3 dyoung */
523 1.1.1.3 dyoung struct ieee80211_key *
524 1.1.1.3 dyoung ieee80211_crypto_decap(struct ieee80211com *ic,
525 1.1.1.3 dyoung struct ieee80211_node *ni, struct mbuf *m)
526 1.1 dyoung {
527 1.1.1.3 dyoung #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
528 1.1.1.3 dyoung #define IEEE80211_WEP_MINLEN \
529 1.1.1.3 dyoung (sizeof(struct ieee80211_frame) + ETHER_HDR_LEN + \
530 1.1.1.3 dyoung IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
531 1.1.1.3 dyoung struct ieee80211_key *k;
532 1.1.1.3 dyoung struct ieee80211_frame *wh;
533 1.1.1.3 dyoung const struct ieee80211_cipher *cip;
534 1.1.1.3 dyoung const u_int8_t *ivp;
535 1.1.1.3 dyoung u_int8_t keyid;
536 1.1.1.3 dyoung int hdrlen;
537 1.1.1.3 dyoung
538 1.1.1.3 dyoung /* NB: this minimum size data frame could be bigger */
539 1.1.1.3 dyoung if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
540 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
541 1.1.1.3 dyoung "%s: WEP data frame too short, len %u\n",
542 1.1.1.3 dyoung __func__, m->m_pkthdr.len);
543 1.1.1.3 dyoung ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */
544 1.1.1.3 dyoung return NULL;
545 1.1.1.3 dyoung }
546 1.1.1.3 dyoung
547 1.1.1.3 dyoung /*
548 1.1.1.3 dyoung * Locate the key. If unicast and there is no unicast
549 1.1.1.3 dyoung * key then we fall back to the key id in the header.
550 1.1.1.3 dyoung * This assumes unicast keys are only configured when
551 1.1.1.3 dyoung * the key id in the header is meaningless (typically 0).
552 1.1.1.3 dyoung */
553 1.1.1.3 dyoung wh = mtod(m, struct ieee80211_frame *);
554 1.1.1.3 dyoung hdrlen = ieee80211_hdrsize(wh);
555 1.1.1.3 dyoung ivp = mtod(m, const u_int8_t *) + hdrlen; /* XXX contig */
556 1.1.1.3 dyoung keyid = ivp[IEEE80211_WEP_IVLEN];
557 1.1.1.3 dyoung if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
558 1.1.1.3 dyoung ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)
559 1.1.1.3 dyoung k = &ic->ic_nw_keys[keyid >> 6];
560 1.1.1.3 dyoung else
561 1.1.1.3 dyoung k = &ni->ni_ucastkey;
562 1.1.1.3 dyoung
563 1.1.1.3 dyoung /*
564 1.1.1.3 dyoung * Insure crypto header is contiguous for all decap work.
565 1.1.1.3 dyoung */
566 1.1.1.3 dyoung cip = k->wk_cipher;
567 1.1.1.3 dyoung if (m->m_len < hdrlen + cip->ic_header &&
568 1.1.1.3 dyoung (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
569 1.1.1.3 dyoung IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
570 1.1.1.3 dyoung "[%s] unable to pullup %s header\n",
571 1.1.1.3 dyoung ether_sprintf(wh->i_addr2), cip->ic_name);
572 1.1.1.3 dyoung ic->ic_stats.is_rx_wepfail++; /* XXX */
573 1.1.1.3 dyoung return 0;
574 1.1.1.3 dyoung }
575 1.1 dyoung
576 1.1.1.3 dyoung return (cip->ic_decap(k, m) ? k : NULL);
577 1.1.1.3 dyoung #undef IEEE80211_WEP_MINLEN
578 1.1.1.3 dyoung #undef IEEE80211_WEP_HDRLEN
579 1.1 dyoung }
580