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