xref: /freebsd/sys/dev/ath/if_ath_keycache.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * Driver for the Atheros Wireless LAN controller.
35  *
36  * This software is derived from work of Atsushi Onoe; his contribution
37  * is greatly appreciated.
38  */
39 
40 #include "opt_inet.h"
41 #include "opt_ath.h"
42 #include "opt_wlan.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
47 #include <sys/mbuf.h>
48 #include <sys/malloc.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/errno.h>
55 #include <sys/callout.h>
56 #include <sys/bus.h>
57 #include <sys/endian.h>
58 #include <sys/kthread.h>
59 #include <sys/taskqueue.h>
60 #include <sys/priv.h>
61 
62 #include <machine/bus.h>
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_types.h>
68 #include <net/if_arp.h>
69 #include <net/ethernet.h>
70 #include <net/if_llc.h>
71 
72 #include <net80211/ieee80211_var.h>
73 
74 #include <net/bpf.h>
75 
76 #include <dev/ath/if_athvar.h>
77 
78 #include <dev/ath/if_ath_debug.h>
79 #include <dev/ath/if_ath_keycache.h>
80 
81 #ifdef ATH_DEBUG
82 static void
83 ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix,
84 	const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
85 {
86 	static const char *ciphers[] = {
87 		"WEP",
88 		"AES-OCB",
89 		"AES-CCM",
90 		"CKIP",
91 		"TKIP",
92 		"CLR",
93 	};
94 	int i, n;
95 
96 	printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
97 	for (i = 0, n = hk->kv_len; i < n; i++)
98 		printf("%02x", hk->kv_val[i]);
99 	printf(" mac %s", ether_sprintf(mac));
100 	if (hk->kv_type == HAL_CIPHER_TKIP) {
101 		printf(" %s ", sc->sc_splitmic ? "mic" : "rxmic");
102 		for (i = 0; i < sizeof(hk->kv_mic); i++)
103 			printf("%02x", hk->kv_mic[i]);
104 		if (!sc->sc_splitmic) {
105 			printf(" txmic ");
106 			for (i = 0; i < sizeof(hk->kv_txmic); i++)
107 				printf("%02x", hk->kv_txmic[i]);
108 		}
109 	}
110 	printf("\n");
111 }
112 #endif
113 
114 /*
115  * Set a TKIP key into the hardware.  This handles the
116  * potential distribution of key state to multiple key
117  * cache slots for TKIP.
118  */
119 static int
120 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
121 	HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
122 {
123 #define	IEEE80211_KEY_XR	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
124 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
125 	struct ath_hal *ah = sc->sc_ah;
126 
127 	KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
128 		("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
129 	if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
130 		if (sc->sc_splitmic) {
131 			/*
132 			 * TX key goes at first index, RX key at the rx index.
133 			 * The hal handles the MIC keys at index+64.
134 			 */
135 			memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
136 			KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
137 			if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
138 				return 0;
139 
140 			memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
141 			KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
142 			/* XXX delete tx key on failure? */
143 			return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
144 		} else {
145 			/*
146 			 * Room for both TX+RX MIC keys in one key cache
147 			 * slot, just set key at the first index; the hal
148 			 * will handle the rest.
149 			 */
150 			memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
151 			memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
152 			KEYPRINTF(sc, k->wk_keyix, hk, mac);
153 			return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
154 		}
155 	} else if (k->wk_flags & IEEE80211_KEY_XMIT) {
156 		if (sc->sc_splitmic) {
157 			/*
158 			 * NB: must pass MIC key in expected location when
159 			 * the keycache only holds one MIC key per entry.
160 			 */
161 			memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_txmic));
162 		} else
163 			memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
164 		KEYPRINTF(sc, k->wk_keyix, hk, mac);
165 		return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
166 	} else if (k->wk_flags & IEEE80211_KEY_RECV) {
167 		memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
168 		KEYPRINTF(sc, k->wk_keyix, hk, mac);
169 		return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
170 	}
171 	return 0;
172 #undef IEEE80211_KEY_XR
173 }
174 
175 /*
176  * Set a net80211 key into the hardware.  This handles the
177  * potential distribution of key state to multiple key
178  * cache slots for TKIP with hardware MIC support.
179  */
180 int
181 ath_keyset(struct ath_softc *sc, struct ieee80211vap *vap,
182 	const struct ieee80211_key *k,
183 	struct ieee80211_node *bss)
184 {
185 #define	N(a)	(sizeof(a)/sizeof(a[0]))
186 	static const u_int8_t ciphermap[] = {
187 		HAL_CIPHER_WEP,		/* IEEE80211_CIPHER_WEP */
188 		HAL_CIPHER_TKIP,	/* IEEE80211_CIPHER_TKIP */
189 		HAL_CIPHER_AES_OCB,	/* IEEE80211_CIPHER_AES_OCB */
190 		HAL_CIPHER_AES_CCM,	/* IEEE80211_CIPHER_AES_CCM */
191 		(u_int8_t) -1,		/* 4 is not allocated */
192 		HAL_CIPHER_CKIP,	/* IEEE80211_CIPHER_CKIP */
193 		HAL_CIPHER_CLR,		/* IEEE80211_CIPHER_NONE */
194 	};
195 	struct ath_hal *ah = sc->sc_ah;
196 	const struct ieee80211_cipher *cip = k->wk_cipher;
197 	u_int8_t gmac[IEEE80211_ADDR_LEN];
198 	const u_int8_t *mac;
199 	HAL_KEYVAL hk;
200 
201 	memset(&hk, 0, sizeof(hk));
202 	/*
203 	 * Software crypto uses a "clear key" so non-crypto
204 	 * state kept in the key cache are maintained and
205 	 * so that rx frames have an entry to match.
206 	 */
207 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
208 		KASSERT(cip->ic_cipher < N(ciphermap),
209 			("invalid cipher type %u", cip->ic_cipher));
210 		hk.kv_type = ciphermap[cip->ic_cipher];
211 		hk.kv_len = k->wk_keylen;
212 		memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
213 	} else
214 		hk.kv_type = HAL_CIPHER_CLR;
215 
216 	/*
217 	 * If we're installing a clear cipher key and
218 	 * the hardware doesn't support that, just succeed.
219 	 * Leave it up to the net80211 layer to figure it out.
220 	 */
221 	if (hk.kv_type == HAL_CIPHER_CLR && sc->sc_hasclrkey == 0) {
222 		return (1);
223 	}
224 
225 	/*
226 	 * XXX TODO: check this:
227 	 *
228 	 * Group keys on hardware that supports multicast frame
229 	 * key search should only be done in adhoc/hostap mode,
230 	 * not STA mode.
231 	 *
232 	 * XXX TODO: what about mesh, tdma?
233 	 */
234 #if 0
235 	if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
236 	     vap->iv_opmode == IEEE80211_M_IBSS) &&
237 #else
238 	if (
239 #endif
240 	    (k->wk_flags & IEEE80211_KEY_GROUP) &&
241 	    sc->sc_mcastkey) {
242 		/*
243 		 * Group keys on hardware that supports multicast frame
244 		 * key search use a MAC that is the sender's address with
245 		 * the multicast bit set instead of the app-specified address.
246 		 */
247 		IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
248 		gmac[0] |= 0x01;
249 		mac = gmac;
250 	} else
251 		mac = k->wk_macaddr;
252 
253 	if (hk.kv_type == HAL_CIPHER_TKIP &&
254 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
255 		return ath_keyset_tkip(sc, k, &hk, mac);
256 	} else {
257 		KEYPRINTF(sc, k->wk_keyix, &hk, mac);
258 		return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
259 	}
260 #undef N
261 }
262 
263 /*
264  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
265  * each key, one for decrypt/encrypt and the other for the MIC.
266  */
267 static u_int16_t
268 key_alloc_2pair(struct ath_softc *sc,
269 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
270 {
271 #define	N(a)	(sizeof(a)/sizeof(a[0]))
272 	u_int i, keyix;
273 
274 	KASSERT(sc->sc_splitmic, ("key cache !split"));
275 	/* XXX could optimize */
276 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
277 		u_int8_t b = sc->sc_keymap[i];
278 		if (b != 0xff) {
279 			/*
280 			 * One or more slots in this byte are free.
281 			 */
282 			keyix = i*NBBY;
283 			while (b & 1) {
284 		again:
285 				keyix++;
286 				b >>= 1;
287 			}
288 			/* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
289 			if (isset(sc->sc_keymap, keyix+32) ||
290 			    isset(sc->sc_keymap, keyix+64) ||
291 			    isset(sc->sc_keymap, keyix+32+64)) {
292 				/* full pair unavailable */
293 				/* XXX statistic */
294 				if (keyix == (i+1)*NBBY) {
295 					/* no slots were appropriate, advance */
296 					continue;
297 				}
298 				goto again;
299 			}
300 			setbit(sc->sc_keymap, keyix);
301 			setbit(sc->sc_keymap, keyix+64);
302 			setbit(sc->sc_keymap, keyix+32);
303 			setbit(sc->sc_keymap, keyix+32+64);
304 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
305 				"%s: key pair %u,%u %u,%u\n",
306 				__func__, keyix, keyix+64,
307 				keyix+32, keyix+32+64);
308 			*txkeyix = keyix;
309 			*rxkeyix = keyix+32;
310 			return 1;
311 		}
312 	}
313 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
314 	return 0;
315 #undef N
316 }
317 
318 /*
319  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
320  * each key, one for decrypt/encrypt and the other for the MIC.
321  */
322 static u_int16_t
323 key_alloc_pair(struct ath_softc *sc,
324 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
325 {
326 #define	N(a)	(sizeof(a)/sizeof(a[0]))
327 	u_int i, keyix;
328 
329 	KASSERT(!sc->sc_splitmic, ("key cache split"));
330 	/* XXX could optimize */
331 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
332 		u_int8_t b = sc->sc_keymap[i];
333 		if (b != 0xff) {
334 			/*
335 			 * One or more slots in this byte are free.
336 			 */
337 			keyix = i*NBBY;
338 			while (b & 1) {
339 		again:
340 				keyix++;
341 				b >>= 1;
342 			}
343 			if (isset(sc->sc_keymap, keyix+64)) {
344 				/* full pair unavailable */
345 				/* XXX statistic */
346 				if (keyix == (i+1)*NBBY) {
347 					/* no slots were appropriate, advance */
348 					continue;
349 				}
350 				goto again;
351 			}
352 			setbit(sc->sc_keymap, keyix);
353 			setbit(sc->sc_keymap, keyix+64);
354 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
355 				"%s: key pair %u,%u\n",
356 				__func__, keyix, keyix+64);
357 			*txkeyix = *rxkeyix = keyix;
358 			return 1;
359 		}
360 	}
361 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
362 	return 0;
363 #undef N
364 }
365 
366 /*
367  * Allocate a single key cache slot.
368  */
369 static int
370 key_alloc_single(struct ath_softc *sc,
371 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
372 {
373 #define	N(a)	(sizeof(a)/sizeof(a[0]))
374 	u_int i, keyix;
375 
376 	if (sc->sc_hasclrkey == 0) {
377 		/*
378 		 * Map to slot 0 for the AR5210.
379 		 */
380 		*txkeyix = *rxkeyix = 0;
381 		return (1);
382 	}
383 
384 	/* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
385 	for (i = 0; i < N(sc->sc_keymap); i++) {
386 		u_int8_t b = sc->sc_keymap[i];
387 		if (b != 0xff) {
388 			/*
389 			 * One or more slots are free.
390 			 */
391 			keyix = i*NBBY;
392 			while (b & 1)
393 				keyix++, b >>= 1;
394 			setbit(sc->sc_keymap, keyix);
395 			DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
396 				__func__, keyix);
397 			*txkeyix = *rxkeyix = keyix;
398 			return 1;
399 		}
400 	}
401 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
402 	return 0;
403 #undef N
404 }
405 
406 /*
407  * Allocate one or more key cache slots for a uniacst key.  The
408  * key itself is needed only to identify the cipher.  For hardware
409  * TKIP with split cipher+MIC keys we allocate two key cache slot
410  * pairs so that we can setup separate TX and RX MIC keys.  Note
411  * that the MIC key for a TKIP key at slot i is assumed by the
412  * hardware to be at slot i+64.  This limits TKIP keys to the first
413  * 64 entries.
414  */
415 int
416 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
417 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
418 {
419 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
420 
421 	/*
422 	 * Group key allocation must be handled specially for
423 	 * parts that do not support multicast key cache search
424 	 * functionality.  For those parts the key id must match
425 	 * the h/w key index so lookups find the right key.  On
426 	 * parts w/ the key search facility we install the sender's
427 	 * mac address (with the high bit set) and let the hardware
428 	 * find the key w/o using the key id.  This is preferred as
429 	 * it permits us to support multiple users for adhoc and/or
430 	 * multi-station operation.
431 	 */
432 	if (k->wk_keyix != IEEE80211_KEYIX_NONE) {
433 		/*
434 		 * Only global keys should have key index assigned.
435 		 */
436 		if (!(&vap->iv_nw_keys[0] <= k &&
437 		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
438 			/* should not happen */
439 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
440 				"%s: bogus group key\n", __func__);
441 			return 0;
442 		}
443 		if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
444 		    !(k->wk_flags & IEEE80211_KEY_GROUP) ||
445 		    !sc->sc_mcastkey) {
446 			/*
447 			 * XXX we pre-allocate the global keys so
448 			 * have no way to check if they've already
449 			 * been allocated.
450 			 */
451 			*keyix = *rxkeyix = k - vap->iv_nw_keys;
452 			return 1;
453 		}
454 		/*
455 		 * Group key and device supports multicast key search.
456 		 */
457 		k->wk_keyix = IEEE80211_KEYIX_NONE;
458 	}
459 
460 	/*
461 	 * We allocate two pair for TKIP when using the h/w to do
462 	 * the MIC.  For everything else, including software crypto,
463 	 * we allocate a single entry.  Note that s/w crypto requires
464 	 * a pass-through slot on the 5211 and 5212.  The 5210 does
465 	 * not support pass-through cache entries and we map all
466 	 * those requests to slot 0.
467 	 */
468 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
469 		return key_alloc_single(sc, keyix, rxkeyix);
470 	} else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
471 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
472 		if (sc->sc_splitmic)
473 			return key_alloc_2pair(sc, keyix, rxkeyix);
474 		else
475 			return key_alloc_pair(sc, keyix, rxkeyix);
476 	} else {
477 		return key_alloc_single(sc, keyix, rxkeyix);
478 	}
479 }
480 
481 /*
482  * Delete an entry in the key cache allocated by ath_key_alloc.
483  */
484 int
485 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
486 {
487 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
488 	struct ath_hal *ah = sc->sc_ah;
489 	const struct ieee80211_cipher *cip = k->wk_cipher;
490 	u_int keyix = k->wk_keyix;
491 
492 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
493 
494 	ath_hal_keyreset(ah, keyix);
495 	/*
496 	 * Handle split tx/rx keying required for TKIP with h/w MIC.
497 	 */
498 	if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
499 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
500 		ath_hal_keyreset(ah, keyix+32);		/* RX key */
501 	if (keyix >= IEEE80211_WEP_NKID) {
502 		/*
503 		 * Don't touch keymap entries for global keys so
504 		 * they are never considered for dynamic allocation.
505 		 */
506 		clrbit(sc->sc_keymap, keyix);
507 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
508 		    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
509 			clrbit(sc->sc_keymap, keyix+64);	/* TX key MIC */
510 			if (sc->sc_splitmic) {
511 				/* +32 for RX key, +32+64 for RX key MIC */
512 				clrbit(sc->sc_keymap, keyix+32);
513 				clrbit(sc->sc_keymap, keyix+32+64);
514 			}
515 		}
516 	}
517 	return 1;
518 }
519 
520 /*
521  * Set the key cache contents for the specified key.  Key cache
522  * slot(s) must already have been allocated by ath_key_alloc.
523  */
524 int
525 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
526 	const u_int8_t mac[IEEE80211_ADDR_LEN])
527 {
528 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
529 
530 	return ath_keyset(sc, vap, k, vap->iv_bss);
531 }
532