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