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