1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116742Ssam */
26116742Ssam
27116742Ssam#include <sys/cdefs.h>
28116742Ssam__FBSDID("$FreeBSD$");
29116742Ssam
30138568Ssam/*
31138568Ssam * IEEE 802.11 generic crypto support.
32138568Ssam */
33178354Ssam#include "opt_wlan.h"
34178354Ssam
35116742Ssam#include <sys/param.h>
36178354Ssam#include <sys/kernel.h>
37178354Ssam#include <sys/malloc.h>
38116742Ssam#include <sys/mbuf.h>
39138568Ssam
40116742Ssam#include <sys/socket.h>
41116742Ssam
42116742Ssam#include <net/if.h>
43116742Ssam#include <net/if_media.h>
44138568Ssam#include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
45116742Ssam
46116742Ssam#include <net80211/ieee80211_var.h>
47116742Ssam
48178354SsamMALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
49178354Ssam
50178354Ssamstatic	int _ieee80211_crypto_delkey(struct ieee80211vap *,
51178354Ssam		struct ieee80211_key *);
52178354Ssam
53138568Ssam/*
54138568Ssam * Table of registered cipher modules.
55138568Ssam */
56138568Ssamstatic	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
57116742Ssam
58138568Ssam/*
59138568Ssam * Default "null" key management routines.
60138568Ssam */
61138568Ssamstatic int
62183248Ssamnull_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
63148863Ssam	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
64138568Ssam{
65178354Ssam	if (!(&vap->iv_nw_keys[0] <= k &&
66178354Ssam	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
67147870Ssam		/*
68147870Ssam		 * Not in the global key table, the driver should handle this
69147870Ssam		 * by allocating a slot in the h/w key table/cache.  In
70147870Ssam		 * lieu of that return key slot 0 for any unicast key
71147870Ssam		 * request.  We disallow the request if this is a group key.
72147870Ssam		 * This default policy does the right thing for legacy hardware
73147870Ssam		 * with a 4 key table.  It also handles devices that pass
74147870Ssam		 * packets through untouched when marked with the WEP bit
75147870Ssam		 * and key index 0.
76147870Ssam		 */
77148863Ssam		if (k->wk_flags & IEEE80211_KEY_GROUP)
78148863Ssam			return 0;
79148863Ssam		*keyix = 0;	/* NB: use key index 0 for ucast key */
80148863Ssam	} else {
81178354Ssam		*keyix = k - vap->iv_nw_keys;
82147870Ssam	}
83148863Ssam	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
84148863Ssam	return 1;
85138568Ssam}
86138568Ssamstatic int
87178354Ssamnull_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
88138568Ssam{
89138568Ssam	return 1;
90138568Ssam}
91138568Ssamstatic 	int
92178354Ssamnull_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
93170530Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
94138568Ssam{
95138568Ssam	return 1;
96138568Ssam}
97178354Ssamstatic void null_key_update(struct ieee80211vap *vap) {}
98116742Ssam
99138568Ssam/*
100138568Ssam * Write-arounds for common operations.
101138568Ssam */
102138568Ssamstatic __inline void
103138568Ssamcipher_detach(struct ieee80211_key *key)
104138568Ssam{
105138568Ssam	key->wk_cipher->ic_detach(key);
106138568Ssam}
107116742Ssam
108138568Ssamstatic __inline void *
109178354Ssamcipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
110138568Ssam{
111178354Ssam	return key->wk_cipher->ic_attach(vap, key);
112138568Ssam}
113138568Ssam
114138568Ssam/*
115138568Ssam * Wrappers for driver key management methods.
116138568Ssam */
117138568Ssamstatic __inline int
118178354Ssamdev_key_alloc(struct ieee80211vap *vap,
119183248Ssam	struct ieee80211_key *key,
120148863Ssam	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
121138568Ssam{
122178354Ssam	return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
123138568Ssam}
124138568Ssam
125138568Ssamstatic __inline int
126178354Ssamdev_key_delete(struct ieee80211vap *vap,
127138568Ssam	const struct ieee80211_key *key)
128138568Ssam{
129178354Ssam	return vap->iv_key_delete(vap, key);
130138568Ssam}
131138568Ssam
132138568Ssamstatic __inline int
133179395Ssamdev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
134138568Ssam{
135179395Ssam	return vap->iv_key_set(vap, key, key->wk_macaddr);
136138568Ssam}
137138568Ssam
138138568Ssam/*
139178354Ssam * Setup crypto support for a device/shared instance.
140138568Ssam */
141116742Ssamvoid
142138568Ssamieee80211_crypto_attach(struct ieee80211com *ic)
143116742Ssam{
144178354Ssam	/* NB: we assume everything is pre-zero'd */
145178354Ssam	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
146178354Ssam}
147178354Ssam
148178354Ssam/*
149178354Ssam * Teardown crypto support.
150178354Ssam */
151178354Ssamvoid
152178354Ssamieee80211_crypto_detach(struct ieee80211com *ic)
153178354Ssam{
154178354Ssam}
155178354Ssam
156178354Ssam/*
157178354Ssam * Setup crypto support for a vap.
158178354Ssam */
159178354Ssamvoid
160178354Ssamieee80211_crypto_vattach(struct ieee80211vap *vap)
161178354Ssam{
162138568Ssam	int i;
163116742Ssam
164138568Ssam	/* NB: we assume everything is pre-zero'd */
165178354Ssam	vap->iv_max_keyix = IEEE80211_WEP_NKID;
166178354Ssam	vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
167138568Ssam	for (i = 0; i < IEEE80211_WEP_NKID; i++)
168178354Ssam		ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
169144960Ssam			IEEE80211_KEYIX_NONE);
170116742Ssam	/*
171138568Ssam	 * Initialize the driver key support routines to noop entries.
172138568Ssam	 * This is useful especially for the cipher test modules.
173116742Ssam	 */
174178354Ssam	vap->iv_key_alloc = null_key_alloc;
175178354Ssam	vap->iv_key_set = null_key_set;
176178354Ssam	vap->iv_key_delete = null_key_delete;
177178354Ssam	vap->iv_key_update_begin = null_key_update;
178178354Ssam	vap->iv_key_update_end = null_key_update;
179116742Ssam}
180116742Ssam
181138568Ssam/*
182178354Ssam * Teardown crypto support for a vap.
183138568Ssam */
184116742Ssamvoid
185178354Ssamieee80211_crypto_vdetach(struct ieee80211vap *vap)
186116742Ssam{
187178354Ssam	ieee80211_crypto_delglobalkeys(vap);
188138568Ssam}
189116742Ssam
190138568Ssam/*
191138568Ssam * Register a crypto cipher module.
192138568Ssam */
193138568Ssamvoid
194138568Ssamieee80211_crypto_register(const struct ieee80211_cipher *cip)
195138568Ssam{
196138568Ssam	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
197138568Ssam		printf("%s: cipher %s has an invalid cipher index %u\n",
198138568Ssam			__func__, cip->ic_name, cip->ic_cipher);
199138568Ssam		return;
200116742Ssam	}
201138568Ssam	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
202138568Ssam		printf("%s: cipher %s registered with a different template\n",
203138568Ssam			__func__, cip->ic_name);
204138568Ssam		return;
205138568Ssam	}
206138568Ssam	ciphers[cip->ic_cipher] = cip;
207116742Ssam}
208116742Ssam
209138568Ssam/*
210138568Ssam * Unregister a crypto cipher module.
211138568Ssam */
212138568Ssamvoid
213138568Ssamieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
214116742Ssam{
215138568Ssam	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
216138568Ssam		printf("%s: cipher %s has an invalid cipher index %u\n",
217138568Ssam			__func__, cip->ic_name, cip->ic_cipher);
218138568Ssam		return;
219116742Ssam	}
220138568Ssam	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
221138568Ssam		printf("%s: cipher %s registered with a different template\n",
222138568Ssam			__func__, cip->ic_name);
223138568Ssam		return;
224121180Ssam	}
225138568Ssam	/* NB: don't complain about not being registered */
226138568Ssam	/* XXX disallow if references */
227138568Ssam	ciphers[cip->ic_cipher] = NULL;
228138568Ssam}
229138568Ssam
230138568Ssamint
231138568Ssamieee80211_crypto_available(u_int cipher)
232138568Ssam{
233138568Ssam	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
234138568Ssam}
235138568Ssam
236138568Ssam/* XXX well-known names! */
237178354Ssamstatic const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
238193665Ssam	[IEEE80211_CIPHER_WEP]	   = "wlan_wep",
239193665Ssam	[IEEE80211_CIPHER_TKIP]	   = "wlan_tkip",
240193665Ssam	[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
241193665Ssam	[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
242193665Ssam	[IEEE80211_CIPHER_TKIPMIC] = "#4",	/* NB: reserved */
243193665Ssam	[IEEE80211_CIPHER_CKIP]	   = "wlan_ckip",
244193665Ssam	[IEEE80211_CIPHER_NONE]	   = "wlan_none",
245138568Ssam};
246138568Ssam
247195812Ssam/* NB: there must be no overlap between user-supplied and device-owned flags */
248195812SsamCTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
249195812Ssam
250138568Ssam/*
251138568Ssam * Establish a relationship between the specified key and cipher
252144960Ssam * and, if necessary, allocate a hardware index from the driver.
253195812Ssam * Note that when a fixed key index is required it must be specified.
254138568Ssam *
255138568Ssam * This must be the first call applied to a key; all the other key
256138568Ssam * routines assume wk_cipher is setup.
257138568Ssam *
258138568Ssam * Locking must be handled by the caller using:
259178354Ssam *	ieee80211_key_update_begin(vap);
260178354Ssam *	ieee80211_key_update_end(vap);
261138568Ssam */
262138568Ssamint
263178354Ssamieee80211_crypto_newkey(struct ieee80211vap *vap,
264144960Ssam	int cipher, int flags, struct ieee80211_key *key)
265138568Ssam{
266178354Ssam	struct ieee80211com *ic = vap->iv_ic;
267138568Ssam	const struct ieee80211_cipher *cip;
268148863Ssam	ieee80211_keyix keyix, rxkeyix;
269138568Ssam	void *keyctx;
270138568Ssam	int oflags;
271138568Ssam
272184268Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
273184268Ssam	    "%s: cipher %u flags 0x%x keyix %u\n",
274184268Ssam	    __func__, cipher, flags, key->wk_keyix);
275184268Ssam
276138568Ssam	/*
277138568Ssam	 * Validate cipher and set reference to cipher routines.
278138568Ssam	 */
279138568Ssam	if (cipher >= IEEE80211_CIPHER_MAX) {
280178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
281178354Ssam		    "%s: invalid cipher %u\n", __func__, cipher);
282178354Ssam		vap->iv_stats.is_crypto_badcipher++;
283138568Ssam		return 0;
284116742Ssam	}
285138568Ssam	cip = ciphers[cipher];
286138568Ssam	if (cip == NULL) {
287116742Ssam		/*
288138568Ssam		 * Auto-load cipher module if we have a well-known name
289138568Ssam		 * for it.  It might be better to use string names rather
290138568Ssam		 * than numbers and craft a module name based on the cipher
291138568Ssam		 * name; e.g. wlan_cipher_<cipher-name>.
292116742Ssam		 */
293178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
294178354Ssam		    "%s: unregistered cipher %u, load module %s\n",
295178354Ssam		    __func__, cipher, cipher_modnames[cipher]);
296178354Ssam		ieee80211_load_module(cipher_modnames[cipher]);
297178354Ssam		/*
298178354Ssam		 * If cipher module loaded it should immediately
299178354Ssam		 * call ieee80211_crypto_register which will fill
300178354Ssam		 * in the entry in the ciphers array.
301178354Ssam		 */
302178354Ssam		cip = ciphers[cipher];
303138568Ssam		if (cip == NULL) {
304178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
305178354Ssam			    "%s: unable to load cipher %u, module %s\n",
306178354Ssam			    __func__, cipher, cipher_modnames[cipher]);
307178354Ssam			vap->iv_stats.is_crypto_nocipher++;
308138568Ssam			return 0;
309138568Ssam		}
310116742Ssam	}
311116742Ssam
312138568Ssam	oflags = key->wk_flags;
313144960Ssam	flags &= IEEE80211_KEY_COMMON;
314195812Ssam	/* NB: preserve device attributes */
315195812Ssam	flags |= (oflags & IEEE80211_KEY_DEVICE);
316138568Ssam	/*
317138568Ssam	 * If the hardware does not support the cipher then
318138568Ssam	 * fallback to a host-based implementation.
319138568Ssam	 */
320178354Ssam	if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
321178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
322138568Ssam		    "%s: no h/w support for cipher %s, falling back to s/w\n",
323138568Ssam		    __func__, cip->ic_name);
324144960Ssam		flags |= IEEE80211_KEY_SWCRYPT;
325138568Ssam	}
326138568Ssam	/*
327138568Ssam	 * Hardware TKIP with software MIC is an important
328138568Ssam	 * combination; we handle it by flagging each key,
329138568Ssam	 * the cipher modules honor it.
330138568Ssam	 */
331138568Ssam	if (cipher == IEEE80211_CIPHER_TKIP &&
332178354Ssam	    (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
333178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
334138568Ssam		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
335138568Ssam		    __func__);
336144960Ssam		flags |= IEEE80211_KEY_SWMIC;
337138568Ssam	}
338138568Ssam
339138568Ssam	/*
340138568Ssam	 * Bind cipher to key instance.  Note we do this
341138568Ssam	 * after checking the device capabilities so the
342138568Ssam	 * cipher module can optimize space usage based on
343138568Ssam	 * whether or not it needs to do the cipher work.
344138568Ssam	 */
345144960Ssam	if (key->wk_cipher != cip || key->wk_flags != flags) {
346144960Ssam		/*
347144960Ssam		 * Fillin the flags so cipher modules can see s/w
348144960Ssam		 * crypto requirements and potentially allocate
349144960Ssam		 * different state and/or attach different method
350144960Ssam		 * pointers.
351144960Ssam		 */
352144960Ssam		key->wk_flags = flags;
353178354Ssam		keyctx = cip->ic_attach(vap, key);
354138568Ssam		if (keyctx == NULL) {
355178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
356138568Ssam				"%s: unable to attach cipher %s\n",
357138568Ssam				__func__, cip->ic_name);
358138568Ssam			key->wk_flags = oflags;	/* restore old flags */
359178354Ssam			vap->iv_stats.is_crypto_attachfail++;
360138568Ssam			return 0;
361116742Ssam		}
362138568Ssam		cipher_detach(key);
363138568Ssam		key->wk_cipher = cip;		/* XXX refcnt? */
364138568Ssam		key->wk_private = keyctx;
365138568Ssam	}
366138568Ssam
367138568Ssam	/*
368138568Ssam	 * Ask the driver for a key index if we don't have one.
369138568Ssam	 * Note that entries in the global key table always have
370138568Ssam	 * an index; this means it's safe to call this routine
371138568Ssam	 * for these entries just to setup the reference to the
372138568Ssam	 * cipher template.  Note also that when using software
373138568Ssam	 * crypto we also call the driver to give us a key index.
374138568Ssam	 */
375183248Ssam	if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
376178354Ssam		if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
377138568Ssam			/*
378183248Ssam			 * Unable to setup driver state.
379138568Ssam			 */
380178354Ssam			vap->iv_stats.is_crypto_keyfail++;
381178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
382138568Ssam			    "%s: unable to setup cipher %s\n",
383138568Ssam			    __func__, cip->ic_name);
384138568Ssam			return 0;
385116742Ssam		}
386183248Ssam		if (key->wk_flags != flags) {
387183248Ssam			/*
388183248Ssam			 * Driver overrode flags we setup; typically because
389183248Ssam			 * resources were unavailable to handle _this_ key.
390183248Ssam			 * Re-attach the cipher context to allow cipher
391183248Ssam			 * modules to handle differing requirements.
392183248Ssam			 */
393183248Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
394183248Ssam			    "%s: driver override for cipher %s, flags "
395183248Ssam			    "0x%x -> 0x%x\n", __func__, cip->ic_name,
396183248Ssam			    oflags, key->wk_flags);
397183248Ssam			keyctx = cip->ic_attach(vap, key);
398183248Ssam			if (keyctx == NULL) {
399183248Ssam				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
400183248Ssam				    "%s: unable to attach cipher %s with "
401183248Ssam				    "flags 0x%x\n", __func__, cip->ic_name,
402183248Ssam				    key->wk_flags);
403183248Ssam				key->wk_flags = oflags;	/* restore old flags */
404183248Ssam				vap->iv_stats.is_crypto_attachfail++;
405183248Ssam				return 0;
406183248Ssam			}
407183248Ssam			cipher_detach(key);
408183248Ssam			key->wk_cipher = cip;		/* XXX refcnt? */
409183248Ssam			key->wk_private = keyctx;
410183248Ssam		}
411148863Ssam		key->wk_keyix = keyix;
412148863Ssam		key->wk_rxkeyix = rxkeyix;
413183248Ssam		key->wk_flags |= IEEE80211_KEY_DEVKEY;
414116742Ssam	}
415138568Ssam	return 1;
416138568Ssam}
417138568Ssam
418138568Ssam/*
419138568Ssam * Remove the key (no locking, for internal use).
420138568Ssam */
421138568Ssamstatic int
422178354Ssam_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
423138568Ssam{
424138568Ssam	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
425138568Ssam
426178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
427139504Ssam	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
428139504Ssam	    __func__, key->wk_cipher->ic_name,
429139504Ssam	    key->wk_keyix, key->wk_flags,
430178354Ssam	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
431178354Ssam	    key->wk_keylen);
432139504Ssam
433183248Ssam	if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
434138568Ssam		/*
435138568Ssam		 * Remove hardware entry.
436138568Ssam		 */
437138568Ssam		/* XXX key cache */
438178354Ssam		if (!dev_key_delete(vap, key)) {
439178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
440138568Ssam			    "%s: driver did not delete key index %u\n",
441183248Ssam			    __func__, key->wk_keyix);
442178354Ssam			vap->iv_stats.is_crypto_delkey++;
443138568Ssam			/* XXX recovery? */
444116742Ssam		}
445116742Ssam	}
446138568Ssam	cipher_detach(key);
447138568Ssam	memset(key, 0, sizeof(*key));
448178354Ssam	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
449138568Ssam	return 1;
450138568Ssam}
451116742Ssam
452138568Ssam/*
453138568Ssam * Remove the specified key.
454138568Ssam */
455138568Ssamint
456178354Ssamieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
457138568Ssam{
458138568Ssam	int status;
459138568Ssam
460178354Ssam	ieee80211_key_update_begin(vap);
461178354Ssam	status = _ieee80211_crypto_delkey(vap, key);
462178354Ssam	ieee80211_key_update_end(vap);
463138568Ssam	return status;
464116742Ssam}
465116742Ssam
466116742Ssam/*
467138568Ssam * Clear the global key table.
468116742Ssam */
469138568Ssamvoid
470178354Ssamieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
471138568Ssam{
472138568Ssam	int i;
473116742Ssam
474178354Ssam	ieee80211_key_update_begin(vap);
475138568Ssam	for (i = 0; i < IEEE80211_WEP_NKID; i++)
476178354Ssam		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
477178354Ssam	ieee80211_key_update_end(vap);
478138568Ssam}
479116742Ssam
480138568Ssam/*
481138568Ssam * Set the contents of the specified key.
482138568Ssam *
483138568Ssam * Locking must be handled by the caller using:
484178354Ssam *	ieee80211_key_update_begin(vap);
485178354Ssam *	ieee80211_key_update_end(vap);
486138568Ssam */
487138568Ssamint
488179395Ssamieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
489116742Ssam{
490138568Ssam	const struct ieee80211_cipher *cip = key->wk_cipher;
491116742Ssam
492138568Ssam	KASSERT(cip != NULL, ("No cipher!"));
493138568Ssam
494178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
495139504Ssam	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
496139504Ssam	    __func__, cip->ic_name, key->wk_keyix,
497179395Ssam	    key->wk_flags, ether_sprintf(key->wk_macaddr),
498178354Ssam	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
499178354Ssam	    key->wk_keylen);
500139504Ssam
501183248Ssam	if ((key->wk_flags & IEEE80211_KEY_DEVKEY)  == 0) {
502183248Ssam		/* XXX nothing allocated, should not happen */
503183248Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
504183248Ssam		    "%s: no device key setup done; should not happen!\n",
505183248Ssam		    __func__);
506183248Ssam		vap->iv_stats.is_crypto_setkey_nokey++;
507183248Ssam		return 0;
508183248Ssam	}
509138568Ssam	/*
510138568Ssam	 * Give cipher a chance to validate key contents.
511138568Ssam	 * XXX should happen before modifying state.
512138568Ssam	 */
513138568Ssam	if (!cip->ic_setkey(key)) {
514178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
515138568Ssam		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
516138568Ssam		    __func__, cip->ic_name, key->wk_keyix,
517138568Ssam		    key->wk_keylen, key->wk_flags);
518178354Ssam		vap->iv_stats.is_crypto_setkey_cipher++;
519138568Ssam		return 0;
520138568Ssam	}
521179395Ssam	return dev_key_set(vap, key);
522138568Ssam}
523138568Ssam
524138568Ssam/*
525138568Ssam * Add privacy headers appropriate for the specified key.
526138568Ssam */
527138568Ssamstruct ieee80211_key *
528178354Ssamieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
529138568Ssam{
530178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
531138568Ssam	struct ieee80211_key *k;
532138568Ssam	struct ieee80211_frame *wh;
533138568Ssam	const struct ieee80211_cipher *cip;
534170530Ssam	uint8_t keyid;
535138568Ssam
536138568Ssam	/*
537138568Ssam	 * Multicast traffic always uses the multicast key.
538138568Ssam	 * Otherwise if a unicast key is set we use that and
539138568Ssam	 * it is always key index 0.  When no unicast key is
540138568Ssam	 * set we fall back to the default transmit key.
541138568Ssam	 */
542138568Ssam	wh = mtod(m, struct ieee80211_frame *);
543138568Ssam	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
544167432Ssam	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
545178354Ssam		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
546178354Ssam			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
547178354Ssam			    wh->i_addr1,
548178354Ssam			    "no default transmit key (%s) deftxkey %u",
549178354Ssam			    __func__, vap->iv_def_txkey);
550178354Ssam			vap->iv_stats.is_tx_nodefkey++;
551138568Ssam			return NULL;
552116742Ssam		}
553178354Ssam		keyid = vap->iv_def_txkey;
554178354Ssam		k = &vap->iv_nw_keys[vap->iv_def_txkey];
555138568Ssam	} else {
556144960Ssam		keyid = 0;
557138568Ssam		k = &ni->ni_ucastkey;
558116742Ssam	}
559138568Ssam	cip = k->wk_cipher;
560144960Ssam	return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
561116742Ssam}
562116742Ssam
563116742Ssam/*
564138568Ssam * Validate and strip privacy headers (and trailer) for a
565138568Ssam * received frame that has the WEP/Privacy bit set.
566116742Ssam */
567138568Ssamstruct ieee80211_key *
568178354Ssamieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
569116742Ssam{
570138568Ssam#define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
571138568Ssam#define	IEEE80211_WEP_MINLEN \
572148303Ssam	(sizeof(struct ieee80211_frame) + \
573138568Ssam	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
574178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
575138568Ssam	struct ieee80211_key *k;
576138568Ssam	struct ieee80211_frame *wh;
577138568Ssam	const struct ieee80211_cipher *cip;
578170530Ssam	uint8_t keyid;
579116742Ssam
580138568Ssam	/* NB: this minimum size data frame could be bigger */
581138568Ssam	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
582178354Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
583138568Ssam			"%s: WEP data frame too short, len %u\n",
584138568Ssam			__func__, m->m_pkthdr.len);
585178354Ssam		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */
586138568Ssam		return NULL;
587138568Ssam	}
588138568Ssam
589138568Ssam	/*
590138568Ssam	 * Locate the key. If unicast and there is no unicast
591138568Ssam	 * key then we fall back to the key id in the header.
592138568Ssam	 * This assumes unicast keys are only configured when
593138568Ssam	 * the key id in the header is meaningless (typically 0).
594138568Ssam	 */
595138568Ssam	wh = mtod(m, struct ieee80211_frame *);
596178354Ssam	m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
597138568Ssam	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
598167432Ssam	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
599178354Ssam		k = &vap->iv_nw_keys[keyid >> 6];
600138568Ssam	else
601138568Ssam		k = &ni->ni_ucastkey;
602138568Ssam
603138568Ssam	/*
604138568Ssam	 * Insure crypto header is contiguous for all decap work.
605138568Ssam	 */
606138568Ssam	cip = k->wk_cipher;
607138568Ssam	if (m->m_len < hdrlen + cip->ic_header &&
608138568Ssam	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
609178354Ssam		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
610178354Ssam		    "unable to pullup %s header", cip->ic_name);
611178354Ssam		vap->iv_stats.is_rx_wepfail++;	/* XXX */
612170530Ssam		return NULL;
613138568Ssam	}
614138568Ssam
615147252Ssam	return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
616138568Ssam#undef IEEE80211_WEP_MINLEN
617138568Ssam#undef IEEE80211_WEP_HDRLEN
618116742Ssam}
619179396Ssam
620179396Ssamstatic void
621179396Ssamload_ucastkey(void *arg, struct ieee80211_node *ni)
622179396Ssam{
623179396Ssam	struct ieee80211vap *vap = ni->ni_vap;
624179396Ssam	struct ieee80211_key *k;
625179396Ssam
626179396Ssam	if (vap->iv_state != IEEE80211_S_RUN)
627179396Ssam		return;
628179396Ssam	k = &ni->ni_ucastkey;
629183248Ssam	if (k->wk_flags & IEEE80211_KEY_DEVKEY)
630179396Ssam		dev_key_set(vap, k);
631179396Ssam}
632179396Ssam
633179396Ssam/*
634179396Ssam * Re-load all keys known to the 802.11 layer that may
635179396Ssam * have hardware state backing them.  This is used by
636179396Ssam * drivers on resume to push keys down into the device.
637179396Ssam */
638179396Ssamvoid
639179396Ssamieee80211_crypto_reload_keys(struct ieee80211com *ic)
640179396Ssam{
641179396Ssam	struct ieee80211vap *vap;
642179396Ssam	int i;
643179396Ssam
644179396Ssam	/*
645179396Ssam	 * Keys in the global key table of each vap.
646179396Ssam	 */
647179396Ssam	/* NB: used only during resume so don't lock for now */
648179396Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
649179396Ssam		if (vap->iv_state != IEEE80211_S_RUN)
650179396Ssam			continue;
651179396Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
652179396Ssam			const struct ieee80211_key *k = &vap->iv_nw_keys[i];
653183248Ssam			if (k->wk_flags & IEEE80211_KEY_DEVKEY)
654179396Ssam				dev_key_set(vap, k);
655179396Ssam		}
656179396Ssam	}
657179396Ssam	/*
658179396Ssam	 * Unicast keys.
659179396Ssam	 */
660179396Ssam	ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
661179396Ssam}
662