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