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