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