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