1 /*
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2005 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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.10.2.2 2005/09/03 22:40:02 sam Exp $
33  * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_crypto.c,v 1.6 2007/05/07 14:12:16 sephe Exp $
34  */
35 
36 /*
37  * IEEE 802.11 generic crypto support.
38  */
39 #include <sys/param.h>
40 #include <sys/mbuf.h>
41 
42 #include <sys/socket.h>
43 
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
48 
49 #include <netproto/802_11/ieee80211_var.h>
50 
51 /*
52  * Table of registered cipher modules.
53  */
54 static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
55 
56 static	int _ieee80211_crypto_delkey(struct ieee80211com *,
57 		struct ieee80211_key *);
58 
59 /*
60  * Default "null" key management routines.
61  */
62 static int
63 null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
64 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
65 {
66 	if (!(&ic->ic_nw_keys[0] <= k &&
67 	     k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
68 		/*
69 		 * Not in the global key table, the driver should handle this
70 		 * by allocating a slot in the h/w key table/cache.  In
71 		 * lieu of that return key slot 0 for any unicast key
72 		 * request.  We disallow the request if this is a group key.
73 		 * This default policy does the right thing for legacy hardware
74 		 * with a 4 key table.  It also handles devices that pass
75 		 * packets through untouched when marked with the WEP bit
76 		 * and key index 0.
77 		 */
78 		if (k->wk_flags & IEEE80211_KEY_GROUP)
79 			return 0;
80 		*keyix = 0;	/* NB: use key index 0 for ucast key */
81 	} else {
82 		*keyix = k - ic->ic_nw_keys;
83 	}
84 	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
85 	return 1;
86 }
87 
88 static int
89 null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
90 {
91 	return 1;
92 }
93 
94 static int
95 null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
96 	     const uint8_t mac[IEEE80211_ADDR_LEN])
97 {
98 	return 1;
99 }
100 
101 static void null_key_update(struct ieee80211com *ic)
102 {
103 }
104 
105 /*
106  * Write-arounds for common operations.
107  */
108 static __inline void
109 cipher_detach(struct ieee80211_key *key)
110 {
111 	key->wk_cipher->ic_detach(key);
112 }
113 
114 static __inline void *
115 cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key)
116 {
117 	return key->wk_cipher->ic_attach(ic, key);
118 }
119 
120 /*
121  * Wrappers for driver key management methods.
122  */
123 static __inline int
124 dev_key_alloc(struct ieee80211com *ic,
125 	const struct ieee80211_key *key,
126 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
127 {
128 	return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix);
129 }
130 
131 static __inline int
132 dev_key_delete(struct ieee80211com *ic,
133 	const struct ieee80211_key *key)
134 {
135 	return ic->ic_crypto.cs_key_delete(ic, key);
136 }
137 
138 static __inline int
139 dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
140 	const uint8_t mac[IEEE80211_ADDR_LEN])
141 {
142 	return ic->ic_crypto.cs_key_set(ic, key, mac);
143 }
144 
145 /*
146  * Setup crypto support.
147  */
148 void
149 ieee80211_crypto_attach(struct ieee80211com *ic)
150 {
151 	struct ieee80211_crypto_state *cs = &ic->ic_crypto;
152 	int i;
153 
154 	/* NB: we assume everything is pre-zero'd */
155 	cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
156 	cs->cs_max_keyix = IEEE80211_WEP_NKID;
157 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
158 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
159 		ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
160 			IEEE80211_KEYIX_NONE);
161 	/*
162 	 * Initialize the driver key support routines to noop entries.
163 	 * This is useful especially for the cipher test modules.
164 	 */
165 	cs->cs_key_alloc = null_key_alloc;
166 	cs->cs_key_set = null_key_set;
167 	cs->cs_key_delete = null_key_delete;
168 	cs->cs_key_update_begin = null_key_update;
169 	cs->cs_key_update_end = null_key_update;
170 }
171 
172 /*
173  * Teardown crypto support.
174  */
175 void
176 ieee80211_crypto_detach(struct ieee80211com *ic)
177 {
178 	ieee80211_crypto_delglobalkeys(ic);
179 }
180 
181 /*
182  * Register a crypto cipher module.
183  */
184 void
185 ieee80211_crypto_register(const struct ieee80211_cipher *cip)
186 {
187 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
188 		kprintf("%s: cipher %s has an invalid cipher index %u\n",
189 			__func__, cip->ic_name, cip->ic_cipher);
190 		return;
191 	}
192 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
193 		kprintf("%s: cipher %s registered with a different template\n",
194 			__func__, cip->ic_name);
195 		return;
196 	}
197 	ciphers[cip->ic_cipher] = cip;
198 }
199 
200 /*
201  * Unregister a crypto cipher module.
202  */
203 void
204 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
205 {
206 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
207 		kprintf("%s: cipher %s has an invalid cipher index %u\n",
208 			__func__, cip->ic_name, cip->ic_cipher);
209 		return;
210 	}
211 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
212 		kprintf("%s: cipher %s registered with a different template\n",
213 			__func__, cip->ic_name);
214 		return;
215 	}
216 	/* NB: don't complain about not being registered */
217 	/* XXX disallow if references */
218 	ciphers[cip->ic_cipher] = NULL;
219 }
220 
221 int
222 ieee80211_crypto_available(u_int cipher)
223 {
224 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
225 }
226 
227 const struct ieee80211_cipher *
228 ieee80211_crypto_cipher(u_int cipher)
229 {
230 	return cipher < IEEE80211_CIPHER_MAX ? ciphers[cipher] : NULL;
231 }
232 
233 /*
234  * Reset key state to an unused state.  The crypto
235  * key allocation mechanism insures other state (e.g.
236  * key data) is properly setup before a key is used.
237  */
238 void
239 ieee80211_crypto_resetkey(struct ieee80211com *ic,
240 	struct ieee80211_key *k, ieee80211_keyix ix)
241 {
242 	if (k < &ic->ic_nw_keys[IEEE80211_WEP_NKID] &&
243 	    k >= &ic->ic_nw_keys[0])
244 		k->wk_keyid = k - ic->ic_nw_keys;
245 	else
246 		k->wk_keyid = 0;
247 
248 	k->wk_cipher = &ieee80211_cipher_none;
249 	k->wk_private = k->wk_cipher->ic_attach(ic, k);
250 	k->wk_keyix = k->wk_rxkeyix = ix;
251 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
252 }
253 
254 /* XXX well-known names! */
255 static const char *cipher_modnames[] = {
256 	"wlan_wep",	/* IEEE80211_CIPHER_WEP */
257 	"wlan_tkip",	/* IEEE80211_CIPHER_TKIP */
258 	"wlan_aes_ocb",	/* IEEE80211_CIPHER_AES_OCB */
259 	"wlan_ccmp",	/* IEEE80211_CIPHER_AES_CCM */
260 	"wlan_ckip",	/* IEEE80211_CIPHER_CKIP */
261 };
262 
263 /*
264  * Establish a relationship between the specified key and cipher
265  * and, if necessary, allocate a hardware index from the driver.
266  * Note that when a fixed key index is required it must be specified
267  * and we blindly assign it w/o consulting the driver (XXX).
268  *
269  * This must be the first call applied to a key; all the other key
270  * routines assume wk_cipher is setup.
271  *
272  * Locking must be handled by the caller using:
273  *	ieee80211_key_update_begin(ic);
274  *	ieee80211_key_update_end(ic);
275  */
276 int
277 ieee80211_crypto_newkey(struct ieee80211com *ic,
278 	int cipher, int flags, struct ieee80211_key *key)
279 {
280 #define	N(a)	(sizeof(a) / sizeof(a[0]))
281 	const struct ieee80211_cipher *cip;
282 	ieee80211_keyix keyix, rxkeyix;
283 	void *keyctx;
284 	int oflags;
285 
286 	/*
287 	 * Validate cipher and set reference to cipher routines.
288 	 */
289 	if (cipher >= IEEE80211_CIPHER_MAX) {
290 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
291 			"%s: invalid cipher %u\n", __func__, cipher);
292 		ic->ic_stats.is_crypto_badcipher++;
293 		return 0;
294 	}
295 	cip = ciphers[cipher];
296 	if (cip == NULL) {
297 		/*
298 		 * Auto-load cipher module if we have a well-known name
299 		 * for it.  It might be better to use string names rather
300 		 * than numbers and craft a module name based on the cipher
301 		 * name; e.g. wlan_cipher_<cipher-name>.
302 		 */
303 		if (cipher < N(cipher_modnames)) {
304 			IEEE80211_DPRINTF(ic, 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 		}
315 		if (cip == NULL) {
316 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
317 				"%s: unable to load cipher %u, module %s\n",
318 				__func__, cipher,
319 				cipher < N(cipher_modnames) ?
320 					cipher_modnames[cipher] : "<unknown>");
321 			ic->ic_stats.is_crypto_nocipher++;
322 			return 0;
323 		}
324 	}
325 
326 	oflags = key->wk_flags;
327 	flags &= IEEE80211_KEY_COMMON;
328 	/*
329 	 * If the hardware does not support the cipher then
330 	 * fallback to a host-based implementation.
331 	 */
332 	if ((ic->ic_caps & (1<<cipher)) == 0) {
333 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
334 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
335 		    __func__, cip->ic_name);
336 		flags |= IEEE80211_KEY_SWCRYPT;
337 	} else if (ic->ic_caps_ext & IEEE80211_CEXT_CRYPTO_HDR) {
338 		flags |= IEEE80211_KEY_NOHDR;
339 	}
340 	/*
341 	 * Hardware TKIP with software MIC is an important
342 	 * combination; we handle it by flagging each key,
343 	 * the cipher modules honor it.
344 	 */
345 	if (cipher == IEEE80211_CIPHER_TKIP &&
346 	    (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
347 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
348 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
349 		    __func__);
350 		flags |= IEEE80211_KEY_SWMIC;
351 	} else if (ic->ic_caps_ext & IEEE80211_CEXT_STRIP_MIC) {
352 		flags |= IEEE80211_KEY_NOMIC;
353 	}
354 
355 	/*
356 	 * Bind cipher to key instance.  Note we do this
357 	 * after checking the device capabilities so the
358 	 * cipher module can optimize space usage based on
359 	 * whether or not it needs to do the cipher work.
360 	 */
361 	if (key->wk_cipher != cip || key->wk_flags != flags) {
362 again:
363 		/*
364 		 * Fillin the flags so cipher modules can see s/w
365 		 * crypto requirements and potentially allocate
366 		 * different state and/or attach different method
367 		 * pointers.
368 		 *
369 		 * XXX this is not right when s/w crypto fallback
370 		 *     fails and we try to restore previous state.
371 		 */
372 		key->wk_flags = flags;
373 		keyctx = cip->ic_attach(ic, key); /* attach new cipher */
374 		if (keyctx == NULL) {
375 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
376 				"%s: unable to attach cipher %s\n",
377 				__func__, cip->ic_name);
378 			key->wk_flags = oflags;	/* restore old flags */
379 			ic->ic_stats.is_crypto_attachfail++;
380 			return 0;
381 		}
382 		cipher_detach(key);		/* detach old cipher */
383 		key->wk_cipher = cip;		/* XXX refcnt? */
384 		key->wk_private = keyctx;
385 	}
386 	/*
387 	 * Commit to requested usage so driver can see the flags.
388 	 */
389 	key->wk_flags = flags;
390 
391 	/*
392 	 * Ask the driver for a key index if we don't have one.
393 	 * Note that entries in the global key table always have
394 	 * an index; this means it's safe to call this routine
395 	 * for these entries just to setup the reference to the
396 	 * cipher template.  Note also that when using software
397 	 * crypto we also call the driver to give us a key index.
398 	 */
399 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
400 		if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) {
401 			/*
402 			 * Driver has no room; fallback to doing crypto
403 			 * in the host.  We change the flags and start the
404 			 * procedure over.  If we get back here then there's
405 			 * no hope and we bail.  Note that this can leave
406 			 * the key in a inconsistent state if the caller
407 			 * continues to use it.
408 			 */
409 			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
410 				ic->ic_stats.is_crypto_swfallback++;
411 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
412 				    "%s: no h/w resources for cipher %s, "
413 				    "falling back to s/w\n", __func__,
414 				    cip->ic_name);
415 				oflags = key->wk_flags;
416 				flags &= IEEE80211_KEY_COMMON;
417 				flags |= IEEE80211_KEY_SWCRYPT;
418 				if (cipher == IEEE80211_CIPHER_TKIP)
419 					flags |= IEEE80211_KEY_SWMIC;
420 				goto again;
421 			}
422 			ic->ic_stats.is_crypto_keyfail++;
423 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
424 			    "%s: unable to setup cipher %s\n",
425 			    __func__, cip->ic_name);
426 			return 0;
427 		}
428 		key->wk_keyix = keyix;
429 		key->wk_rxkeyix = rxkeyix;
430 	}
431 	return 1;
432 #undef N
433 }
434 
435 /*
436  * Remove the key (no locking, for internal use).
437  */
438 static int
439 _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
440 {
441 	ieee80211_keyix keyix;
442 
443 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
444 
445 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
446 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
447 	    __func__, key->wk_cipher->ic_name,
448 	    key->wk_keyix, key->wk_flags,
449 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
450 
451 	keyix = key->wk_keyix;
452 	if (keyix != IEEE80211_KEYIX_NONE) {
453 		/*
454 		 * Remove hardware entry.
455 		 */
456 		/* XXX key cache */
457 		if (!dev_key_delete(ic, key)) {
458 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
459 			    "%s: driver did not delete key index %u\n",
460 			    __func__, keyix);
461 			ic->ic_stats.is_crypto_delkey++;
462 			/* XXX recovery? */
463 		}
464 	}
465 	cipher_detach(key);
466 	memset(key, 0, sizeof(*key));
467 	ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
468 	return 1;
469 }
470 
471 /*
472  * Remove the specified key.
473  */
474 int
475 ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
476 {
477 	int status;
478 
479 	ieee80211_key_update_begin(ic);
480 	status = _ieee80211_crypto_delkey(ic, key);
481 	ieee80211_key_update_end(ic);
482 	return status;
483 }
484 
485 /*
486  * Clear the global key table.
487  */
488 void
489 ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
490 {
491 	int i;
492 
493 	ieee80211_key_update_begin(ic);
494 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
495 		_ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
496 	ieee80211_key_update_end(ic);
497 }
498 
499 /*
500  * Set the contents of the specified key.
501  *
502  * Locking must be handled by the caller using:
503  *	ieee80211_key_update_begin(ic);
504  *	ieee80211_key_update_end(ic);
505  */
506 int
507 ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
508 		const uint8_t macaddr[IEEE80211_ADDR_LEN])
509 {
510 	const struct ieee80211_cipher *cip = key->wk_cipher;
511 
512 	KASSERT(cip != NULL, ("No cipher!"));
513 
514 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
515 	    "%s: %s keyix %u flags 0x%x mac %6D rsc %ju tsc %ju len %u\n",
516 	    __func__, cip->ic_name, key->wk_keyix,
517 	    key->wk_flags, macaddr, ":",
518 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
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(ic, 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 		ic->ic_stats.is_crypto_setkey_cipher++;
530 		return 0;
531 	}
532 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
533 		/* XXX nothing allocated, should not happen */
534 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
535 		    "%s: no key index; should not happen!\n", __func__);
536 		ic->ic_stats.is_crypto_setkey_nokey++;
537 		return 0;
538 	}
539 	return dev_key_set(ic, key, macaddr);
540 }
541 
542 /*
543  * Add privacy headers appropriate for the specified key.
544  */
545 struct ieee80211_key *
546 ieee80211_crypto_encap(struct ieee80211com *ic,
547 	struct ieee80211_node *ni, struct mbuf *m)
548 {
549 	struct ieee80211_key *k;
550 
551 	k = ieee80211_crypto_findkey(ic, ni, m);
552 	if (k != NULL)
553 		k = ieee80211_crypto_encap_withkey(ic, m, k);
554 	return k;
555 }
556 
557 struct ieee80211_key *
558 ieee80211_crypto_findkey(struct ieee80211com *ic,
559 	struct ieee80211_node *ni, struct mbuf *m)
560 {
561 	struct ieee80211_frame *wh;
562 	struct ieee80211_key *k;
563 
564 	/*
565 	 * Multicast traffic always uses the multicast key.
566 	 * Otherwise if a unicast key is set we use that and
567 	 * it is always key index 0.  When no unicast key is
568 	 * set we fall back to the default transmit key.
569 	 */
570 	wh = mtod(m, struct ieee80211_frame *);
571 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
572 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
573 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
574 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
575 			    "[%6D] no default transmit key (%s) deftxkey %u\n",
576 			    wh->i_addr1, ":", __func__,
577 			    ic->ic_def_txkey);
578 			ic->ic_stats.is_tx_nodefkey++;
579 			return NULL;
580 		}
581 		k = &ic->ic_nw_keys[ic->ic_def_txkey];
582 		KASSERT(k->wk_keyid == ic->ic_def_txkey,
583 			("keyid mismatch: wk_keyid %d, def_txkey %d\n",
584 			 k->wk_keyid, ic->ic_def_txkey));
585 	} else {
586 		k = &ni->ni_ucastkey;
587 		KASSERT(k->wk_keyid == 0, ("unicast key keyid is not zero\n"));
588 	}
589 	return k;
590 }
591 
592 struct ieee80211_key *
593 ieee80211_crypto_encap_withkey(struct ieee80211com *ic,
594 	struct mbuf *m, struct ieee80211_key *k)
595 {
596 	return (k->wk_cipher->ic_encap(k, m, k->wk_keyid << 6) ? k : NULL);
597 }
598 
599 struct ieee80211_key *
600 ieee80211_crypto_getiv(struct ieee80211com *ic, struct ieee80211_crypto_iv *iv,
601 		       struct ieee80211_key *k)
602 {
603 	memset(iv, 0, sizeof(*iv));
604 	return (k->wk_cipher->ic_getiv(k, iv, k->wk_keyid << 6) ? k : NULL);
605 }
606 
607 /*
608  * Validate and strip privacy headers (and trailer) for a
609  * received frame that has the WEP/Privacy bit set.
610  */
611 struct ieee80211_key *
612 ieee80211_crypto_decap(struct ieee80211com *ic,
613 	struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
614 {
615 #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
616 #define	IEEE80211_WEP_MINLEN \
617 	(sizeof(struct ieee80211_frame) + \
618 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
619 	struct ieee80211_key *k;
620 	struct ieee80211_frame *wh;
621 	const struct ieee80211_cipher *cip;
622 	const uint8_t *ivp;
623 	uint8_t keyid;
624 
625 	/* NB: this minimum size data frame could be bigger */
626 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
627 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
628 			"%s: WEP data frame too short, len %u\n",
629 			__func__, m->m_pkthdr.len);
630 		ic->ic_stats.is_rx_tooshort++;	/* XXX need unique stat? */
631 		return NULL;
632 	}
633 
634 	/*
635 	 * Locate the key. If unicast and there is no unicast
636 	 * key then we fall back to the key id in the header.
637 	 * This assumes unicast keys are only configured when
638 	 * the key id in the header is meaningless (typically 0).
639 	 */
640 	wh = mtod(m, struct ieee80211_frame *);
641 	ivp = mtod(m, const uint8_t *) + hdrlen;	/* XXX contig */
642 	keyid = ivp[IEEE80211_WEP_IVLEN];
643 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
644 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)
645 		k = &ic->ic_nw_keys[keyid >> 6];
646 	else
647 		k = &ni->ni_ucastkey;
648 
649 	/*
650 	 * Insure crypto header is contiguous for all decap work.
651 	 */
652 	cip = k->wk_cipher;
653 	if (m->m_len < hdrlen + cip->ic_header &&
654 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
655 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
656 		    "[%6D] unable to pullup %s header\n",
657 		    wh->i_addr2, ":", cip->ic_name);
658 		ic->ic_stats.is_rx_wepfail++;	/* XXX */
659 		return 0;
660 	}
661 
662 	return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
663 #undef IEEE80211_WEP_MINLEN
664 #undef IEEE80211_WEP_HDRLEN
665 }
666 
667 struct ieee80211_key *
668 ieee80211_crypto_update(struct ieee80211com *ic, struct ieee80211_node *ni,
669 	const struct ieee80211_crypto_iv *iv, const struct ieee80211_frame *wh)
670 {
671 	struct ieee80211_key *k;
672 
673 	/*
674 	 * Locate the key. If unicast and there is no unicast
675 	 * key then we fall back to the key id in the header.
676 	 * This assumes unicast keys are only configured when
677 	 * the key id in the header is meaningless (typically 0).
678 	 */
679 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
680 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
681 		const uint8_t *ivp;
682 		uint8_t keyid;
683 
684 		ivp = (const uint8_t *)iv;
685 		keyid = ivp[IEEE80211_WEP_IVLEN];
686 		k = &ic->ic_nw_keys[keyid >> 6];
687 	} else {
688 		k = &ni->ni_ucastkey;
689 	}
690 	return (k->wk_cipher->ic_update(k, iv, wh) ? k : NULL);
691 }
692