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  * $FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.10.2.2 2005/09/03 22:40:02 sam Exp $
33  * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_crypto.c,v 1.5 2006/12/22 23:57:53 swildner Exp $
34  */
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_arp.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
48 
49 #include <netproto/802_11/ieee80211_var.h>
50 
51 /*
52  * Table of registered cipher modules.
53  */
54 static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
55 
56 static	int _ieee80211_crypto_delkey(struct ieee80211com *,
57 		struct ieee80211_key *);
58 
59 /*
60  * Default "null" key management routines.
61  */
62 static int
63 null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
64 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
65 {
66 	if (!(&ic->ic_nw_keys[0] <= k &&
67 	     k < &ic->ic_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 - ic->ic_nw_keys;
83 	}
84 	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
85 	return 1;
86 }
87 
88 static int
89 null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
90 {
91 	return 1;
92 }
93 
94 static int
95 null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
96 	     const uint8_t mac[IEEE80211_ADDR_LEN])
97 {
98 	return 1;
99 }
100 
101 static void null_key_update(struct ieee80211com *ic)
102 {
103 }
104 
105 /*
106  * Write-arounds for common operations.
107  */
108 static __inline void
109 cipher_detach(struct ieee80211_key *key)
110 {
111 	key->wk_cipher->ic_detach(key);
112 }
113 
114 static __inline void *
115 cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key)
116 {
117 	return key->wk_cipher->ic_attach(ic, key);
118 }
119 
120 /*
121  * Wrappers for driver key management methods.
122  */
123 static __inline int
124 dev_key_alloc(struct ieee80211com *ic,
125 	const struct ieee80211_key *key,
126 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
127 {
128 	return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix);
129 }
130 
131 static __inline int
132 dev_key_delete(struct ieee80211com *ic,
133 	const struct ieee80211_key *key)
134 {
135 	return ic->ic_crypto.cs_key_delete(ic, key);
136 }
137 
138 static __inline int
139 dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
140 	const uint8_t mac[IEEE80211_ADDR_LEN])
141 {
142 	return ic->ic_crypto.cs_key_set(ic, key, mac);
143 }
144 
145 /*
146  * Setup crypto support.
147  */
148 void
149 ieee80211_crypto_attach(struct ieee80211com *ic)
150 {
151 	struct ieee80211_crypto_state *cs = &ic->ic_crypto;
152 	int i;
153 
154 	/* NB: we assume everything is pre-zero'd */
155 	cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
156 	cs->cs_max_keyix = IEEE80211_WEP_NKID;
157 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
158 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
159 		ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
160 			IEEE80211_KEYIX_NONE);
161 	/*
162 	 * Initialize the driver key support routines to noop entries.
163 	 * This is useful especially for the cipher test modules.
164 	 */
165 	cs->cs_key_alloc = null_key_alloc;
166 	cs->cs_key_set = null_key_set;
167 	cs->cs_key_delete = null_key_delete;
168 	cs->cs_key_update_begin = null_key_update;
169 	cs->cs_key_update_end = null_key_update;
170 }
171 
172 /*
173  * Teardown crypto support.
174  */
175 void
176 ieee80211_crypto_detach(struct ieee80211com *ic)
177 {
178 	ieee80211_crypto_delglobalkeys(ic);
179 }
180 
181 /*
182  * Register a crypto cipher module.
183  */
184 void
185 ieee80211_crypto_register(const struct ieee80211_cipher *cip)
186 {
187 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
188 		kprintf("%s: cipher %s has an invalid cipher index %u\n",
189 			__func__, cip->ic_name, cip->ic_cipher);
190 		return;
191 	}
192 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
193 		kprintf("%s: cipher %s registered with a different template\n",
194 			__func__, cip->ic_name);
195 		return;
196 	}
197 	ciphers[cip->ic_cipher] = cip;
198 }
199 
200 /*
201  * Unregister a crypto cipher module.
202  */
203 void
204 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
205 {
206 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
207 		kprintf("%s: cipher %s has an invalid cipher index %u\n",
208 			__func__, cip->ic_name, cip->ic_cipher);
209 		return;
210 	}
211 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
212 		kprintf("%s: cipher %s registered with a different template\n",
213 			__func__, cip->ic_name);
214 		return;
215 	}
216 	/* NB: don't complain about not being registered */
217 	/* XXX disallow if references */
218 	ciphers[cip->ic_cipher] = NULL;
219 }
220 
221 int
222 ieee80211_crypto_available(u_int cipher)
223 {
224 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
225 }
226 
227 const struct ieee80211_cipher *
228 ieee80211_crypto_cipher(u_int cipher)
229 {
230 	return cipher < IEEE80211_CIPHER_MAX ? ciphers[cipher] : NULL;
231 }
232 
233 /* XXX well-known names! */
234 static const char *cipher_modnames[] = {
235 	"wlan_wep",	/* IEEE80211_CIPHER_WEP */
236 	"wlan_tkip",	/* IEEE80211_CIPHER_TKIP */
237 	"wlan_aes_ocb",	/* IEEE80211_CIPHER_AES_OCB */
238 	"wlan_ccmp",	/* IEEE80211_CIPHER_AES_CCM */
239 	"wlan_ckip",	/* IEEE80211_CIPHER_CKIP */
240 };
241 
242 /*
243  * Establish a relationship between the specified key and cipher
244  * and, if necessary, allocate a hardware index from the driver.
245  * Note that when a fixed key index is required it must be specified
246  * and we blindly assign it w/o consulting the driver (XXX).
247  *
248  * This must be the first call applied to a key; all the other key
249  * routines assume wk_cipher is setup.
250  *
251  * Locking must be handled by the caller using:
252  *	ieee80211_key_update_begin(ic);
253  *	ieee80211_key_update_end(ic);
254  */
255 int
256 ieee80211_crypto_newkey(struct ieee80211com *ic,
257 	int cipher, int flags, struct ieee80211_key *key)
258 {
259 #define	N(a)	(sizeof(a) / sizeof(a[0]))
260 	const struct ieee80211_cipher *cip;
261 	ieee80211_keyix keyix, rxkeyix;
262 	void *keyctx;
263 	int oflags;
264 
265 	/*
266 	 * Validate cipher and set reference to cipher routines.
267 	 */
268 	if (cipher >= IEEE80211_CIPHER_MAX) {
269 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
270 			"%s: invalid cipher %u\n", __func__, cipher);
271 		ic->ic_stats.is_crypto_badcipher++;
272 		return 0;
273 	}
274 	cip = ciphers[cipher];
275 	if (cip == NULL) {
276 		/*
277 		 * Auto-load cipher module if we have a well-known name
278 		 * for it.  It might be better to use string names rather
279 		 * than numbers and craft a module name based on the cipher
280 		 * name; e.g. wlan_cipher_<cipher-name>.
281 		 */
282 		if (cipher < N(cipher_modnames)) {
283 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
284 				"%s: unregistered cipher %u, load module %s\n",
285 				__func__, cipher, cipher_modnames[cipher]);
286 			ieee80211_load_module(cipher_modnames[cipher]);
287 			/*
288 			 * If cipher module loaded it should immediately
289 			 * call ieee80211_crypto_register which will fill
290 			 * in the entry in the ciphers array.
291 			 */
292 			cip = ciphers[cipher];
293 		}
294 		if (cip == NULL) {
295 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
296 				"%s: unable to load cipher %u, module %s\n",
297 				__func__, cipher,
298 				cipher < N(cipher_modnames) ?
299 					cipher_modnames[cipher] : "<unknown>");
300 			ic->ic_stats.is_crypto_nocipher++;
301 			return 0;
302 		}
303 	}
304 
305 	oflags = key->wk_flags;
306 	flags &= IEEE80211_KEY_COMMON;
307 	/*
308 	 * If the hardware does not support the cipher then
309 	 * fallback to a host-based implementation.
310 	 */
311 	if ((ic->ic_caps & (1<<cipher)) == 0) {
312 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
313 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
314 		    __func__, cip->ic_name);
315 		flags |= IEEE80211_KEY_SWCRYPT;
316 	}
317 	/*
318 	 * Hardware TKIP with software MIC is an important
319 	 * combination; we handle it by flagging each key,
320 	 * the cipher modules honor it.
321 	 */
322 	if (cipher == IEEE80211_CIPHER_TKIP &&
323 	    (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
324 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
325 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
326 		    __func__);
327 		flags |= IEEE80211_KEY_SWMIC;
328 	}
329 
330 	/*
331 	 * Bind cipher to key instance.  Note we do this
332 	 * after checking the device capabilities so the
333 	 * cipher module can optimize space usage based on
334 	 * whether or not it needs to do the cipher work.
335 	 */
336 	if (key->wk_cipher != cip || key->wk_flags != flags) {
337 again:
338 		/*
339 		 * Fillin the flags so cipher modules can see s/w
340 		 * crypto requirements and potentially allocate
341 		 * different state and/or attach different method
342 		 * pointers.
343 		 *
344 		 * XXX this is not right when s/w crypto fallback
345 		 *     fails and we try to restore previous state.
346 		 */
347 		key->wk_flags = flags;
348 		keyctx = cip->ic_attach(ic, key); /* attach new cipher */
349 		if (keyctx == NULL) {
350 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
351 				"%s: unable to attach cipher %s\n",
352 				__func__, cip->ic_name);
353 			key->wk_flags = oflags;	/* restore old flags */
354 			ic->ic_stats.is_crypto_attachfail++;
355 			return 0;
356 		}
357 		cipher_detach(key);		/* detach old cipher */
358 		key->wk_cipher = cip;		/* XXX refcnt? */
359 		key->wk_private = keyctx;
360 	}
361 	/*
362 	 * Commit to requested usage so driver can see the flags.
363 	 */
364 	key->wk_flags = flags;
365 
366 	/*
367 	 * Ask the driver for a key index if we don't have one.
368 	 * Note that entries in the global key table always have
369 	 * an index; this means it's safe to call this routine
370 	 * for these entries just to setup the reference to the
371 	 * cipher template.  Note also that when using software
372 	 * crypto we also call the driver to give us a key index.
373 	 */
374 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
375 		if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) {
376 			/*
377 			 * Driver has no room; fallback to doing crypto
378 			 * in the host.  We change the flags and start the
379 			 * procedure over.  If we get back here then there's
380 			 * no hope and we bail.  Note that this can leave
381 			 * the key in a inconsistent state if the caller
382 			 * continues to use it.
383 			 */
384 			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
385 				ic->ic_stats.is_crypto_swfallback++;
386 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
387 				    "%s: no h/w resources for cipher %s, "
388 				    "falling back to s/w\n", __func__,
389 				    cip->ic_name);
390 				oflags = key->wk_flags;
391 				flags |= IEEE80211_KEY_SWCRYPT;
392 				if (cipher == IEEE80211_CIPHER_TKIP)
393 					flags |= IEEE80211_KEY_SWMIC;
394 				goto again;
395 			}
396 			ic->ic_stats.is_crypto_keyfail++;
397 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
398 			    "%s: unable to setup cipher %s\n",
399 			    __func__, cip->ic_name);
400 			return 0;
401 		}
402 		key->wk_keyix = keyix;
403 		key->wk_rxkeyix = rxkeyix;
404 	}
405 	return 1;
406 #undef N
407 }
408 
409 /*
410  * Remove the key (no locking, for internal use).
411  */
412 static int
413 _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
414 {
415 	ieee80211_keyix keyix;
416 
417 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
418 
419 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
420 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
421 	    __func__, key->wk_cipher->ic_name,
422 	    key->wk_keyix, key->wk_flags,
423 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
424 
425 	keyix = key->wk_keyix;
426 	if (keyix != IEEE80211_KEYIX_NONE) {
427 		/*
428 		 * Remove hardware entry.
429 		 */
430 		/* XXX key cache */
431 		if (!dev_key_delete(ic, key)) {
432 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
433 			    "%s: driver did not delete key index %u\n",
434 			    __func__, keyix);
435 			ic->ic_stats.is_crypto_delkey++;
436 			/* XXX recovery? */
437 		}
438 	}
439 	cipher_detach(key);
440 	memset(key, 0, sizeof(*key));
441 	ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
442 	return 1;
443 }
444 
445 /*
446  * Remove the specified key.
447  */
448 int
449 ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
450 {
451 	int status;
452 
453 	ieee80211_key_update_begin(ic);
454 	status = _ieee80211_crypto_delkey(ic, key);
455 	ieee80211_key_update_end(ic);
456 	return status;
457 }
458 
459 /*
460  * Clear the global key table.
461  */
462 void
463 ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
464 {
465 	int i;
466 
467 	ieee80211_key_update_begin(ic);
468 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
469 		_ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
470 	ieee80211_key_update_end(ic);
471 }
472 
473 /*
474  * Set the contents of the specified key.
475  *
476  * Locking must be handled by the caller using:
477  *	ieee80211_key_update_begin(ic);
478  *	ieee80211_key_update_end(ic);
479  */
480 int
481 ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
482 		const uint8_t macaddr[IEEE80211_ADDR_LEN])
483 {
484 	const struct ieee80211_cipher *cip = key->wk_cipher;
485 
486 	KASSERT(cip != NULL, ("No cipher!"));
487 
488 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
489 	    "%s: %s keyix %u flags 0x%x mac %6D rsc %ju tsc %ju len %u\n",
490 	    __func__, cip->ic_name, key->wk_keyix,
491 	    key->wk_flags, macaddr, ":",
492 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
493 
494 	/*
495 	 * Give cipher a chance to validate key contents.
496 	 * XXX should happen before modifying state.
497 	 */
498 	if (!cip->ic_setkey(key)) {
499 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
500 		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
501 		    __func__, cip->ic_name, key->wk_keyix,
502 		    key->wk_keylen, key->wk_flags);
503 		ic->ic_stats.is_crypto_setkey_cipher++;
504 		return 0;
505 	}
506 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
507 		/* XXX nothing allocated, should not happen */
508 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
509 		    "%s: no key index; should not happen!\n", __func__);
510 		ic->ic_stats.is_crypto_setkey_nokey++;
511 		return 0;
512 	}
513 	return dev_key_set(ic, key, macaddr);
514 }
515 
516 /*
517  * Add privacy headers appropriate for the specified key.
518  */
519 struct ieee80211_key *
520 ieee80211_crypto_encap(struct ieee80211com *ic,
521 	struct ieee80211_node *ni, struct mbuf *m)
522 {
523 	struct ieee80211_key *k;
524 	struct ieee80211_frame *wh;
525 	const struct ieee80211_cipher *cip;
526 	uint8_t keyid;
527 
528 	/*
529 	 * Multicast traffic always uses the multicast key.
530 	 * Otherwise if a unicast key is set we use that and
531 	 * it is always key index 0.  When no unicast key is
532 	 * set we fall back to the default transmit key.
533 	 */
534 	wh = mtod(m, struct ieee80211_frame *);
535 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
536 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
537 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
538 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
539 			    "[%6D] no default transmit key (%s) deftxkey %u\n",
540 			    wh->i_addr1, ":", __func__,
541 			    ic->ic_def_txkey);
542 			ic->ic_stats.is_tx_nodefkey++;
543 			return NULL;
544 		}
545 		keyid = ic->ic_def_txkey;
546 		k = &ic->ic_nw_keys[ic->ic_def_txkey];
547 	} else {
548 		keyid = 0;
549 		k = &ni->ni_ucastkey;
550 	}
551 	cip = k->wk_cipher;
552 	return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
553 }
554 
555 /*
556  * Validate and strip privacy headers (and trailer) for a
557  * received frame that has the WEP/Privacy bit set.
558  */
559 struct ieee80211_key *
560 ieee80211_crypto_decap(struct ieee80211com *ic,
561 	struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
562 {
563 #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
564 #define	IEEE80211_WEP_MINLEN \
565 	(sizeof(struct ieee80211_frame) + \
566 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
567 	struct ieee80211_key *k;
568 	struct ieee80211_frame *wh;
569 	const struct ieee80211_cipher *cip;
570 	const uint8_t *ivp;
571 	uint8_t keyid;
572 
573 	/* NB: this minimum size data frame could be bigger */
574 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
575 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
576 			"%s: WEP data frame too short, len %u\n",
577 			__func__, m->m_pkthdr.len);
578 		ic->ic_stats.is_rx_tooshort++;	/* XXX need unique stat? */
579 		return NULL;
580 	}
581 
582 	/*
583 	 * Locate the key. If unicast and there is no unicast
584 	 * key then we fall back to the key id in the header.
585 	 * This assumes unicast keys are only configured when
586 	 * the key id in the header is meaningless (typically 0).
587 	 */
588 	wh = mtod(m, struct ieee80211_frame *);
589 	ivp = mtod(m, const uint8_t *) + hdrlen;	/* XXX contig */
590 	keyid = ivp[IEEE80211_WEP_IVLEN];
591 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
592 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)
593 		k = &ic->ic_nw_keys[keyid >> 6];
594 	else
595 		k = &ni->ni_ucastkey;
596 
597 	/*
598 	 * Insure crypto header is contiguous for all decap work.
599 	 */
600 	cip = k->wk_cipher;
601 	if (m->m_len < hdrlen + cip->ic_header &&
602 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
603 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
604 		    "[%6D] unable to pullup %s header\n",
605 		    wh->i_addr2, ":", cip->ic_name);
606 		ic->ic_stats.is_rx_wepfail++;	/* XXX */
607 		return 0;
608 	}
609 
610 	return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
611 #undef IEEE80211_WEP_MINLEN
612 #undef IEEE80211_WEP_HDRLEN
613 }
614