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