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