xref: /freebsd/sys/net80211/ieee80211_crypto.c (revision 4a19d712383190c461ebfba2d9b97f756ba9b5d1)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
308a1b9b6aSSam Leffler /*
318a1b9b6aSSam Leffler  * IEEE 802.11 generic crypto support.
328a1b9b6aSSam Leffler  */
33b032f27cSSam Leffler #include "opt_wlan.h"
34b032f27cSSam Leffler 
351a1e1d21SSam Leffler #include <sys/param.h>
36b032f27cSSam Leffler #include <sys/kernel.h>
37b032f27cSSam Leffler #include <sys/malloc.h>
381a1e1d21SSam Leffler #include <sys/mbuf.h>
391a1e1d21SSam Leffler 
408a1b9b6aSSam Leffler #include <sys/socket.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net/if.h>
431a1e1d21SSam Leffler #include <net/if_media.h>
448a1b9b6aSSam Leffler #include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
451a1e1d21SSam Leffler 
461a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
471a1e1d21SSam Leffler 
48b032f27cSSam Leffler MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
49b032f27cSSam Leffler 
50b032f27cSSam Leffler static	int _ieee80211_crypto_delkey(struct ieee80211vap *,
51b032f27cSSam Leffler 		struct ieee80211_key *);
52b032f27cSSam Leffler 
538a1b9b6aSSam Leffler /*
548a1b9b6aSSam Leffler  * Table of registered cipher modules.
558a1b9b6aSSam Leffler  */
568a1b9b6aSSam Leffler static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
571a1e1d21SSam Leffler 
588a1b9b6aSSam Leffler /*
598a1b9b6aSSam Leffler  * Default "null" key management routines.
608a1b9b6aSSam Leffler  */
618a1b9b6aSSam Leffler static int
62e6e547d5SSam Leffler null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
63c1225b52SSam Leffler 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
641a1e1d21SSam Leffler {
65b032f27cSSam Leffler 	if (!(&vap->iv_nw_keys[0] <= k &&
66b032f27cSSam Leffler 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
675c8a7b1bSSam Leffler 		/*
685c8a7b1bSSam Leffler 		 * Not in the global key table, the driver should handle this
695c8a7b1bSSam Leffler 		 * by allocating a slot in the h/w key table/cache.  In
705c8a7b1bSSam Leffler 		 * lieu of that return key slot 0 for any unicast key
715c8a7b1bSSam Leffler 		 * request.  We disallow the request if this is a group key.
725c8a7b1bSSam Leffler 		 * This default policy does the right thing for legacy hardware
735c8a7b1bSSam Leffler 		 * with a 4 key table.  It also handles devices that pass
745c8a7b1bSSam Leffler 		 * packets through untouched when marked with the WEP bit
755c8a7b1bSSam Leffler 		 * and key index 0.
765c8a7b1bSSam Leffler 		 */
77c1225b52SSam Leffler 		if (k->wk_flags & IEEE80211_KEY_GROUP)
78c1225b52SSam Leffler 			return 0;
79c1225b52SSam Leffler 		*keyix = 0;	/* NB: use key index 0 for ucast key */
80c1225b52SSam Leffler 	} else {
81*4a19d712SAndriy Voskoboinyk 		*keyix = ieee80211_crypto_get_key_wepidx(vap, k);
828a1b9b6aSSam Leffler 	}
83c1225b52SSam Leffler 	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
84c1225b52SSam Leffler 	return 1;
855c8a7b1bSSam Leffler }
868a1b9b6aSSam Leffler static int
87b032f27cSSam Leffler null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
888a1b9b6aSSam Leffler {
898a1b9b6aSSam Leffler 	return 1;
908a1b9b6aSSam Leffler }
918a1b9b6aSSam Leffler static 	int
92bc813c40SAdrian Chadd null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
938a1b9b6aSSam Leffler {
948a1b9b6aSSam Leffler 	return 1;
958a1b9b6aSSam Leffler }
96b032f27cSSam Leffler static void null_key_update(struct ieee80211vap *vap) {}
978a1b9b6aSSam Leffler 
988a1b9b6aSSam Leffler /*
998a1b9b6aSSam Leffler  * Write-arounds for common operations.
1008a1b9b6aSSam Leffler  */
1018a1b9b6aSSam Leffler static __inline void
1028a1b9b6aSSam Leffler cipher_detach(struct ieee80211_key *key)
1038a1b9b6aSSam Leffler {
1048a1b9b6aSSam Leffler 	key->wk_cipher->ic_detach(key);
1058a1b9b6aSSam Leffler }
1068a1b9b6aSSam Leffler 
1078a1b9b6aSSam Leffler static __inline void *
108b032f27cSSam Leffler cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
1098a1b9b6aSSam Leffler {
110b032f27cSSam Leffler 	return key->wk_cipher->ic_attach(vap, key);
1118a1b9b6aSSam Leffler }
1128a1b9b6aSSam Leffler 
1138a1b9b6aSSam Leffler /*
1148a1b9b6aSSam Leffler  * Wrappers for driver key management methods.
1158a1b9b6aSSam Leffler  */
1168a1b9b6aSSam Leffler static __inline int
117b032f27cSSam Leffler dev_key_alloc(struct ieee80211vap *vap,
118e6e547d5SSam Leffler 	struct ieee80211_key *key,
119c1225b52SSam Leffler 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
1208a1b9b6aSSam Leffler {
121b032f27cSSam Leffler 	return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
1228a1b9b6aSSam Leffler }
1238a1b9b6aSSam Leffler 
1248a1b9b6aSSam Leffler static __inline int
125b032f27cSSam Leffler dev_key_delete(struct ieee80211vap *vap,
1268a1b9b6aSSam Leffler 	const struct ieee80211_key *key)
1278a1b9b6aSSam Leffler {
128b032f27cSSam Leffler 	return vap->iv_key_delete(vap, key);
1298a1b9b6aSSam Leffler }
1308a1b9b6aSSam Leffler 
1318a1b9b6aSSam Leffler static __inline int
13271fe06caSSam Leffler dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
1338a1b9b6aSSam Leffler {
134bc813c40SAdrian Chadd 	return vap->iv_key_set(vap, key);
1358a1b9b6aSSam Leffler }
1361a1e1d21SSam Leffler 
1371a1e1d21SSam Leffler /*
138b032f27cSSam Leffler  * Setup crypto support for a device/shared instance.
1391a1e1d21SSam Leffler  */
1401a1e1d21SSam Leffler void
1418a1b9b6aSSam Leffler ieee80211_crypto_attach(struct ieee80211com *ic)
1421a1e1d21SSam Leffler {
1438a1b9b6aSSam Leffler 	/* NB: we assume everything is pre-zero'd */
1448a1b9b6aSSam Leffler 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
1451a1e1d21SSam Leffler }
1461a1e1d21SSam Leffler 
1478a1b9b6aSSam Leffler /*
1488a1b9b6aSSam Leffler  * Teardown crypto support.
1498a1b9b6aSSam Leffler  */
1508a1b9b6aSSam Leffler void
1518a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic)
1528a1b9b6aSSam Leffler {
153b032f27cSSam Leffler }
154b032f27cSSam Leffler 
155b032f27cSSam Leffler /*
156b032f27cSSam Leffler  * Setup crypto support for a vap.
157b032f27cSSam Leffler  */
158b032f27cSSam Leffler void
159b032f27cSSam Leffler ieee80211_crypto_vattach(struct ieee80211vap *vap)
160b032f27cSSam Leffler {
161b032f27cSSam Leffler 	int i;
162b032f27cSSam Leffler 
163b032f27cSSam Leffler 	/* NB: we assume everything is pre-zero'd */
164b032f27cSSam Leffler 	vap->iv_max_keyix = IEEE80211_WEP_NKID;
165b032f27cSSam Leffler 	vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
166b032f27cSSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
167b032f27cSSam Leffler 		ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
168b032f27cSSam Leffler 			IEEE80211_KEYIX_NONE);
169b032f27cSSam Leffler 	/*
170b032f27cSSam Leffler 	 * Initialize the driver key support routines to noop entries.
171b032f27cSSam Leffler 	 * This is useful especially for the cipher test modules.
172b032f27cSSam Leffler 	 */
173b032f27cSSam Leffler 	vap->iv_key_alloc = null_key_alloc;
174b032f27cSSam Leffler 	vap->iv_key_set = null_key_set;
175b032f27cSSam Leffler 	vap->iv_key_delete = null_key_delete;
176b032f27cSSam Leffler 	vap->iv_key_update_begin = null_key_update;
177b032f27cSSam Leffler 	vap->iv_key_update_end = null_key_update;
178b032f27cSSam Leffler }
179b032f27cSSam Leffler 
180b032f27cSSam Leffler /*
181b032f27cSSam Leffler  * Teardown crypto support for a vap.
182b032f27cSSam Leffler  */
183b032f27cSSam Leffler void
184b032f27cSSam Leffler ieee80211_crypto_vdetach(struct ieee80211vap *vap)
185b032f27cSSam Leffler {
186b032f27cSSam Leffler 	ieee80211_crypto_delglobalkeys(vap);
1871a1e1d21SSam Leffler }
1881a1e1d21SSam Leffler 
1898a1b9b6aSSam Leffler /*
1908a1b9b6aSSam Leffler  * Register a crypto cipher module.
1918a1b9b6aSSam Leffler  */
1928a1b9b6aSSam Leffler void
1938a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip)
1948a1b9b6aSSam Leffler {
1958a1b9b6aSSam Leffler 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
1968a1b9b6aSSam Leffler 		printf("%s: cipher %s has an invalid cipher index %u\n",
1978a1b9b6aSSam Leffler 			__func__, cip->ic_name, cip->ic_cipher);
1988a1b9b6aSSam Leffler 		return;
1998a1b9b6aSSam Leffler 	}
2008a1b9b6aSSam Leffler 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
2018a1b9b6aSSam Leffler 		printf("%s: cipher %s registered with a different template\n",
2028a1b9b6aSSam Leffler 			__func__, cip->ic_name);
2038a1b9b6aSSam Leffler 		return;
2048a1b9b6aSSam Leffler 	}
2058a1b9b6aSSam Leffler 	ciphers[cip->ic_cipher] = cip;
2068a1b9b6aSSam Leffler }
2078a1b9b6aSSam Leffler 
2088a1b9b6aSSam Leffler /*
2098a1b9b6aSSam Leffler  * Unregister a crypto cipher module.
2108a1b9b6aSSam Leffler  */
2118a1b9b6aSSam Leffler void
2128a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
2138a1b9b6aSSam Leffler {
2148a1b9b6aSSam Leffler 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
2158a1b9b6aSSam Leffler 		printf("%s: cipher %s has an invalid cipher index %u\n",
2168a1b9b6aSSam Leffler 			__func__, cip->ic_name, cip->ic_cipher);
2178a1b9b6aSSam Leffler 		return;
2188a1b9b6aSSam Leffler 	}
2198a1b9b6aSSam Leffler 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
2208a1b9b6aSSam Leffler 		printf("%s: cipher %s registered with a different template\n",
2218a1b9b6aSSam Leffler 			__func__, cip->ic_name);
2228a1b9b6aSSam Leffler 		return;
2238a1b9b6aSSam Leffler 	}
2248a1b9b6aSSam Leffler 	/* NB: don't complain about not being registered */
2258a1b9b6aSSam Leffler 	/* XXX disallow if references */
2268a1b9b6aSSam Leffler 	ciphers[cip->ic_cipher] = NULL;
2278a1b9b6aSSam Leffler }
2288a1b9b6aSSam Leffler 
2298a1b9b6aSSam Leffler int
2308a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher)
2318a1b9b6aSSam Leffler {
2328a1b9b6aSSam Leffler 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
2338a1b9b6aSSam Leffler }
2348a1b9b6aSSam Leffler 
2358a1b9b6aSSam Leffler /* XXX well-known names! */
236b032f27cSSam Leffler static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
2379fcf5318SSam Leffler 	[IEEE80211_CIPHER_WEP]	   = "wlan_wep",
2389fcf5318SSam Leffler 	[IEEE80211_CIPHER_TKIP]	   = "wlan_tkip",
2399fcf5318SSam Leffler 	[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
2409fcf5318SSam Leffler 	[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
2419fcf5318SSam Leffler 	[IEEE80211_CIPHER_TKIPMIC] = "#4",	/* NB: reserved */
2429fcf5318SSam Leffler 	[IEEE80211_CIPHER_CKIP]	   = "wlan_ckip",
2439fcf5318SSam Leffler 	[IEEE80211_CIPHER_NONE]	   = "wlan_none",
2448a1b9b6aSSam Leffler };
2458a1b9b6aSSam Leffler 
246411ccf5fSSam Leffler /* NB: there must be no overlap between user-supplied and device-owned flags */
247411ccf5fSSam Leffler CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
248411ccf5fSSam Leffler 
2498a1b9b6aSSam Leffler /*
2508a1b9b6aSSam Leffler  * Establish a relationship between the specified key and cipher
251dd70e17bSSam Leffler  * and, if necessary, allocate a hardware index from the driver.
252411ccf5fSSam Leffler  * Note that when a fixed key index is required it must be specified.
2538a1b9b6aSSam Leffler  *
2548a1b9b6aSSam Leffler  * This must be the first call applied to a key; all the other key
2558a1b9b6aSSam Leffler  * routines assume wk_cipher is setup.
2568a1b9b6aSSam Leffler  *
2578a1b9b6aSSam Leffler  * Locking must be handled by the caller using:
258b032f27cSSam Leffler  *	ieee80211_key_update_begin(vap);
259b032f27cSSam Leffler  *	ieee80211_key_update_end(vap);
2608a1b9b6aSSam Leffler  */
2618a1b9b6aSSam Leffler int
262b032f27cSSam Leffler ieee80211_crypto_newkey(struct ieee80211vap *vap,
263dd70e17bSSam Leffler 	int cipher, int flags, struct ieee80211_key *key)
2648a1b9b6aSSam Leffler {
265b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2668a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
267c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
2688a1b9b6aSSam Leffler 	void *keyctx;
2698a1b9b6aSSam Leffler 	int oflags;
2708a1b9b6aSSam Leffler 
27107760642SSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
27207760642SSam Leffler 	    "%s: cipher %u flags 0x%x keyix %u\n",
27307760642SSam Leffler 	    __func__, cipher, flags, key->wk_keyix);
27407760642SSam Leffler 
2758a1b9b6aSSam Leffler 	/*
2768a1b9b6aSSam Leffler 	 * Validate cipher and set reference to cipher routines.
2778a1b9b6aSSam Leffler 	 */
2788a1b9b6aSSam Leffler 	if (cipher >= IEEE80211_CIPHER_MAX) {
279b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
2808a1b9b6aSSam Leffler 		    "%s: invalid cipher %u\n", __func__, cipher);
281b032f27cSSam Leffler 		vap->iv_stats.is_crypto_badcipher++;
2828a1b9b6aSSam Leffler 		return 0;
2838a1b9b6aSSam Leffler 	}
2848a1b9b6aSSam Leffler 	cip = ciphers[cipher];
2858a1b9b6aSSam Leffler 	if (cip == NULL) {
2868a1b9b6aSSam Leffler 		/*
2878a1b9b6aSSam Leffler 		 * Auto-load cipher module if we have a well-known name
2888a1b9b6aSSam Leffler 		 * for it.  It might be better to use string names rather
2898a1b9b6aSSam Leffler 		 * than numbers and craft a module name based on the cipher
2908a1b9b6aSSam Leffler 		 * name; e.g. wlan_cipher_<cipher-name>.
2918a1b9b6aSSam Leffler 		 */
292b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
2938a1b9b6aSSam Leffler 		    "%s: unregistered cipher %u, load module %s\n",
2948a1b9b6aSSam Leffler 		    __func__, cipher, cipher_modnames[cipher]);
2958a1b9b6aSSam Leffler 		ieee80211_load_module(cipher_modnames[cipher]);
2968a1b9b6aSSam Leffler 		/*
2978a1b9b6aSSam Leffler 		 * If cipher module loaded it should immediately
2988a1b9b6aSSam Leffler 		 * call ieee80211_crypto_register which will fill
2998a1b9b6aSSam Leffler 		 * in the entry in the ciphers array.
3008a1b9b6aSSam Leffler 		 */
3018a1b9b6aSSam Leffler 		cip = ciphers[cipher];
3028a1b9b6aSSam Leffler 		if (cip == NULL) {
303b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3048a1b9b6aSSam Leffler 			    "%s: unable to load cipher %u, module %s\n",
305b032f27cSSam Leffler 			    __func__, cipher, cipher_modnames[cipher]);
306b032f27cSSam Leffler 			vap->iv_stats.is_crypto_nocipher++;
3078a1b9b6aSSam Leffler 			return 0;
3088a1b9b6aSSam Leffler 		}
3098a1b9b6aSSam Leffler 	}
3108a1b9b6aSSam Leffler 
3118a1b9b6aSSam Leffler 	oflags = key->wk_flags;
312dd70e17bSSam Leffler 	flags &= IEEE80211_KEY_COMMON;
313411ccf5fSSam Leffler 	/* NB: preserve device attributes */
314411ccf5fSSam Leffler 	flags |= (oflags & IEEE80211_KEY_DEVICE);
3158a1b9b6aSSam Leffler 	/*
3168a1b9b6aSSam Leffler 	 * If the hardware does not support the cipher then
3178a1b9b6aSSam Leffler 	 * fallback to a host-based implementation.
3188a1b9b6aSSam Leffler 	 */
319b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
320b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3218a1b9b6aSSam Leffler 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
3228a1b9b6aSSam Leffler 		    __func__, cip->ic_name);
323dd70e17bSSam Leffler 		flags |= IEEE80211_KEY_SWCRYPT;
3248a1b9b6aSSam Leffler 	}
3258a1b9b6aSSam Leffler 	/*
3268a1b9b6aSSam Leffler 	 * Hardware TKIP with software MIC is an important
3278a1b9b6aSSam Leffler 	 * combination; we handle it by flagging each key,
3288a1b9b6aSSam Leffler 	 * the cipher modules honor it.
3298a1b9b6aSSam Leffler 	 */
3308a1b9b6aSSam Leffler 	if (cipher == IEEE80211_CIPHER_TKIP &&
331b032f27cSSam Leffler 	    (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
332b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3338a1b9b6aSSam Leffler 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
3348a1b9b6aSSam Leffler 		    __func__);
335dd70e17bSSam Leffler 		flags |= IEEE80211_KEY_SWMIC;
3368a1b9b6aSSam Leffler 	}
3378a1b9b6aSSam Leffler 
3388a1b9b6aSSam Leffler 	/*
3398a1b9b6aSSam Leffler 	 * Bind cipher to key instance.  Note we do this
3408a1b9b6aSSam Leffler 	 * after checking the device capabilities so the
3418a1b9b6aSSam Leffler 	 * cipher module can optimize space usage based on
3428a1b9b6aSSam Leffler 	 * whether or not it needs to do the cipher work.
3438a1b9b6aSSam Leffler 	 */
344dd70e17bSSam Leffler 	if (key->wk_cipher != cip || key->wk_flags != flags) {
345dd70e17bSSam Leffler 		/*
346dd70e17bSSam Leffler 		 * Fillin the flags so cipher modules can see s/w
347dd70e17bSSam Leffler 		 * crypto requirements and potentially allocate
348dd70e17bSSam Leffler 		 * different state and/or attach different method
349dd70e17bSSam Leffler 		 * pointers.
350dd70e17bSSam Leffler 		 */
351dd70e17bSSam Leffler 		key->wk_flags = flags;
352b032f27cSSam Leffler 		keyctx = cip->ic_attach(vap, key);
3538a1b9b6aSSam Leffler 		if (keyctx == NULL) {
354b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3558a1b9b6aSSam Leffler 				"%s: unable to attach cipher %s\n",
3568a1b9b6aSSam Leffler 				__func__, cip->ic_name);
3578a1b9b6aSSam Leffler 			key->wk_flags = oflags;	/* restore old flags */
358b032f27cSSam Leffler 			vap->iv_stats.is_crypto_attachfail++;
3598a1b9b6aSSam Leffler 			return 0;
3608a1b9b6aSSam Leffler 		}
3618a1b9b6aSSam Leffler 		cipher_detach(key);
3628a1b9b6aSSam Leffler 		key->wk_cipher = cip;		/* XXX refcnt? */
3638a1b9b6aSSam Leffler 		key->wk_private = keyctx;
3648a1b9b6aSSam Leffler 	}
3658a1b9b6aSSam Leffler 
3668a1b9b6aSSam Leffler 	/*
3678a1b9b6aSSam Leffler 	 * Ask the driver for a key index if we don't have one.
3688a1b9b6aSSam Leffler 	 * Note that entries in the global key table always have
3698a1b9b6aSSam Leffler 	 * an index; this means it's safe to call this routine
3708a1b9b6aSSam Leffler 	 * for these entries just to setup the reference to the
3718a1b9b6aSSam Leffler 	 * cipher template.  Note also that when using software
3728a1b9b6aSSam Leffler 	 * crypto we also call the driver to give us a key index.
3738a1b9b6aSSam Leffler 	 */
374e6e547d5SSam Leffler 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
375b032f27cSSam Leffler 		if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
3768a1b9b6aSSam Leffler 			/*
377e6e547d5SSam Leffler 			 * Unable to setup driver state.
3788a1b9b6aSSam Leffler 			 */
379b032f27cSSam Leffler 			vap->iv_stats.is_crypto_keyfail++;
380b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3818a1b9b6aSSam Leffler 			    "%s: unable to setup cipher %s\n",
3828a1b9b6aSSam Leffler 			    __func__, cip->ic_name);
3838a1b9b6aSSam Leffler 			return 0;
3848a1b9b6aSSam Leffler 		}
385e6e547d5SSam Leffler 		if (key->wk_flags != flags) {
386e6e547d5SSam Leffler 			/*
387e6e547d5SSam Leffler 			 * Driver overrode flags we setup; typically because
388e6e547d5SSam Leffler 			 * resources were unavailable to handle _this_ key.
389e6e547d5SSam Leffler 			 * Re-attach the cipher context to allow cipher
390e6e547d5SSam Leffler 			 * modules to handle differing requirements.
391e6e547d5SSam Leffler 			 */
392e6e547d5SSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
393e6e547d5SSam Leffler 			    "%s: driver override for cipher %s, flags "
394e6e547d5SSam Leffler 			    "0x%x -> 0x%x\n", __func__, cip->ic_name,
395e6e547d5SSam Leffler 			    oflags, key->wk_flags);
396e6e547d5SSam Leffler 			keyctx = cip->ic_attach(vap, key);
397e6e547d5SSam Leffler 			if (keyctx == NULL) {
398e6e547d5SSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
399e6e547d5SSam Leffler 				    "%s: unable to attach cipher %s with "
400e6e547d5SSam Leffler 				    "flags 0x%x\n", __func__, cip->ic_name,
401e6e547d5SSam Leffler 				    key->wk_flags);
402e6e547d5SSam Leffler 				key->wk_flags = oflags;	/* restore old flags */
403e6e547d5SSam Leffler 				vap->iv_stats.is_crypto_attachfail++;
404e6e547d5SSam Leffler 				return 0;
405e6e547d5SSam Leffler 			}
406e6e547d5SSam Leffler 			cipher_detach(key);
407e6e547d5SSam Leffler 			key->wk_cipher = cip;		/* XXX refcnt? */
408e6e547d5SSam Leffler 			key->wk_private = keyctx;
409e6e547d5SSam Leffler 		}
410c1225b52SSam Leffler 		key->wk_keyix = keyix;
411c1225b52SSam Leffler 		key->wk_rxkeyix = rxkeyix;
412e6e547d5SSam Leffler 		key->wk_flags |= IEEE80211_KEY_DEVKEY;
4138a1b9b6aSSam Leffler 	}
4148a1b9b6aSSam Leffler 	return 1;
4158a1b9b6aSSam Leffler }
4168a1b9b6aSSam Leffler 
4178a1b9b6aSSam Leffler /*
4188a1b9b6aSSam Leffler  * Remove the key (no locking, for internal use).
4198a1b9b6aSSam Leffler  */
4208a1b9b6aSSam Leffler static int
421b032f27cSSam Leffler _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
4228a1b9b6aSSam Leffler {
4238a1b9b6aSSam Leffler 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
4248a1b9b6aSSam Leffler 
425b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
426fc508e8eSSam Leffler 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
427fc508e8eSSam Leffler 	    __func__, key->wk_cipher->ic_name,
428fc508e8eSSam Leffler 	    key->wk_keyix, key->wk_flags,
429b032f27cSSam Leffler 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
430b032f27cSSam Leffler 	    key->wk_keylen);
431fc508e8eSSam Leffler 
432e6e547d5SSam Leffler 	if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
4338a1b9b6aSSam Leffler 		/*
4348a1b9b6aSSam Leffler 		 * Remove hardware entry.
4358a1b9b6aSSam Leffler 		 */
4368a1b9b6aSSam Leffler 		/* XXX key cache */
437b032f27cSSam Leffler 		if (!dev_key_delete(vap, key)) {
438b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
4398a1b9b6aSSam Leffler 			    "%s: driver did not delete key index %u\n",
440e6e547d5SSam Leffler 			    __func__, key->wk_keyix);
441b032f27cSSam Leffler 			vap->iv_stats.is_crypto_delkey++;
4428a1b9b6aSSam Leffler 			/* XXX recovery? */
4438a1b9b6aSSam Leffler 		}
4448a1b9b6aSSam Leffler 	}
4458a1b9b6aSSam Leffler 	cipher_detach(key);
4468a1b9b6aSSam Leffler 	memset(key, 0, sizeof(*key));
447b032f27cSSam Leffler 	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
4488a1b9b6aSSam Leffler 	return 1;
4498a1b9b6aSSam Leffler }
4508a1b9b6aSSam Leffler 
4518a1b9b6aSSam Leffler /*
4528a1b9b6aSSam Leffler  * Remove the specified key.
4538a1b9b6aSSam Leffler  */
4548a1b9b6aSSam Leffler int
455b032f27cSSam Leffler ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
4568a1b9b6aSSam Leffler {
4578a1b9b6aSSam Leffler 	int status;
4588a1b9b6aSSam Leffler 
459b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
460b032f27cSSam Leffler 	status = _ieee80211_crypto_delkey(vap, key);
461b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
4628a1b9b6aSSam Leffler 	return status;
4638a1b9b6aSSam Leffler }
4648a1b9b6aSSam Leffler 
4658a1b9b6aSSam Leffler /*
4668a1b9b6aSSam Leffler  * Clear the global key table.
4678a1b9b6aSSam Leffler  */
4688a1b9b6aSSam Leffler void
469b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
4708a1b9b6aSSam Leffler {
4718a1b9b6aSSam Leffler 	int i;
4728a1b9b6aSSam Leffler 
473b032f27cSSam Leffler 	ieee80211_key_update_begin(vap);
4748a1b9b6aSSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
475b032f27cSSam Leffler 		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
476b032f27cSSam Leffler 	ieee80211_key_update_end(vap);
4778a1b9b6aSSam Leffler }
4788a1b9b6aSSam Leffler 
4798a1b9b6aSSam Leffler /*
4808a1b9b6aSSam Leffler  * Set the contents of the specified key.
4818a1b9b6aSSam Leffler  *
4828a1b9b6aSSam Leffler  * Locking must be handled by the caller using:
483b032f27cSSam Leffler  *	ieee80211_key_update_begin(vap);
484b032f27cSSam Leffler  *	ieee80211_key_update_end(vap);
4858a1b9b6aSSam Leffler  */
4868a1b9b6aSSam Leffler int
48771fe06caSSam Leffler ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
4888a1b9b6aSSam Leffler {
4898a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip = key->wk_cipher;
4908a1b9b6aSSam Leffler 
4918a1b9b6aSSam Leffler 	KASSERT(cip != NULL, ("No cipher!"));
4928a1b9b6aSSam Leffler 
493b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
494fc508e8eSSam Leffler 	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
495fc508e8eSSam Leffler 	    __func__, cip->ic_name, key->wk_keyix,
49671fe06caSSam Leffler 	    key->wk_flags, ether_sprintf(key->wk_macaddr),
497b032f27cSSam Leffler 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
498b032f27cSSam Leffler 	    key->wk_keylen);
499fc508e8eSSam Leffler 
500e6e547d5SSam Leffler 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY)  == 0) {
501e6e547d5SSam Leffler 		/* XXX nothing allocated, should not happen */
502e6e547d5SSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
503e6e547d5SSam Leffler 		    "%s: no device key setup done; should not happen!\n",
504e6e547d5SSam Leffler 		    __func__);
505e6e547d5SSam Leffler 		vap->iv_stats.is_crypto_setkey_nokey++;
506e6e547d5SSam Leffler 		return 0;
507e6e547d5SSam Leffler 	}
5088a1b9b6aSSam Leffler 	/*
5098a1b9b6aSSam Leffler 	 * Give cipher a chance to validate key contents.
5108a1b9b6aSSam Leffler 	 * XXX should happen before modifying state.
5118a1b9b6aSSam Leffler 	 */
5128a1b9b6aSSam Leffler 	if (!cip->ic_setkey(key)) {
513b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
5148a1b9b6aSSam Leffler 		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
5158a1b9b6aSSam Leffler 		    __func__, cip->ic_name, key->wk_keyix,
5168a1b9b6aSSam Leffler 		    key->wk_keylen, key->wk_flags);
517b032f27cSSam Leffler 		vap->iv_stats.is_crypto_setkey_cipher++;
5188a1b9b6aSSam Leffler 		return 0;
5198a1b9b6aSSam Leffler 	}
52071fe06caSSam Leffler 	return dev_key_set(vap, key);
5218a1b9b6aSSam Leffler }
5228a1b9b6aSSam Leffler 
52354a95d0dSAdrian Chadd /*
52454a95d0dSAdrian Chadd  * Return index if the key is a WEP key (0..3); -1 otherwise.
52554a95d0dSAdrian Chadd  *
52654a95d0dSAdrian Chadd  * This is different to "get_keyid" which defaults to returning
52754a95d0dSAdrian Chadd  * 0 for unicast keys; it assumes that it won't be used for WEP.
52854a95d0dSAdrian Chadd  */
52954a95d0dSAdrian Chadd int
53054a95d0dSAdrian Chadd ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
53154a95d0dSAdrian Chadd     const struct ieee80211_key *k)
53254a95d0dSAdrian Chadd {
53354a95d0dSAdrian Chadd 
53454a95d0dSAdrian Chadd 	if (k >= &vap->iv_nw_keys[0] &&
53554a95d0dSAdrian Chadd 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
53654a95d0dSAdrian Chadd 		return (k - vap->iv_nw_keys);
537d0155f67SXin LI 	return (-1);
53854a95d0dSAdrian Chadd }
53954a95d0dSAdrian Chadd 
54054a95d0dSAdrian Chadd /*
54154a95d0dSAdrian Chadd  * Note: only supports a single unicast key (0).
54254a95d0dSAdrian Chadd  */
543ef0d8f63SAdrian Chadd uint8_t
544ef0d8f63SAdrian Chadd ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
545ef0d8f63SAdrian Chadd {
546ef0d8f63SAdrian Chadd 	if (k >= &vap->iv_nw_keys[0] &&
547ef0d8f63SAdrian Chadd 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
548ef0d8f63SAdrian Chadd 		return (k - vap->iv_nw_keys);
549ef0d8f63SAdrian Chadd 	else
550ef0d8f63SAdrian Chadd 		return (0);
551ef0d8f63SAdrian Chadd }
552ef0d8f63SAdrian Chadd 
5538a1b9b6aSSam Leffler struct ieee80211_key *
55415395998SAdrian Chadd ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
5558a1b9b6aSSam Leffler {
556b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5578a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
5588a1b9b6aSSam Leffler 
5598a1b9b6aSSam Leffler 	/*
5608a1b9b6aSSam Leffler 	 * Multicast traffic always uses the multicast key.
5618a1b9b6aSSam Leffler 	 * Otherwise if a unicast key is set we use that and
5628a1b9b6aSSam Leffler 	 * it is always key index 0.  When no unicast key is
5638a1b9b6aSSam Leffler 	 * set we fall back to the default transmit key.
5648a1b9b6aSSam Leffler 	 */
5658a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5668a1b9b6aSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
567cda15ce1SSam Leffler 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
568b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
569b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
570b032f27cSSam Leffler 			    wh->i_addr1,
571b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
572b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
573b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
5748a1b9b6aSSam Leffler 			return NULL;
5758a1b9b6aSSam Leffler 		}
57615395998SAdrian Chadd 		return &vap->iv_nw_keys[vap->iv_def_txkey];
57715395998SAdrian Chadd 	}
578ef0d8f63SAdrian Chadd 
57915395998SAdrian Chadd 	return &ni->ni_ucastkey;
58015395998SAdrian Chadd }
58115395998SAdrian Chadd 
58215395998SAdrian Chadd /*
58315395998SAdrian Chadd  * Add privacy headers appropriate for the specified key.
58415395998SAdrian Chadd  */
58515395998SAdrian Chadd struct ieee80211_key *
58615395998SAdrian Chadd ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
58715395998SAdrian Chadd {
58815395998SAdrian Chadd 	struct ieee80211_key *k;
58915395998SAdrian Chadd 	const struct ieee80211_cipher *cip;
59015395998SAdrian Chadd 
59115395998SAdrian Chadd 	if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
5928a1b9b6aSSam Leffler 		cip = k->wk_cipher;
593ef0d8f63SAdrian Chadd 		return (cip->ic_encap(k, m) ? k : NULL);
5948a1b9b6aSSam Leffler 	}
5958a1b9b6aSSam Leffler 
59615395998SAdrian Chadd 	return NULL;
59715395998SAdrian Chadd }
59815395998SAdrian Chadd 
5998a1b9b6aSSam Leffler /*
6008a1b9b6aSSam Leffler  * Validate and strip privacy headers (and trailer) for a
6018a1b9b6aSSam Leffler  * received frame that has the WEP/Privacy bit set.
6028a1b9b6aSSam Leffler  */
603fe75b452SAdrian Chadd int
604fe75b452SAdrian Chadd ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
605fe75b452SAdrian Chadd     struct ieee80211_key **key)
6068a1b9b6aSSam Leffler {
6078a1b9b6aSSam Leffler #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
6088a1b9b6aSSam Leffler #define	IEEE80211_WEP_MINLEN \
609a92c6eb0SSam Leffler 	(sizeof(struct ieee80211_frame) + \
6108a1b9b6aSSam Leffler 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
611b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6128a1b9b6aSSam Leffler 	struct ieee80211_key *k;
6138a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
614fe75b452SAdrian Chadd 	const struct ieee80211_rx_stats *rxs;
6158a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
61668e8e04eSSam Leffler 	uint8_t keyid;
6178a1b9b6aSSam Leffler 
618fe75b452SAdrian Chadd 	/*
619fe75b452SAdrian Chadd 	 * Check for hardware decryption and IV stripping.
620fe75b452SAdrian Chadd 	 * If the IV is stripped then we definitely can't find a key.
621fe75b452SAdrian Chadd 	 * Set the key to NULL but return true; upper layers
622fe75b452SAdrian Chadd 	 * will need to handle a NULL key for a successful
623fe75b452SAdrian Chadd 	 * decrypt.
624fe75b452SAdrian Chadd 	 */
625fe75b452SAdrian Chadd 	rxs = ieee80211_get_rx_params_ptr(m);
626fe75b452SAdrian Chadd 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
627fe75b452SAdrian Chadd 		if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
628fe75b452SAdrian Chadd 			/*
629fe75b452SAdrian Chadd 			 * Hardware decrypted, IV stripped.
630fe75b452SAdrian Chadd 			 * We can't find a key with a stripped IV.
631fe75b452SAdrian Chadd 			 * Return successful.
632fe75b452SAdrian Chadd 			 */
633fe75b452SAdrian Chadd 			*key = NULL;
634fe75b452SAdrian Chadd 			return (1);
635fe75b452SAdrian Chadd 		}
636fe75b452SAdrian Chadd 	}
637fe75b452SAdrian Chadd 
6388a1b9b6aSSam Leffler 	/* NB: this minimum size data frame could be bigger */
6398a1b9b6aSSam Leffler 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
640b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
6418a1b9b6aSSam Leffler 			"%s: WEP data frame too short, len %u\n",
6428a1b9b6aSSam Leffler 			__func__, m->m_pkthdr.len);
643b032f27cSSam Leffler 		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */
644fe75b452SAdrian Chadd 		*key = NULL;
645fe75b452SAdrian Chadd 		return (0);
6461a1e1d21SSam Leffler 	}
6471a1e1d21SSam Leffler 
6481a1e1d21SSam Leffler 	/*
6498a1b9b6aSSam Leffler 	 * Locate the key. If unicast and there is no unicast
6508a1b9b6aSSam Leffler 	 * key then we fall back to the key id in the header.
6518a1b9b6aSSam Leffler 	 * This assumes unicast keys are only configured when
6528a1b9b6aSSam Leffler 	 * the key id in the header is meaningless (typically 0).
6531a1e1d21SSam Leffler 	 */
6548a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
655b032f27cSSam Leffler 	m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
6568a1b9b6aSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
657cda15ce1SSam Leffler 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
658b032f27cSSam Leffler 		k = &vap->iv_nw_keys[keyid >> 6];
6591a1e1d21SSam Leffler 	else
6608a1b9b6aSSam Leffler 		k = &ni->ni_ucastkey;
6611a1e1d21SSam Leffler 
6621a1e1d21SSam Leffler 	/*
6638a1b9b6aSSam Leffler 	 * Insure crypto header is contiguous for all decap work.
6641a1e1d21SSam Leffler 	 */
6658a1b9b6aSSam Leffler 	cip = k->wk_cipher;
6668a1b9b6aSSam Leffler 	if (m->m_len < hdrlen + cip->ic_header &&
6678a1b9b6aSSam Leffler 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
668b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
669b032f27cSSam Leffler 		    "unable to pullup %s header", cip->ic_name);
670b032f27cSSam Leffler 		vap->iv_stats.is_rx_wepfail++;	/* XXX */
671fe75b452SAdrian Chadd 		*key = NULL;
672fe75b452SAdrian Chadd 		return (0);
6738a1b9b6aSSam Leffler 	}
6741a1e1d21SSam Leffler 
675fe75b452SAdrian Chadd 	/*
676fe75b452SAdrian Chadd 	 * Attempt decryption.
677fe75b452SAdrian Chadd 	 *
678fe75b452SAdrian Chadd 	 * If we fail then don't return the key - return NULL
679fe75b452SAdrian Chadd 	 * and an error.
680fe75b452SAdrian Chadd 	 */
681fe75b452SAdrian Chadd 	if (cip->ic_decap(k, m, hdrlen)) {
682fe75b452SAdrian Chadd 		/* success */
683fe75b452SAdrian Chadd 		*key = k;
684fe75b452SAdrian Chadd 		return (1);
685fe75b452SAdrian Chadd 	}
686fe75b452SAdrian Chadd 
687fe75b452SAdrian Chadd 	/* Failure */
688fe75b452SAdrian Chadd 	*key = NULL;
689fe75b452SAdrian Chadd 	return (0);
6908a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN
6918a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN
6921a1e1d21SSam Leffler }
69306b2d888SSam Leffler 
694ee9d294bSAdrian Chadd /*
695ee9d294bSAdrian Chadd  * Check and remove any MIC.
696ee9d294bSAdrian Chadd  */
697ee9d294bSAdrian Chadd int
698ee9d294bSAdrian Chadd ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
699ee9d294bSAdrian Chadd     struct mbuf *m, int force)
700ee9d294bSAdrian Chadd {
701ee9d294bSAdrian Chadd 	const struct ieee80211_cipher *cip;
702ee9d294bSAdrian Chadd 	const struct ieee80211_rx_stats *rxs;
703ee9d294bSAdrian Chadd 	struct ieee80211_frame *wh;
704ee9d294bSAdrian Chadd 
705ee9d294bSAdrian Chadd 	rxs = ieee80211_get_rx_params_ptr(m);
706ee9d294bSAdrian Chadd 	wh = mtod(m, struct ieee80211_frame *);
707ee9d294bSAdrian Chadd 
708ee9d294bSAdrian Chadd 	/*
709ee9d294bSAdrian Chadd 	 * Handle demic / mic errors from hardware-decrypted offload devices.
710ee9d294bSAdrian Chadd 	 */
711ee9d294bSAdrian Chadd 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
712ee9d294bSAdrian Chadd 		if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) {
713ee9d294bSAdrian Chadd 			/*
714ee9d294bSAdrian Chadd 			 * Hardware has said MIC failed.  We don't care about
715ee9d294bSAdrian Chadd 			 * whether it was stripped or not.
716ee9d294bSAdrian Chadd 			 *
717ee9d294bSAdrian Chadd 			 * Eventually - teach the demic methods in crypto
718ee9d294bSAdrian Chadd 			 * modules to handle a NULL key and not to dereference
719ee9d294bSAdrian Chadd 			 * it.
720ee9d294bSAdrian Chadd 			 */
721ee9d294bSAdrian Chadd 			ieee80211_notify_michael_failure(vap, wh, -1);
722ee9d294bSAdrian Chadd 			return (0);
723ee9d294bSAdrian Chadd 		}
724ee9d294bSAdrian Chadd 
725ee9d294bSAdrian Chadd 		if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) {
726ee9d294bSAdrian Chadd 			/*
727ee9d294bSAdrian Chadd 			 * Hardware has decrypted and not indicated a
728ee9d294bSAdrian Chadd 			 * MIC failure and has stripped the MIC.
729ee9d294bSAdrian Chadd 			 * We may not have a key, so for now just
730ee9d294bSAdrian Chadd 			 * return OK.
731ee9d294bSAdrian Chadd 			 */
732ee9d294bSAdrian Chadd 			return (1);
733ee9d294bSAdrian Chadd 		}
734ee9d294bSAdrian Chadd 	}
735ee9d294bSAdrian Chadd 
736ee9d294bSAdrian Chadd 	/*
737ee9d294bSAdrian Chadd 	 * If we don't have a key at this point then we don't
738ee9d294bSAdrian Chadd 	 * have to demic anything.
739ee9d294bSAdrian Chadd 	 */
740ee9d294bSAdrian Chadd 	if (k == NULL)
741ee9d294bSAdrian Chadd 		return (1);
742ee9d294bSAdrian Chadd 
743ee9d294bSAdrian Chadd 	cip = k->wk_cipher;
744ee9d294bSAdrian Chadd 	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
745ee9d294bSAdrian Chadd }
746ee9d294bSAdrian Chadd 
747ee9d294bSAdrian Chadd 
74806b2d888SSam Leffler static void
74906b2d888SSam Leffler load_ucastkey(void *arg, struct ieee80211_node *ni)
75006b2d888SSam Leffler {
75106b2d888SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
75206b2d888SSam Leffler 	struct ieee80211_key *k;
75306b2d888SSam Leffler 
75406b2d888SSam Leffler 	if (vap->iv_state != IEEE80211_S_RUN)
75506b2d888SSam Leffler 		return;
75606b2d888SSam Leffler 	k = &ni->ni_ucastkey;
757e6e547d5SSam Leffler 	if (k->wk_flags & IEEE80211_KEY_DEVKEY)
75806b2d888SSam Leffler 		dev_key_set(vap, k);
75906b2d888SSam Leffler }
76006b2d888SSam Leffler 
76106b2d888SSam Leffler /*
76206b2d888SSam Leffler  * Re-load all keys known to the 802.11 layer that may
76306b2d888SSam Leffler  * have hardware state backing them.  This is used by
76406b2d888SSam Leffler  * drivers on resume to push keys down into the device.
76506b2d888SSam Leffler  */
76606b2d888SSam Leffler void
76706b2d888SSam Leffler ieee80211_crypto_reload_keys(struct ieee80211com *ic)
76806b2d888SSam Leffler {
76906b2d888SSam Leffler 	struct ieee80211vap *vap;
77006b2d888SSam Leffler 	int i;
77106b2d888SSam Leffler 
77206b2d888SSam Leffler 	/*
77306b2d888SSam Leffler 	 * Keys in the global key table of each vap.
77406b2d888SSam Leffler 	 */
77506b2d888SSam Leffler 	/* NB: used only during resume so don't lock for now */
77606b2d888SSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
77706b2d888SSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN)
77806b2d888SSam Leffler 			continue;
77906b2d888SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
78006b2d888SSam Leffler 			const struct ieee80211_key *k = &vap->iv_nw_keys[i];
781e6e547d5SSam Leffler 			if (k->wk_flags & IEEE80211_KEY_DEVKEY)
78206b2d888SSam Leffler 				dev_key_set(vap, k);
78306b2d888SSam Leffler 		}
78406b2d888SSam Leffler 	}
78506b2d888SSam Leffler 	/*
78606b2d888SSam Leffler 	 * Unicast keys.
78706b2d888SSam Leffler 	 */
78806b2d888SSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
78906b2d888SSam Leffler }
790