ieee80211_crypto.c revision 178354
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_crypto.c 178354 2008-04-20 20:35:46Z sam $");
29
30/*
31 * IEEE 802.11 generic crypto support.
32 */
33#include "opt_wlan.h"
34
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
39
40#include <sys/socket.h>
41
42#include <net/if.h>
43#include <net/if_media.h>
44#include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
45
46#include <net80211/ieee80211_var.h>
47
48MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
49
50static	int _ieee80211_crypto_delkey(struct ieee80211vap *,
51		struct ieee80211_key *);
52
53/*
54 * Table of registered cipher modules.
55 */
56static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
57
58/*
59 * Default "null" key management routines.
60 */
61static int
62null_key_alloc(struct ieee80211vap *vap, const struct ieee80211_key *k,
63	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
64{
65	if (!(&vap->iv_nw_keys[0] <= k &&
66	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
67		/*
68		 * Not in the global key table, the driver should handle this
69		 * by allocating a slot in the h/w key table/cache.  In
70		 * lieu of that return key slot 0 for any unicast key
71		 * request.  We disallow the request if this is a group key.
72		 * This default policy does the right thing for legacy hardware
73		 * with a 4 key table.  It also handles devices that pass
74		 * packets through untouched when marked with the WEP bit
75		 * and key index 0.
76		 */
77		if (k->wk_flags & IEEE80211_KEY_GROUP)
78			return 0;
79		*keyix = 0;	/* NB: use key index 0 for ucast key */
80	} else {
81		*keyix = k - vap->iv_nw_keys;
82	}
83	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
84	return 1;
85}
86static int
87null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
88{
89	return 1;
90}
91static 	int
92null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
93	const uint8_t mac[IEEE80211_ADDR_LEN])
94{
95	return 1;
96}
97static void null_key_update(struct ieee80211vap *vap) {}
98
99/*
100 * Write-arounds for common operations.
101 */
102static __inline void
103cipher_detach(struct ieee80211_key *key)
104{
105	key->wk_cipher->ic_detach(key);
106}
107
108static __inline void *
109cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
110{
111	return key->wk_cipher->ic_attach(vap, key);
112}
113
114/*
115 * Wrappers for driver key management methods.
116 */
117static __inline int
118dev_key_alloc(struct ieee80211vap *vap,
119	const struct ieee80211_key *key,
120	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
121{
122	return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
123}
124
125static __inline int
126dev_key_delete(struct ieee80211vap *vap,
127	const struct ieee80211_key *key)
128{
129	return vap->iv_key_delete(vap, key);
130}
131
132static __inline int
133dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key,
134	const uint8_t mac[IEEE80211_ADDR_LEN])
135{
136	return vap->iv_key_set(vap, key, mac);
137}
138
139/*
140 * Setup crypto support for a device/shared instance.
141 */
142void
143ieee80211_crypto_attach(struct ieee80211com *ic)
144{
145	/* NB: we assume everything is pre-zero'd */
146	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
147}
148
149/*
150 * Teardown crypto support.
151 */
152void
153ieee80211_crypto_detach(struct ieee80211com *ic)
154{
155}
156
157/*
158 * Setup crypto support for a vap.
159 */
160void
161ieee80211_crypto_vattach(struct ieee80211vap *vap)
162{
163	int i;
164
165	/* NB: we assume everything is pre-zero'd */
166	vap->iv_max_keyix = IEEE80211_WEP_NKID;
167	vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
168	for (i = 0; i < IEEE80211_WEP_NKID; i++)
169		ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
170			IEEE80211_KEYIX_NONE);
171	/*
172	 * Initialize the driver key support routines to noop entries.
173	 * This is useful especially for the cipher test modules.
174	 */
175	vap->iv_key_alloc = null_key_alloc;
176	vap->iv_key_set = null_key_set;
177	vap->iv_key_delete = null_key_delete;
178	vap->iv_key_update_begin = null_key_update;
179	vap->iv_key_update_end = null_key_update;
180}
181
182/*
183 * Teardown crypto support for a vap.
184 */
185void
186ieee80211_crypto_vdetach(struct ieee80211vap *vap)
187{
188	ieee80211_crypto_delglobalkeys(vap);
189}
190
191/*
192 * Register a crypto cipher module.
193 */
194void
195ieee80211_crypto_register(const struct ieee80211_cipher *cip)
196{
197	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
198		printf("%s: cipher %s has an invalid cipher index %u\n",
199			__func__, cip->ic_name, cip->ic_cipher);
200		return;
201	}
202	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
203		printf("%s: cipher %s registered with a different template\n",
204			__func__, cip->ic_name);
205		return;
206	}
207	ciphers[cip->ic_cipher] = cip;
208}
209
210/*
211 * Unregister a crypto cipher module.
212 */
213void
214ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
215{
216	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
217		printf("%s: cipher %s has an invalid cipher index %u\n",
218			__func__, cip->ic_name, cip->ic_cipher);
219		return;
220	}
221	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
222		printf("%s: cipher %s registered with a different template\n",
223			__func__, cip->ic_name);
224		return;
225	}
226	/* NB: don't complain about not being registered */
227	/* XXX disallow if references */
228	ciphers[cip->ic_cipher] = NULL;
229}
230
231int
232ieee80211_crypto_available(u_int cipher)
233{
234	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
235}
236
237/* XXX well-known names! */
238static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
239	"wlan_wep",	/* IEEE80211_CIPHER_WEP */
240	"wlan_tkip",	/* IEEE80211_CIPHER_TKIP */
241	"wlan_aes_ocb",	/* IEEE80211_CIPHER_AES_OCB */
242	"wlan_ccmp",	/* IEEE80211_CIPHER_AES_CCM */
243	"#4",		/* reserved */
244	"wlan_ckip",	/* IEEE80211_CIPHER_CKIP */
245	"wlan_none",	/* IEEE80211_CIPHER_NONE */
246};
247
248/*
249 * Establish a relationship between the specified key and cipher
250 * and, if necessary, allocate a hardware index from the driver.
251 * Note that when a fixed key index is required it must be specified
252 * and we blindly assign it w/o consulting the driver (XXX).
253 *
254 * This must be the first call applied to a key; all the other key
255 * routines assume wk_cipher is setup.
256 *
257 * Locking must be handled by the caller using:
258 *	ieee80211_key_update_begin(vap);
259 *	ieee80211_key_update_end(vap);
260 */
261int
262ieee80211_crypto_newkey(struct ieee80211vap *vap,
263	int cipher, int flags, struct ieee80211_key *key)
264{
265	struct ieee80211com *ic = vap->iv_ic;
266	const struct ieee80211_cipher *cip;
267	ieee80211_keyix keyix, rxkeyix;
268	void *keyctx;
269	int oflags;
270
271	/*
272	 * Validate cipher and set reference to cipher routines.
273	 */
274	if (cipher >= IEEE80211_CIPHER_MAX) {
275		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
276		    "%s: invalid cipher %u\n", __func__, cipher);
277		vap->iv_stats.is_crypto_badcipher++;
278		return 0;
279	}
280	cip = ciphers[cipher];
281	if (cip == NULL) {
282		/*
283		 * Auto-load cipher module if we have a well-known name
284		 * for it.  It might be better to use string names rather
285		 * than numbers and craft a module name based on the cipher
286		 * name; e.g. wlan_cipher_<cipher-name>.
287		 */
288		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
289		    "%s: unregistered cipher %u, load module %s\n",
290		    __func__, cipher, cipher_modnames[cipher]);
291		ieee80211_load_module(cipher_modnames[cipher]);
292		/*
293		 * If cipher module loaded it should immediately
294		 * call ieee80211_crypto_register which will fill
295		 * in the entry in the ciphers array.
296		 */
297		cip = ciphers[cipher];
298		if (cip == NULL) {
299			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
300			    "%s: unable to load cipher %u, module %s\n",
301			    __func__, cipher, cipher_modnames[cipher]);
302			vap->iv_stats.is_crypto_nocipher++;
303			return 0;
304		}
305	}
306
307	oflags = key->wk_flags;
308	flags &= IEEE80211_KEY_COMMON;
309	/*
310	 * If the hardware does not support the cipher then
311	 * fallback to a host-based implementation.
312	 */
313	if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
314		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
315		    "%s: no h/w support for cipher %s, falling back to s/w\n",
316		    __func__, cip->ic_name);
317		flags |= IEEE80211_KEY_SWCRYPT;
318	}
319	/*
320	 * Hardware TKIP with software MIC is an important
321	 * combination; we handle it by flagging each key,
322	 * the cipher modules honor it.
323	 */
324	if (cipher == IEEE80211_CIPHER_TKIP &&
325	    (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
326		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
327		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
328		    __func__);
329		flags |= IEEE80211_KEY_SWMIC;
330	}
331
332	/*
333	 * Bind cipher to key instance.  Note we do this
334	 * after checking the device capabilities so the
335	 * cipher module can optimize space usage based on
336	 * whether or not it needs to do the cipher work.
337	 */
338	if (key->wk_cipher != cip || key->wk_flags != flags) {
339again:
340		/*
341		 * Fillin the flags so cipher modules can see s/w
342		 * crypto requirements and potentially allocate
343		 * different state and/or attach different method
344		 * pointers.
345		 *
346		 * XXX this is not right when s/w crypto fallback
347		 *     fails and we try to restore previous state.
348		 */
349		key->wk_flags = flags;
350		keyctx = cip->ic_attach(vap, key);
351		if (keyctx == NULL) {
352			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
353				"%s: unable to attach cipher %s\n",
354				__func__, cip->ic_name);
355			key->wk_flags = oflags;	/* restore old flags */
356			vap->iv_stats.is_crypto_attachfail++;
357			return 0;
358		}
359		cipher_detach(key);
360		key->wk_cipher = cip;		/* XXX refcnt? */
361		key->wk_private = keyctx;
362	}
363	/*
364	 * Commit to requested usage so driver can see the flags.
365	 */
366	key->wk_flags = flags;
367
368	/*
369	 * Ask the driver for a key index if we don't have one.
370	 * Note that entries in the global key table always have
371	 * an index; this means it's safe to call this routine
372	 * for these entries just to setup the reference to the
373	 * cipher template.  Note also that when using software
374	 * crypto we also call the driver to give us a key index.
375	 */
376	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
377		if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
378			/*
379			 * Driver has no room; fallback to doing crypto
380			 * in the host.  We change the flags and start the
381			 * procedure over.  If we get back here then there's
382			 * no hope and we bail.  Note that this can leave
383			 * the key in a inconsistent state if the caller
384			 * continues to use it.
385			 */
386			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
387				vap->iv_stats.is_crypto_swfallback++;
388				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
389				    "%s: no h/w resources for cipher %s, "
390				    "falling back to s/w\n", __func__,
391				    cip->ic_name);
392				oflags = key->wk_flags;
393				flags |= IEEE80211_KEY_SWCRYPT;
394				if (cipher == IEEE80211_CIPHER_TKIP)
395					flags |= IEEE80211_KEY_SWMIC;
396				goto again;
397			}
398			vap->iv_stats.is_crypto_keyfail++;
399			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
400			    "%s: unable to setup cipher %s\n",
401			    __func__, cip->ic_name);
402			return 0;
403		}
404		key->wk_keyix = keyix;
405		key->wk_rxkeyix = rxkeyix;
406	}
407	return 1;
408}
409
410/*
411 * Remove the key (no locking, for internal use).
412 */
413static int
414_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
415{
416	ieee80211_keyix keyix;
417
418	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
419
420	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
421	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
422	    __func__, key->wk_cipher->ic_name,
423	    key->wk_keyix, key->wk_flags,
424	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
425	    key->wk_keylen);
426
427	keyix = key->wk_keyix;
428	if (keyix != IEEE80211_KEYIX_NONE) {
429		/*
430		 * Remove hardware entry.
431		 */
432		/* XXX key cache */
433		if (!dev_key_delete(vap, key)) {
434			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
435			    "%s: driver did not delete key index %u\n",
436			    __func__, keyix);
437			vap->iv_stats.is_crypto_delkey++;
438			/* XXX recovery? */
439		}
440	}
441	cipher_detach(key);
442	memset(key, 0, sizeof(*key));
443	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
444	return 1;
445}
446
447/*
448 * Remove the specified key.
449 */
450int
451ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
452{
453	int status;
454
455	ieee80211_key_update_begin(vap);
456	status = _ieee80211_crypto_delkey(vap, key);
457	ieee80211_key_update_end(vap);
458	return status;
459}
460
461/*
462 * Clear the global key table.
463 */
464void
465ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
466{
467	int i;
468
469	ieee80211_key_update_begin(vap);
470	for (i = 0; i < IEEE80211_WEP_NKID; i++)
471		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
472	ieee80211_key_update_end(vap);
473}
474
475/*
476 * Set the contents of the specified key.
477 *
478 * Locking must be handled by the caller using:
479 *	ieee80211_key_update_begin(vap);
480 *	ieee80211_key_update_end(vap);
481 */
482int
483ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key,
484		const uint8_t macaddr[IEEE80211_ADDR_LEN])
485{
486	const struct ieee80211_cipher *cip = key->wk_cipher;
487
488	KASSERT(cip != NULL, ("No cipher!"));
489
490	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
491	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
492	    __func__, cip->ic_name, key->wk_keyix,
493	    key->wk_flags, ether_sprintf(macaddr),
494	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
495	    key->wk_keylen);
496
497	/*
498	 * Give cipher a chance to validate key contents.
499	 * XXX should happen before modifying state.
500	 */
501	if (!cip->ic_setkey(key)) {
502		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
503		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
504		    __func__, cip->ic_name, key->wk_keyix,
505		    key->wk_keylen, key->wk_flags);
506		vap->iv_stats.is_crypto_setkey_cipher++;
507		return 0;
508	}
509	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
510		/* XXX nothing allocated, should not happen */
511		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
512		    "%s: no key index; should not happen!\n", __func__);
513		vap->iv_stats.is_crypto_setkey_nokey++;
514		return 0;
515	}
516	return dev_key_set(vap, key, macaddr);
517}
518
519/*
520 * Add privacy headers appropriate for the specified key.
521 */
522struct ieee80211_key *
523ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
524{
525	struct ieee80211vap *vap = ni->ni_vap;
526	struct ieee80211_key *k;
527	struct ieee80211_frame *wh;
528	const struct ieee80211_cipher *cip;
529	uint8_t keyid;
530
531	/*
532	 * Multicast traffic always uses the multicast key.
533	 * Otherwise if a unicast key is set we use that and
534	 * it is always key index 0.  When no unicast key is
535	 * set we fall back to the default transmit key.
536	 */
537	wh = mtod(m, struct ieee80211_frame *);
538	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
539	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
540		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
541			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
542			    wh->i_addr1,
543			    "no default transmit key (%s) deftxkey %u",
544			    __func__, vap->iv_def_txkey);
545			vap->iv_stats.is_tx_nodefkey++;
546			return NULL;
547		}
548		keyid = vap->iv_def_txkey;
549		k = &vap->iv_nw_keys[vap->iv_def_txkey];
550	} else {
551		keyid = 0;
552		k = &ni->ni_ucastkey;
553	}
554	cip = k->wk_cipher;
555	return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
556}
557
558/*
559 * Validate and strip privacy headers (and trailer) for a
560 * received frame that has the WEP/Privacy bit set.
561 */
562struct ieee80211_key *
563ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
564{
565#define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
566#define	IEEE80211_WEP_MINLEN \
567	(sizeof(struct ieee80211_frame) + \
568	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
569	struct ieee80211vap *vap = ni->ni_vap;
570	struct ieee80211_key *k;
571	struct ieee80211_frame *wh;
572	const struct ieee80211_cipher *cip;
573	uint8_t keyid;
574
575	/* NB: this minimum size data frame could be bigger */
576	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
577		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
578			"%s: WEP data frame too short, len %u\n",
579			__func__, m->m_pkthdr.len);
580		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */
581		return NULL;
582	}
583
584	/*
585	 * Locate the key. If unicast and there is no unicast
586	 * key then we fall back to the key id in the header.
587	 * This assumes unicast keys are only configured when
588	 * the key id in the header is meaningless (typically 0).
589	 */
590	wh = mtod(m, struct ieee80211_frame *);
591	m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
592	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
593	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
594		k = &vap->iv_nw_keys[keyid >> 6];
595	else
596		k = &ni->ni_ucastkey;
597
598	/*
599	 * Insure crypto header is contiguous for all decap work.
600	 */
601	cip = k->wk_cipher;
602	if (m->m_len < hdrlen + cip->ic_header &&
603	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
604		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
605		    "unable to pullup %s header", cip->ic_name);
606		vap->iv_stats.is_rx_wepfail++;	/* XXX */
607		return NULL;
608	}
609
610	return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
611#undef IEEE80211_WEP_MINLEN
612#undef IEEE80211_WEP_HDRLEN
613}
614