Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.c revision 1.23.2.3
      1 /*	$NetBSD: ieee80211_crypto.c,v 1.23.2.3 2018/07/16 20:11:11 phil Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2001 Atsushi Onoe
      7  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if __FreeBSD__
     33 __FBSDID("$FreeBSD$");
     34 #endif
     35 
     36 /*
     37  * IEEE 802.11 generic crypto support.
     38  */
     39 #include "opt_wlan.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/mbuf.h>
     45 
     46 #include <sys/socket.h>
     47 
     48 #include <net/if.h>
     49 #include <net/if_media.h>
     50 #if __FreeBSD__
     51 #include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
     52 #endif
     53 #if __NetBSD__
     54 #include <net/if_ether.h>
     55 #include <net/route.h>
     56 #endif
     57 
     58 #include <net80211/ieee80211_var.h>
     59 
     60 #ifdef __NetBSD__
     61 #undef  KASSERT
     62 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     63 #endif
     64 
     65 MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
     66 
     67 static	int _ieee80211_crypto_delkey(struct ieee80211vap *,
     68 		struct ieee80211_key *);
     69 
     70 /*
     71  * Table of registered cipher modules.
     72  */
     73 static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
     74 
     75 /*
     76  * Default "null" key management routines.
     77  */
     78 static int
     79 null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
     80 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
     81 {
     82 	if (!(&vap->iv_nw_keys[0] <= k &&
     83 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
     84 		/*
     85 		 * Not in the global key table, the driver should handle this
     86 		 * by allocating a slot in the h/w key table/cache.  In
     87 		 * lieu of that return key slot 0 for any unicast key
     88 		 * request.  We disallow the request if this is a group key.
     89 		 * This default policy does the right thing for legacy hardware
     90 		 * with a 4 key table.  It also handles devices that pass
     91 		 * packets through untouched when marked with the WEP bit
     92 		 * and key index 0.
     93 		 */
     94 		if (k->wk_flags & IEEE80211_KEY_GROUP)
     95 			return 0;
     96 		*keyix = 0;	/* NB: use key index 0 for ucast key */
     97 	} else {
     98 		*keyix = ieee80211_crypto_get_key_wepidx(vap, k);
     99 	}
    100 	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
    101 	return 1;
    102 }
    103 static int
    104 null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
    105 {
    106 	return 1;
    107 }
    108 static 	int
    109 null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
    110 {
    111 	return 1;
    112 }
    113 static void null_key_update(struct ieee80211vap *vap) {}
    114 
    115 /*
    116  * Write-arounds for common operations.
    117  */
    118 static __inline void
    119 cipher_detach(struct ieee80211_key *key)
    120 {
    121 	key->wk_cipher->ic_detach(key);
    122 }
    123 
    124 static __inline void *
    125 cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
    126 {
    127 	return key->wk_cipher->ic_attach(vap, key);
    128 }
    129 
    130 /*
    131  * Wrappers for driver key management methods.
    132  */
    133 static __inline int
    134 dev_key_alloc(struct ieee80211vap *vap,
    135 	struct ieee80211_key *key,
    136 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
    137 {
    138 	return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
    139 }
    140 
    141 static __inline int
    142 dev_key_delete(struct ieee80211vap *vap,
    143 	const struct ieee80211_key *key)
    144 {
    145 	return vap->iv_key_delete(vap, key);
    146 }
    147 
    148 static __inline int
    149 dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
    150 {
    151 	return vap->iv_key_set(vap, key);
    152 }
    153 
    154 /*
    155  * Setup crypto support for a device/shared instance.
    156  */
    157 void
    158 ieee80211_crypto_attach(struct ieee80211com *ic)
    159 {
    160 	/* NB: we assume everything is pre-zero'd */
    161 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
    162 }
    163 
    164 /*
    165  * Teardown crypto support.
    166  */
    167 void
    168 ieee80211_crypto_detach(struct ieee80211com *ic)
    169 {
    170 }
    171 
    172 /*
    173  * Setup crypto support for a vap.
    174  */
    175 void
    176 ieee80211_crypto_vattach(struct ieee80211vap *vap)
    177 {
    178 	int i;
    179 
    180 	/* NB: we assume everything is pre-zero'd */
    181 	vap->iv_max_keyix = IEEE80211_WEP_NKID;
    182 	vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
    183 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    184 		ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
    185 			IEEE80211_KEYIX_NONE);
    186 	/*
    187 	 * Initialize the driver key support routines to noop entries.
    188 	 * This is useful especially for the cipher test modules.
    189 	 */
    190 	vap->iv_key_alloc = null_key_alloc;
    191 	vap->iv_key_set = null_key_set;
    192 	vap->iv_key_delete = null_key_delete;
    193 	vap->iv_key_update_begin = null_key_update;
    194 	vap->iv_key_update_end = null_key_update;
    195 }
    196 
    197 /*
    198  * Teardown crypto support for a vap.
    199  */
    200 void
    201 ieee80211_crypto_vdetach(struct ieee80211vap *vap)
    202 {
    203 	ieee80211_crypto_delglobalkeys(vap);
    204 }
    205 
    206 /*
    207  * Register a crypto cipher module.
    208  */
    209 void
    210 ieee80211_crypto_register(const struct ieee80211_cipher *cip)
    211 {
    212 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    213 		printf("%s: cipher %s has an invalid cipher index %u\n",
    214 			__func__, cip->ic_name, cip->ic_cipher);
    215 		return;
    216 	}
    217 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    218 		printf("%s: cipher %s registered with a different template\n",
    219 			__func__, cip->ic_name);
    220 		return;
    221 	}
    222 	ciphers[cip->ic_cipher] = cip;
    223 }
    224 
    225 /*
    226  * Unregister a crypto cipher module.
    227  */
    228 void
    229 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
    230 {
    231 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    232 		printf("%s: cipher %s has an invalid cipher index %u\n",
    233 			__func__, cip->ic_name, cip->ic_cipher);
    234 		return;
    235 	}
    236 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    237 		printf("%s: cipher %s registered with a different template\n",
    238 			__func__, cip->ic_name);
    239 		return;
    240 	}
    241 	/* NB: don't complain about not being registered */
    242 	/* XXX disallow if references */
    243 	ciphers[cip->ic_cipher] = NULL;
    244 }
    245 
    246 int
    247 ieee80211_crypto_available(u_int cipher)
    248 {
    249 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
    250 }
    251 
    252 #if __FreeBSD__
    253 /* XXX well-known names! */
    254 static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
    255 	[IEEE80211_CIPHER_WEP]	   = "wlan_wep",
    256 	[IEEE80211_CIPHER_TKIP]	   = "wlan_tkip",
    257 	[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
    258 	[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
    259 	[IEEE80211_CIPHER_TKIPMIC] = "#4",	/* NB: reserved */
    260 	[IEEE80211_CIPHER_CKIP]	   = "wlan_ckip",
    261 	[IEEE80211_CIPHER_NONE]	   = "wlan_none",
    262 };
    263 #endif
    264 
    265 /* NB: there must be no overlap between user-supplied and device-owned flags */
    266 CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
    267 
    268 /*
    269  * Establish a relationship between the specified key and cipher
    270  * and, if necessary, allocate a hardware index from the driver.
    271  * Note that when a fixed key index is required it must be specified.
    272  *
    273  * This must be the first call applied to a key; all the other key
    274  * routines assume wk_cipher is setup.
    275  *
    276  * Locking must be handled by the caller using:
    277  *	ieee80211_key_update_begin(vap);
    278  *	ieee80211_key_update_end(vap);
    279  */
    280 int
    281 ieee80211_crypto_newkey(struct ieee80211vap *vap,
    282 	int cipher, int flags, struct ieee80211_key *key)
    283 {
    284 	struct ieee80211com *ic = vap->iv_ic;
    285 	const struct ieee80211_cipher *cip;
    286 	ieee80211_keyix keyix, rxkeyix;
    287 	void *keyctx;
    288 	int oflags;
    289 
    290 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    291 	    "%s: cipher %u flags 0x%x keyix %u\n",
    292 	    __func__, cipher, flags, key->wk_keyix);
    293 
    294 	/*
    295 	 * Validate cipher and set reference to cipher routines.
    296 	 */
    297 	if (cipher >= IEEE80211_CIPHER_MAX) {
    298 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    299 		    "%s: invalid cipher %u\n", __func__, cipher);
    300 		vap->iv_stats.is_crypto_badcipher++;
    301 		return 0;
    302 	}
    303 	cip = ciphers[cipher];
    304 	if (cip == NULL) {
    305 #if __FreeBSD__
    306 		/*
    307 		 * Auto-load cipher module if we have a well-known name
    308 		 * for it.  It might be better to use string names rather
    309 		 * than numbers and craft a module name based on the cipher
    310 		 * name; e.g. wlan_cipher_<cipher-name>.
    311 		 */
    312 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    313 		    "%s: unregistered cipher %u, load module %s\n",
    314 		    __func__, cipher, cipher_modnames[cipher]);
    315 		ieee80211_load_module(cipher_modnames[cipher]);
    316 		/*
    317 		 * If cipher module loaded it should immediately
    318 		 * call ieee80211_crypto_register which will fill
    319 		 * in the entry in the ciphers array.
    320 		 */
    321 		cip = ciphers[cipher];
    322 		if (cip == NULL) {
    323 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    324 			    "%s: unable to load cipher %u, module %s\n",
    325 			    __func__, cipher, cipher_modnames[cipher]);
    326 			vap->iv_stats.is_crypto_nocipher++;
    327 			return 0;
    328 		}
    329 #else
    330 		panic("wlan_cipher not usable.");    /* NNN NetBSD modules? */
    331 #endif
    332 	}
    333 
    334 	oflags = key->wk_flags;
    335 	flags &= IEEE80211_KEY_COMMON;
    336 	/* NB: preserve device attributes */
    337 	flags |= (oflags & IEEE80211_KEY_DEVICE);
    338 	/*
    339 	 * If the hardware does not support the cipher then
    340 	 * fallback to a host-based implementation.
    341 	 */
    342 	if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
    343 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    344 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
    345 		    __func__, cip->ic_name);
    346 		flags |= IEEE80211_KEY_SWCRYPT;
    347 	}
    348 	/*
    349 	 * Hardware TKIP with software MIC is an important
    350 	 * combination; we handle it by flagging each key,
    351 	 * the cipher modules honor it.
    352 	 */
    353 	if (cipher == IEEE80211_CIPHER_TKIP &&
    354 	    (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
    355 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    356 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
    357 		    __func__);
    358 		flags |= IEEE80211_KEY_SWMIC;
    359 	}
    360 
    361 	/*
    362 	 * Bind cipher to key instance.  Note we do this
    363 	 * after checking the device capabilities so the
    364 	 * cipher module can optimize space usage based on
    365 	 * whether or not it needs to do the cipher work.
    366 	 */
    367 	if (key->wk_cipher != cip || key->wk_flags != flags) {
    368 		/*
    369 		 * Fillin the flags so cipher modules can see s/w
    370 		 * crypto requirements and potentially allocate
    371 		 * different state and/or attach different method
    372 		 * pointers.
    373 		 */
    374 		key->wk_flags = flags;
    375 		keyctx = cip->ic_attach(vap, key);
    376 		if (keyctx == NULL) {
    377 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    378 				"%s: unable to attach cipher %s\n",
    379 				__func__, cip->ic_name);
    380 			key->wk_flags = oflags;	/* restore old flags */
    381 			vap->iv_stats.is_crypto_attachfail++;
    382 			return 0;
    383 		}
    384 		cipher_detach(key);
    385 		key->wk_cipher = cip;		/* XXX refcnt? */
    386 		key->wk_private = keyctx;
    387 	}
    388 
    389 	/*
    390 	 * Ask the driver for a key index if we don't have one.
    391 	 * Note that entries in the global key table always have
    392 	 * an index; this means it's safe to call this routine
    393 	 * for these entries just to setup the reference to the
    394 	 * cipher template.  Note also that when using software
    395 	 * crypto we also call the driver to give us a key index.
    396 	 */
    397 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
    398 		if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
    399 			/*
    400 			 * Unable to setup driver state.
    401 			 */
    402 			vap->iv_stats.is_crypto_keyfail++;
    403 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    404 			    "%s: unable to setup cipher %s\n",
    405 			    __func__, cip->ic_name);
    406 			return 0;
    407 		}
    408 		if (key->wk_flags != flags) {
    409 			/*
    410 			 * Driver overrode flags we setup; typically because
    411 			 * resources were unavailable to handle _this_ key.
    412 			 * Re-attach the cipher context to allow cipher
    413 			 * modules to handle differing requirements.
    414 			 */
    415 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    416 			    "%s: driver override for cipher %s, flags "
    417 			    "0x%x -> 0x%x\n", __func__, cip->ic_name,
    418 			    oflags, key->wk_flags);
    419 			keyctx = cip->ic_attach(vap, key);
    420 			if (keyctx == NULL) {
    421 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    422 				    "%s: unable to attach cipher %s with "
    423 				    "flags 0x%x\n", __func__, cip->ic_name,
    424 				    key->wk_flags);
    425 				key->wk_flags = oflags;	/* restore old flags */
    426 				vap->iv_stats.is_crypto_attachfail++;
    427 				return 0;
    428 			}
    429 			cipher_detach(key);
    430 			key->wk_cipher = cip;		/* XXX refcnt? */
    431 			key->wk_private = keyctx;
    432 		}
    433 		key->wk_keyix = keyix;
    434 		key->wk_rxkeyix = rxkeyix;
    435 		key->wk_flags |= IEEE80211_KEY_DEVKEY;
    436 	}
    437 	return 1;
    438 }
    439 
    440 /*
    441  * Remove the key (no locking, for internal use).
    442  */
    443 static int
    444 _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    445 {
    446 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
    447 
    448 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    449 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
    450 	    __func__, key->wk_cipher->ic_name,
    451 	    key->wk_keyix, key->wk_flags,
    452 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
    453 	    key->wk_keylen);
    454 
    455 	if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
    456 		/*
    457 		 * Remove hardware entry.
    458 		 */
    459 		/* XXX key cache */
    460 		if (!dev_key_delete(vap, key)) {
    461 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    462 			    "%s: driver did not delete key index %u\n",
    463 			    __func__, key->wk_keyix);
    464 			vap->iv_stats.is_crypto_delkey++;
    465 			/* XXX recovery? */
    466 		}
    467 	}
    468 	cipher_detach(key);
    469 	memset(key, 0, sizeof(*key));
    470 	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
    471 	return 1;
    472 }
    473 
    474 /*
    475  * Remove the specified key.
    476  */
    477 int
    478 ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    479 {
    480 	int status;
    481 
    482 	ieee80211_key_update_begin(vap);
    483 	status = _ieee80211_crypto_delkey(vap, key);
    484 	ieee80211_key_update_end(vap);
    485 	return status;
    486 }
    487 
    488 /*
    489  * Clear the global key table.
    490  */
    491 void
    492 ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
    493 {
    494 	int i;
    495 
    496 	ieee80211_key_update_begin(vap);
    497 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    498 		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
    499 	ieee80211_key_update_end(vap);
    500 }
    501 
    502 /*
    503  * Set the contents of the specified key.
    504  *
    505  * Locking must be handled by the caller using:
    506  *	ieee80211_key_update_begin(vap);
    507  *	ieee80211_key_update_end(vap);
    508  */
    509 int
    510 ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    511 {
    512 	const struct ieee80211_cipher *cip = key->wk_cipher;
    513 
    514 	KASSERT(cip != NULL, ("No cipher!"));
    515 
    516 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    517 	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
    518 	    __func__, cip->ic_name, key->wk_keyix,
    519 	    key->wk_flags, ether_sprintf(key->wk_macaddr),
    520 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
    521 	    key->wk_keylen);
    522 
    523 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY)  == 0) {
    524 		/* XXX nothing allocated, should not happen */
    525 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    526 		    "%s: no device key setup done; should not happen!\n",
    527 		    __func__);
    528 		vap->iv_stats.is_crypto_setkey_nokey++;
    529 		return 0;
    530 	}
    531 	/*
    532 	 * Give cipher a chance to validate key contents.
    533 	 * XXX should happen before modifying state.
    534 	 */
    535 	if (!cip->ic_setkey(key)) {
    536 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    537 		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
    538 		    __func__, cip->ic_name, key->wk_keyix,
    539 		    key->wk_keylen, key->wk_flags);
    540 		vap->iv_stats.is_crypto_setkey_cipher++;
    541 		return 0;
    542 	}
    543 	return dev_key_set(vap, key);
    544 }
    545 
    546 /*
    547  * Return index if the key is a WEP key (0..3); -1 otherwise.
    548  *
    549  * This is different to "get_keyid" which defaults to returning
    550  * 0 for unicast keys; it assumes that it won't be used for WEP.
    551  */
    552 int
    553 ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
    554     const struct ieee80211_key *k)
    555 {
    556 
    557 	if (k >= &vap->iv_nw_keys[0] &&
    558 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
    559 		return (k - vap->iv_nw_keys);
    560 	return (-1);
    561 }
    562 
    563 /*
    564  * Note: only supports a single unicast key (0).
    565  */
    566 uint8_t
    567 ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
    568 {
    569 	if (k >= &vap->iv_nw_keys[0] &&
    570 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
    571 		return (k - vap->iv_nw_keys);
    572 	else
    573 		return (0);
    574 }
    575 
    576 struct ieee80211_key *
    577 ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
    578 {
    579 	struct ieee80211vap *vap = ni->ni_vap;
    580 	struct ieee80211_frame *wh;
    581 
    582 	/*
    583 	 * Multicast traffic always uses the multicast key.
    584 	 * Otherwise if a unicast key is set we use that and
    585 	 * it is always key index 0.  When no unicast key is
    586 	 * set we fall back to the default transmit key.
    587 	 */
    588 	wh = mtod(m, struct ieee80211_frame *);
    589 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    590 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
    591 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
    592 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
    593 			    wh->i_addr1,
    594 			    "no default transmit key (%s) deftxkey %u",
    595 			    __func__, vap->iv_def_txkey);
    596 			vap->iv_stats.is_tx_nodefkey++;
    597 			return NULL;
    598 		}
    599 		return &vap->iv_nw_keys[vap->iv_def_txkey];
    600 	}
    601 
    602 	return &ni->ni_ucastkey;
    603 }
    604 
    605 /*
    606  * Add privacy headers appropriate for the specified key.
    607  */
    608 struct ieee80211_key *
    609 ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
    610 {
    611 	struct ieee80211_key *k;
    612 	const struct ieee80211_cipher *cip;
    613 
    614 	if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
    615 		cip = k->wk_cipher;
    616 		return (cip->ic_encap(k, m) ? k : NULL);
    617 	}
    618 
    619 	return NULL;
    620 }
    621 
    622 /*
    623  * Validate and strip privacy headers (and trailer) for a
    624  * received frame that has the WEP/Privacy bit set.
    625  */
    626 int
    627 ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
    628     struct ieee80211_key **key)
    629 {
    630 #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
    631 #define	IEEE80211_WEP_MINLEN \
    632 	(sizeof(struct ieee80211_frame) + \
    633 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
    634 	struct ieee80211vap *vap = ni->ni_vap;
    635 	struct ieee80211_key *k;
    636 	struct ieee80211_frame *wh;
    637 	const struct ieee80211_rx_stats *rxs;
    638 	const struct ieee80211_cipher *cip;
    639 	uint8_t keyid;
    640 
    641 	/*
    642 	 * Check for hardware decryption and IV stripping.
    643 	 * If the IV is stripped then we definitely can't find a key.
    644 	 * Set the key to NULL but return true; upper layers
    645 	 * will need to handle a NULL key for a successful
    646 	 * decrypt.
    647 	 */
    648 	rxs = ieee80211_get_rx_params_ptr(m);
    649 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
    650 		if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
    651 			/*
    652 			 * Hardware decrypted, IV stripped.
    653 			 * We can't find a key with a stripped IV.
    654 			 * Return successful.
    655 			 */
    656 			*key = NULL;
    657 			return (1);
    658 		}
    659 	}
    660 
    661 	/* NB: this minimum size data frame could be bigger */
    662 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
    663 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
    664 			"%s: WEP data frame too short, len %u\n",
    665 			__func__, m->m_pkthdr.len);
    666 		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */
    667 		*key = NULL;
    668 		return (0);
    669 	}
    670 
    671 	/*
    672 	 * Locate the key. If unicast and there is no unicast
    673 	 * key then we fall back to the key id in the header.
    674 	 * This assumes unicast keys are only configured when
    675 	 * the key id in the header is meaningless (typically 0).
    676 	 */
    677 	wh = mtod(m, struct ieee80211_frame *);
    678 	m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
    679 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    680 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
    681 		k = &vap->iv_nw_keys[keyid >> 6];
    682 	else
    683 		k = &ni->ni_ucastkey;
    684 
    685 	/*
    686 	 * Insure crypto header is contiguous for all decap work.
    687 	 */
    688 	cip = k->wk_cipher;
    689 	if (m->m_len < hdrlen + cip->ic_header &&
    690 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
    691 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
    692 		    "unable to pullup %s header", cip->ic_name);
    693 		vap->iv_stats.is_rx_wepfail++;	/* XXX */
    694 		*key = NULL;
    695 		return (0);
    696 	}
    697 
    698 	/*
    699 	 * Attempt decryption.
    700 	 *
    701 	 * If we fail then don't return the key - return NULL
    702 	 * and an error.
    703 	 */
    704 	if (cip->ic_decap(k, m, hdrlen)) {
    705 		/* success */
    706 		*key = k;
    707 		return (1);
    708 	}
    709 
    710 	/* Failure */
    711 	*key = NULL;
    712 	return (0);
    713 #undef IEEE80211_WEP_MINLEN
    714 #undef IEEE80211_WEP_HDRLEN
    715 }
    716 
    717 /*
    718  * Check and remove any MIC.
    719  */
    720 int
    721 ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
    722     struct mbuf *m, int force)
    723 {
    724 	const struct ieee80211_cipher *cip;
    725 	const struct ieee80211_rx_stats *rxs;
    726 	struct ieee80211_frame *wh;
    727 
    728 	rxs = ieee80211_get_rx_params_ptr(m);
    729 	wh = mtod(m, struct ieee80211_frame *);
    730 
    731 	/*
    732 	 * Handle demic / mic errors from hardware-decrypted offload devices.
    733 	 */
    734 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
    735 		if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) {
    736 			/*
    737 			 * Hardware has said MIC failed.  We don't care about
    738 			 * whether it was stripped or not.
    739 			 *
    740 			 * Eventually - teach the demic methods in crypto
    741 			 * modules to handle a NULL key and not to dereference
    742 			 * it.
    743 			 */
    744 			ieee80211_notify_michael_failure(vap, wh, -1);
    745 			return (0);
    746 		}
    747 
    748 		if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) {
    749 			/*
    750 			 * Hardware has decrypted and not indicated a
    751 			 * MIC failure and has stripped the MIC.
    752 			 * We may not have a key, so for now just
    753 			 * return OK.
    754 			 */
    755 			return (1);
    756 		}
    757 	}
    758 
    759 	/*
    760 	 * If we don't have a key at this point then we don't
    761 	 * have to demic anything.
    762 	 */
    763 	if (k == NULL)
    764 		return (1);
    765 
    766 	cip = k->wk_cipher;
    767 	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
    768 }
    769 
    770 
    771 static void
    772 load_ucastkey(void *arg, struct ieee80211_node *ni)
    773 {
    774 	struct ieee80211vap *vap = ni->ni_vap;
    775 	struct ieee80211_key *k;
    776 
    777 	if (vap->iv_state != IEEE80211_S_RUN)
    778 		return;
    779 	k = &ni->ni_ucastkey;
    780 	if (k->wk_flags & IEEE80211_KEY_DEVKEY)
    781 		dev_key_set(vap, k);
    782 }
    783 
    784 /*
    785  * Re-load all keys known to the 802.11 layer that may
    786  * have hardware state backing them.  This is used by
    787  * drivers on resume to push keys down into the device.
    788  */
    789 void
    790 ieee80211_crypto_reload_keys(struct ieee80211com *ic)
    791 {
    792 	struct ieee80211vap *vap;
    793 	int i;
    794 
    795 	/*
    796 	 * Keys in the global key table of each vap.
    797 	 */
    798 	/* NB: used only during resume so don't lock for now */
    799 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    800 		if (vap->iv_state != IEEE80211_S_RUN)
    801 			continue;
    802 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    803 			const struct ieee80211_key *k = &vap->iv_nw_keys[i];
    804 			if (k->wk_flags & IEEE80211_KEY_DEVKEY)
    805 				dev_key_set(vap, k);
    806 		}
    807 	}
    808 	/*
    809 	 * Unicast keys.
    810 	 */
    811 	ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
    812 }
    813 
    814 /*
    815  * Set the default key index for WEP, or KEYIX_NONE for no default TX key.
    816  *
    817  * This should be done as part of a key update block (iv_key_update_begin /
    818  * iv_key_update_end.)
    819  */
    820 void
    821 ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
    822 {
    823 
    824 	/* XXX TODO: assert we're in a key update block */
    825 
    826 	vap->iv_update_deftxkey(vap, kid);
    827 }
    828