wlan - Update wlan from Adrian / FreeBSD
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_crypto.c
CommitLineData
32176cfd 1/*-
f186073c 2 * Copyright (c) 2001 Atsushi Onoe
32176cfd 3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
f186073c
JS
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.
f186073c
JS
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f186073c
JS
25 */
26
085ff963
MD
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
841ab66c
SZ
30/*
31 * IEEE 802.11 generic crypto support.
32 */
32176cfd
RP
33#include "opt_wlan.h"
34
f186073c 35#include <sys/param.h>
32176cfd
RP
36#include <sys/kernel.h>
37#include <sys/malloc.h>
f186073c 38#include <sys/mbuf.h>
841ab66c 39
f186073c 40#include <sys/socket.h>
841ab66c 41
f186073c 42#include <net/if.h>
841ab66c
SZ
43#include <net/if_media.h>
44#include <net/ethernet.h> /* XXX ETHER_HDR_LEN */
f186073c
JS
45
46#include <netproto/802_11/ieee80211_var.h>
47
32176cfd
RP
48MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
49
50static int _ieee80211_crypto_delkey(struct ieee80211vap *,
51 struct ieee80211_key *);
52
841ab66c
SZ
53/*
54 * Table of registered cipher modules.
55 */
56static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
57
841ab66c
SZ
58/*
59 * Default "null" key management routines.
60 */
61static int
32176cfd 62null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
841ab66c
SZ
63 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
64{
32176cfd
RP
65 if (!(&vap->iv_nw_keys[0] <= k &&
66 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
841ab66c
SZ
67 /*
68 * Not in the global key table, the driver should handle this
69 * by allocating a slot in the h/w key table/cache. In
70 * lieu of that return key slot 0 for any unicast key
71 * request. We disallow the request if this is a group key.
72 * This default policy does the right thing for legacy hardware
73 * with a 4 key table. It also handles devices that pass
74 * packets through untouched when marked with the WEP bit
75 * and key index 0.
76 */
77 if (k->wk_flags & IEEE80211_KEY_GROUP)
78 return 0;
79 *keyix = 0; /* NB: use key index 0 for ucast key */
80 } else {
32176cfd 81 *keyix = k - vap->iv_nw_keys;
841ab66c
SZ
82 }
83 *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */
84 return 1;
85}
841ab66c 86static int
32176cfd 87null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
841ab66c
SZ
88{
89 return 1;
90}
32176cfd
RP
91static int
92null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
93 const uint8_t mac[IEEE80211_ADDR_LEN])
841ab66c
SZ
94{
95 return 1;
96}
32176cfd 97static void null_key_update(struct ieee80211vap *vap) {}
841ab66c
SZ
98
99/*
100 * Write-arounds for common operations.
101 */
102static __inline void
103cipher_detach(struct ieee80211_key *key)
104{
105 key->wk_cipher->ic_detach(key);
106}
107
108static __inline void *
32176cfd 109cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
841ab66c 110{
32176cfd 111 return key->wk_cipher->ic_attach(vap, key);
841ab66c
SZ
112}
113
114/*
115 * Wrappers for driver key management methods.
116 */
117static __inline int
32176cfd
RP
118dev_key_alloc(struct ieee80211vap *vap,
119 struct ieee80211_key *key,
841ab66c
SZ
120 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
121{
32176cfd 122 return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
841ab66c 123}
f186073c 124
841ab66c 125static __inline int
32176cfd 126dev_key_delete(struct ieee80211vap *vap,
841ab66c
SZ
127 const struct ieee80211_key *key)
128{
32176cfd 129 return vap->iv_key_delete(vap, key);
841ab66c 130}
f186073c 131
841ab66c 132static __inline int
32176cfd 133dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
841ab66c 134{
32176cfd 135 return vap->iv_key_set(vap, key, key->wk_macaddr);
841ab66c 136}
f186073c 137
841ab66c 138/*
32176cfd 139 * Setup crypto support for a device/shared instance.
841ab66c 140 */
f186073c 141void
841ab66c 142ieee80211_crypto_attach(struct ieee80211com *ic)
f186073c 143{
32176cfd
RP
144 /* NB: we assume everything is pre-zero'd */
145 ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
146}
147
148/*
149 * Teardown crypto support.
150 */
151void
152ieee80211_crypto_detach(struct ieee80211com *ic)
153{
154}
155
156/*
157 * Setup crypto support for a vap.
158 */
159void
160ieee80211_crypto_vattach(struct ieee80211vap *vap)
161{
841ab66c 162 int i;
f186073c 163
841ab66c 164 /* NB: we assume everything is pre-zero'd */
32176cfd
RP
165 vap->iv_max_keyix = IEEE80211_WEP_NKID;
166 vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
841ab66c 167 for (i = 0; i < IEEE80211_WEP_NKID; i++)
32176cfd 168 ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
841ab66c 169 IEEE80211_KEYIX_NONE);
f186073c 170 /*
841ab66c
SZ
171 * Initialize the driver key support routines to noop entries.
172 * This is useful especially for the cipher test modules.
f186073c 173 */
32176cfd
RP
174 vap->iv_key_alloc = null_key_alloc;
175 vap->iv_key_set = null_key_set;
176 vap->iv_key_delete = null_key_delete;
177 vap->iv_key_update_begin = null_key_update;
178 vap->iv_key_update_end = null_key_update;
f186073c
JS
179}
180
841ab66c 181/*
32176cfd 182 * Teardown crypto support for a vap.
841ab66c 183 */
f186073c 184void
32176cfd 185ieee80211_crypto_vdetach(struct ieee80211vap *vap)
f186073c 186{
32176cfd 187 ieee80211_crypto_delglobalkeys(vap);
841ab66c 188}
f186073c 189
841ab66c
SZ
190/*
191 * Register a crypto cipher module.
192 */
193void
194ieee80211_crypto_register(const struct ieee80211_cipher *cip)
195{
196 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
a6ec04bc 197 kprintf("%s: cipher %s has an invalid cipher index %u\n",
841ab66c
SZ
198 __func__, cip->ic_name, cip->ic_cipher);
199 return;
f186073c 200 }
841ab66c 201 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
a6ec04bc 202 kprintf("%s: cipher %s registered with a different template\n",
841ab66c
SZ
203 __func__, cip->ic_name);
204 return;
205 }
206 ciphers[cip->ic_cipher] = cip;
f186073c
JS
207}
208
841ab66c
SZ
209/*
210 * Unregister a crypto cipher module.
211 */
212void
213ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
f186073c 214{
841ab66c 215 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
a6ec04bc 216 kprintf("%s: cipher %s has an invalid cipher index %u\n",
841ab66c
SZ
217 __func__, cip->ic_name, cip->ic_cipher);
218 return;
f186073c 219 }
841ab66c 220 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
a6ec04bc 221 kprintf("%s: cipher %s registered with a different template\n",
841ab66c
SZ
222 __func__, cip->ic_name);
223 return;
f186073c 224 }
841ab66c
SZ
225 /* NB: don't complain about not being registered */
226 /* XXX disallow if references */
227 ciphers[cip->ic_cipher] = NULL;
228}
229
230int
231ieee80211_crypto_available(u_int cipher)
232{
233 return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
234}
235
236/* XXX well-known names! */
32176cfd
RP
237static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
238 [IEEE80211_CIPHER_WEP] = "wlan_wep",
239 [IEEE80211_CIPHER_TKIP] = "wlan_tkip",
240 [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
241 [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
242 [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */
243 [IEEE80211_CIPHER_CKIP] = "wlan_ckip",
244 [IEEE80211_CIPHER_NONE] = "wlan_none",
841ab66c
SZ
245};
246
32176cfd
RP
247/* NB: there must be no overlap between user-supplied and device-owned flags */
248CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
249
841ab66c
SZ
250/*
251 * Establish a relationship between the specified key and cipher
252 * and, if necessary, allocate a hardware index from the driver.
32176cfd 253 * Note that when a fixed key index is required it must be specified.
841ab66c
SZ
254 *
255 * This must be the first call applied to a key; all the other key
256 * routines assume wk_cipher is setup.
257 *
258 * Locking must be handled by the caller using:
32176cfd
RP
259 * ieee80211_key_update_begin(vap);
260 * ieee80211_key_update_end(vap);
841ab66c
SZ
261 */
262int
32176cfd 263ieee80211_crypto_newkey(struct ieee80211vap *vap,
841ab66c
SZ
264 int cipher, int flags, struct ieee80211_key *key)
265{
32176cfd 266 struct ieee80211com *ic = vap->iv_ic;
841ab66c
SZ
267 const struct ieee80211_cipher *cip;
268 ieee80211_keyix keyix, rxkeyix;
269 void *keyctx;
270 int oflags;
271
32176cfd
RP
272 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
273 "%s: cipher %u flags 0x%x keyix %u\n",
274 __func__, cipher, flags, key->wk_keyix);
275
841ab66c
SZ
276 /*
277 * Validate cipher and set reference to cipher routines.
278 */
279 if (cipher >= IEEE80211_CIPHER_MAX) {
32176cfd
RP
280 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
281 "%s: invalid cipher %u\n", __func__, cipher);
282 vap->iv_stats.is_crypto_badcipher++;
841ab66c 283 return 0;
f186073c 284 }
841ab66c
SZ
285 cip = ciphers[cipher];
286 if (cip == NULL) {
f186073c 287 /*
841ab66c
SZ
288 * Auto-load cipher module if we have a well-known name
289 * for it. It might be better to use string names rather
290 * than numbers and craft a module name based on the cipher
291 * name; e.g. wlan_cipher_<cipher-name>.
f186073c 292 */
32176cfd
RP
293 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
294 "%s: unregistered cipher %u, load module %s\n",
295 __func__, cipher, cipher_modnames[cipher]);
296 ieee80211_load_module(cipher_modnames[cipher]);
297 /*
298 * If cipher module loaded it should immediately
299 * call ieee80211_crypto_register which will fill
300 * in the entry in the ciphers array.
301 */
302 cip = ciphers[cipher];
841ab66c 303 if (cip == NULL) {
32176cfd
RP
304 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
305 "%s: unable to load cipher %u, module %s\n",
306 __func__, cipher, cipher_modnames[cipher]);
307 vap->iv_stats.is_crypto_nocipher++;
841ab66c 308 return 0;
f186073c 309 }
841ab66c
SZ
310 }
311
312 oflags = key->wk_flags;
313 flags &= IEEE80211_KEY_COMMON;
32176cfd
RP
314 /* NB: preserve device attributes */
315 flags |= (oflags & IEEE80211_KEY_DEVICE);
841ab66c
SZ
316 /*
317 * If the hardware does not support the cipher then
318 * fallback to a host-based implementation.
319 */
32176cfd
RP
320 if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
321 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
322 "%s: no h/w support for cipher %s, falling back to s/w\n",
323 __func__, cip->ic_name);
324 flags |= IEEE80211_KEY_SWCRYPT;
325 }
326 /*
327 * Hardware TKIP with software MIC is an important
328 * combination; we handle it by flagging each key,
329 * the cipher modules honor it.
330 */
331 if (cipher == IEEE80211_CIPHER_TKIP &&
32176cfd
RP
332 (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
333 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
334 "%s: no h/w support for TKIP MIC, falling back to s/w\n",
335 __func__);
336 flags |= IEEE80211_KEY_SWMIC;
337 }
338
339 /*
340 * Bind cipher to key instance. Note we do this
341 * after checking the device capabilities so the
342 * cipher module can optimize space usage based on
343 * whether or not it needs to do the cipher work.
344 */
345 if (key->wk_cipher != cip || key->wk_flags != flags) {
841ab66c
SZ
346 /*
347 * Fillin the flags so cipher modules can see s/w
348 * crypto requirements and potentially allocate
349 * different state and/or attach different method
350 * pointers.
841ab66c
SZ
351 */
352 key->wk_flags = flags;
32176cfd 353 keyctx = cip->ic_attach(vap, key);
841ab66c 354 if (keyctx == NULL) {
32176cfd 355 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
356 "%s: unable to attach cipher %s\n",
357 __func__, cip->ic_name);
358 key->wk_flags = oflags; /* restore old flags */
32176cfd 359 vap->iv_stats.is_crypto_attachfail++;
841ab66c 360 return 0;
f186073c 361 }
32176cfd 362 cipher_detach(key);
841ab66c
SZ
363 key->wk_cipher = cip; /* XXX refcnt? */
364 key->wk_private = keyctx;
f186073c 365 }
841ab66c
SZ
366
367 /*
368 * Ask the driver for a key index if we don't have one.
369 * Note that entries in the global key table always have
370 * an index; this means it's safe to call this routine
371 * for these entries just to setup the reference to the
372 * cipher template. Note also that when using software
373 * crypto we also call the driver to give us a key index.
374 */
32176cfd
RP
375 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
376 if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
841ab66c 377 /*
32176cfd 378 * Unable to setup driver state.
841ab66c 379 */
32176cfd
RP
380 vap->iv_stats.is_crypto_keyfail++;
381 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
382 "%s: unable to setup cipher %s\n",
383 __func__, cip->ic_name);
384 return 0;
f186073c 385 }
32176cfd
RP
386 if (key->wk_flags != flags) {
387 /*
388 * Driver overrode flags we setup; typically because
389 * resources were unavailable to handle _this_ key.
390 * Re-attach the cipher context to allow cipher
391 * modules to handle differing requirements.
392 */
393 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
394 "%s: driver override for cipher %s, flags "
395 "0x%x -> 0x%x\n", __func__, cip->ic_name,
396 oflags, key->wk_flags);
397 keyctx = cip->ic_attach(vap, key);
398 if (keyctx == NULL) {
399 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
400 "%s: unable to attach cipher %s with "
401 "flags 0x%x\n", __func__, cip->ic_name,
402 key->wk_flags);
403 key->wk_flags = oflags; /* restore old flags */
404 vap->iv_stats.is_crypto_attachfail++;
405 return 0;
406 }
407 cipher_detach(key);
408 key->wk_cipher = cip; /* XXX refcnt? */
409 key->wk_private = keyctx;
410 }
841ab66c
SZ
411 key->wk_keyix = keyix;
412 key->wk_rxkeyix = rxkeyix;
32176cfd 413 key->wk_flags |= IEEE80211_KEY_DEVKEY;
841ab66c
SZ
414 }
415 return 1;
841ab66c
SZ
416}
417
418/*
419 * Remove the key (no locking, for internal use).
420 */
421static int
32176cfd 422_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
841ab66c 423{
841ab66c
SZ
424 KASSERT(key->wk_cipher != NULL, ("No cipher!"));
425
32176cfd 426 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
427 "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
428 __func__, key->wk_cipher->ic_name,
429 key->wk_keyix, key->wk_flags,
32176cfd
RP
430 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
431 key->wk_keylen);
841ab66c 432
32176cfd 433 if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
841ab66c
SZ
434 /*
435 * Remove hardware entry.
436 */
437 /* XXX key cache */
32176cfd
RP
438 if (!dev_key_delete(vap, key)) {
439 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c 440 "%s: driver did not delete key index %u\n",
32176cfd
RP
441 __func__, key->wk_keyix);
442 vap->iv_stats.is_crypto_delkey++;
841ab66c 443 /* XXX recovery? */
f186073c
JS
444 }
445 }
841ab66c
SZ
446 cipher_detach(key);
447 memset(key, 0, sizeof(*key));
32176cfd 448 ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
841ab66c
SZ
449 return 1;
450}
451
452/*
453 * Remove the specified key.
454 */
455int
32176cfd 456ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
841ab66c
SZ
457{
458 int status;
f186073c 459
32176cfd
RP
460 ieee80211_key_update_begin(vap);
461 status = _ieee80211_crypto_delkey(vap, key);
462 ieee80211_key_update_end(vap);
841ab66c 463 return status;
f186073c
JS
464}
465
466/*
841ab66c 467 * Clear the global key table.
f186073c 468 */
841ab66c 469void
32176cfd 470ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
841ab66c
SZ
471{
472 int i;
f186073c 473
32176cfd 474 ieee80211_key_update_begin(vap);
841ab66c 475 for (i = 0; i < IEEE80211_WEP_NKID; i++)
32176cfd
RP
476 (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
477 ieee80211_key_update_end(vap);
841ab66c 478}
f186073c 479
841ab66c
SZ
480/*
481 * Set the contents of the specified key.
482 *
483 * Locking must be handled by the caller using:
32176cfd
RP
484 * ieee80211_key_update_begin(vap);
485 * ieee80211_key_update_end(vap);
841ab66c
SZ
486 */
487int
32176cfd 488ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
f186073c 489{
841ab66c
SZ
490 const struct ieee80211_cipher *cip = key->wk_cipher;
491
492 KASSERT(cip != NULL, ("No cipher!"));
493
32176cfd 494 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
1e290df3 495 "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
841ab66c 496 __func__, cip->ic_name, key->wk_keyix,
085ff963 497 key->wk_flags, ether_sprintf(key->wk_macaddr),
32176cfd
RP
498 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
499 key->wk_keylen);
841ab66c 500
32176cfd
RP
501 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
502 /* XXX nothing allocated, should not happen */
503 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
504 "%s: no device key setup done; should not happen!\n",
505 __func__);
506 vap->iv_stats.is_crypto_setkey_nokey++;
507 return 0;
508 }
841ab66c
SZ
509 /*
510 * Give cipher a chance to validate key contents.
511 * XXX should happen before modifying state.
512 */
513 if (!cip->ic_setkey(key)) {
32176cfd 514 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
841ab66c
SZ
515 "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
516 __func__, cip->ic_name, key->wk_keyix,
517 key->wk_keylen, key->wk_flags);
32176cfd 518 vap->iv_stats.is_crypto_setkey_cipher++;
841ab66c
SZ
519 return 0;
520 }
32176cfd 521 return dev_key_set(vap, key);
f186073c
JS
522}
523
524/*
841ab66c 525 * Add privacy headers appropriate for the specified key.
f186073c 526 */
841ab66c 527struct ieee80211_key *
32176cfd 528ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
841ab66c 529{
32176cfd 530 struct ieee80211vap *vap = ni->ni_vap;
841ab66c
SZ
531 struct ieee80211_key *k;
532 struct ieee80211_frame *wh;
32176cfd
RP
533 const struct ieee80211_cipher *cip;
534 uint8_t keyid;
f186073c 535
841ab66c
SZ
536 /*
537 * Multicast traffic always uses the multicast key.
538 * Otherwise if a unicast key is set we use that and
539 * it is always key index 0. When no unicast key is
540 * set we fall back to the default transmit key.
541 */
542 wh = mtod(m, struct ieee80211_frame *);
543 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
32176cfd
RP
544 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
545 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
546 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
547 wh->i_addr1,
548 "no default transmit key (%s) deftxkey %u",
549 __func__, vap->iv_def_txkey);
550 vap->iv_stats.is_tx_nodefkey++;
841ab66c
SZ
551 return NULL;
552 }
32176cfd
RP
553 keyid = vap->iv_def_txkey;
554 k = &vap->iv_nw_keys[vap->iv_def_txkey];
841ab66c 555 } else {
32176cfd 556 keyid = 0;
841ab66c
SZ
557 k = &ni->ni_ucastkey;
558 }
32176cfd
RP
559 cip = k->wk_cipher;
560 return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
841ab66c
SZ
561}
562
563/*
564 * Validate and strip privacy headers (and trailer) for a
565 * received frame that has the WEP/Privacy bit set.
566 */
567struct ieee80211_key *
32176cfd 568ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
f186073c 569{
841ab66c
SZ
570#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
571#define IEEE80211_WEP_MINLEN \
572 (sizeof(struct ieee80211_frame) + \
573 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
32176cfd 574 struct ieee80211vap *vap = ni->ni_vap;
841ab66c
SZ
575 struct ieee80211_key *k;
576 struct ieee80211_frame *wh;
577 const struct ieee80211_cipher *cip;
841ab66c
SZ
578 uint8_t keyid;
579
580 /* NB: this minimum size data frame could be bigger */
581 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
32176cfd 582 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
841ab66c
SZ
583 "%s: WEP data frame too short, len %u\n",
584 __func__, m->m_pkthdr.len);
32176cfd 585 vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */
841ab66c
SZ
586 return NULL;
587 }
588
589 /*
590 * Locate the key. If unicast and there is no unicast
591 * key then we fall back to the key id in the header.
592 * This assumes unicast keys are only configured when
593 * the key id in the header is meaningless (typically 0).
594 */
595 wh = mtod(m, struct ieee80211_frame *);
32176cfd 596 m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
841ab66c 597 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
32176cfd
RP
598 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
599 k = &vap->iv_nw_keys[keyid >> 6];
841ab66c
SZ
600 else
601 k = &ni->ni_ucastkey;
602
603 /*
604 * Insure crypto header is contiguous for all decap work.
605 */
606 cip = k->wk_cipher;
607 if (m->m_len < hdrlen + cip->ic_header &&
608 (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
32176cfd
RP
609 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
610 "unable to pullup %s header", cip->ic_name);
611 vap->iv_stats.is_rx_wepfail++; /* XXX */
612 return NULL;
841ab66c 613 }
f186073c 614
841ab66c
SZ
615 return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
616#undef IEEE80211_WEP_MINLEN
617#undef IEEE80211_WEP_HDRLEN
f186073c 618}
6bd66811 619
32176cfd
RP
620static void
621load_ucastkey(void *arg, struct ieee80211_node *ni)
6bd66811 622{
32176cfd 623 struct ieee80211vap *vap = ni->ni_vap;
6bd66811
SZ
624 struct ieee80211_key *k;
625
32176cfd
RP
626 if (vap->iv_state != IEEE80211_S_RUN)
627 return;
628 k = &ni->ni_ucastkey;
629 if (k->wk_flags & IEEE80211_KEY_DEVKEY)
630 dev_key_set(vap, k);
631}
632
633/*
634 * Re-load all keys known to the 802.11 layer that may
635 * have hardware state backing them. This is used by
636 * drivers on resume to push keys down into the device.
637 */
638void
639ieee80211_crypto_reload_keys(struct ieee80211com *ic)
640{
641 struct ieee80211vap *vap;
642 int i;
643
6bd66811 644 /*
32176cfd 645 * Keys in the global key table of each vap.
6bd66811 646 */
32176cfd
RP
647 /* NB: used only during resume so don't lock for now */
648 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
649 if (vap->iv_state != IEEE80211_S_RUN)
650 continue;
651 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
652 const struct ieee80211_key *k = &vap->iv_nw_keys[i];
653 if (k->wk_flags & IEEE80211_KEY_DEVKEY)
654 dev_key_set(vap, k);
655 }
6bd66811 656 }
32176cfd
RP
657 /*
658 * Unicast keys.
659 */
660 ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
6bd66811 661}