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