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