2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Driver for the Atheros Wireless LAN controller.
38 * This software is derived from work of Atsushi Onoe; his contribution
39 * is greatly appreciated.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/errno.h>
57 #include <sys/callout.h>
59 #include <sys/endian.h>
60 #include <sys/kthread.h>
61 #include <sys/taskqueue.h>
64 #include <machine/bus.h>
67 #include <net/if_var.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_types.h>
71 #include <net/if_arp.h>
72 #include <net/ethernet.h>
73 #include <net/if_llc.h>
75 #include <net80211/ieee80211_var.h>
79 #include <dev/ath/if_athvar.h>
81 #include <dev/ath/if_ath_debug.h>
82 #include <dev/ath/if_ath_keycache.h>
83 #include <dev/ath/if_ath_misc.h>
87 ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix,
88 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
90 static const char *ciphers[] = {
100 printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
101 for (i = 0, n = hk->kv_len; i < n; i++)
102 printf("%02x", hk->kv_val[i]);
103 printf(" mac %s", ether_sprintf(mac));
104 if (hk->kv_type == HAL_CIPHER_TKIP) {
105 printf(" %s ", sc->sc_splitmic ? "mic" : "rxmic");
106 for (i = 0; i < sizeof(hk->kv_mic); i++)
107 printf("%02x", hk->kv_mic[i]);
108 if (!sc->sc_splitmic) {
110 for (i = 0; i < sizeof(hk->kv_txmic); i++)
111 printf("%02x", hk->kv_txmic[i]);
119 * Set a TKIP key into the hardware. This handles the
120 * potential distribution of key state to multiple key
121 * cache slots for TKIP.
124 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
125 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
127 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
128 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
129 struct ath_hal *ah = sc->sc_ah;
131 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
132 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
133 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
134 if (sc->sc_splitmic) {
136 * TX key goes at first index, RX key at the rx index.
137 * The hal handles the MIC keys at index+64.
139 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
140 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
141 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
144 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
145 KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
146 /* XXX delete tx key on failure? */
147 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
150 * Room for both TX+RX MIC keys in one key cache
151 * slot, just set key at the first index; the hal
152 * will handle the rest.
154 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
155 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
156 KEYPRINTF(sc, k->wk_keyix, hk, mac);
157 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
159 } else if (k->wk_flags & IEEE80211_KEY_XMIT) {
160 if (sc->sc_splitmic) {
162 * NB: must pass MIC key in expected location when
163 * the keycache only holds one MIC key per entry.
165 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_txmic));
167 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
168 KEYPRINTF(sc, k->wk_keyix, hk, mac);
169 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
170 } else if (k->wk_flags & IEEE80211_KEY_RECV) {
171 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
172 KEYPRINTF(sc, k->wk_keyix, hk, mac);
173 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
176 #undef IEEE80211_KEY_XR
180 * Set a net80211 key into the hardware. This handles the
181 * potential distribution of key state to multiple key
182 * cache slots for TKIP with hardware MIC support.
185 ath_keyset(struct ath_softc *sc, struct ieee80211vap *vap,
186 const struct ieee80211_key *k,
187 struct ieee80211_node *bss)
189 static const u_int8_t ciphermap[] = {
190 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */
191 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */
192 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */
193 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */
194 (u_int8_t) -1, /* 4 is not allocated */
195 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */
196 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */
198 struct ath_hal *ah = sc->sc_ah;
199 const struct ieee80211_cipher *cip = k->wk_cipher;
200 u_int8_t gmac[IEEE80211_ADDR_LEN];
205 memset(&hk, 0, sizeof(hk));
207 * Software crypto uses a "clear key" so non-crypto
208 * state kept in the key cache are maintained and
209 * so that rx frames have an entry to match.
211 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
212 KASSERT(cip->ic_cipher < nitems(ciphermap),
213 ("invalid cipher type %u", cip->ic_cipher));
214 hk.kv_type = ciphermap[cip->ic_cipher];
215 hk.kv_len = k->wk_keylen;
216 memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
218 hk.kv_type = HAL_CIPHER_CLR;
221 * If we're installing a clear cipher key and
222 * the hardware doesn't support that, just succeed.
223 * Leave it up to the net80211 layer to figure it out.
225 if (hk.kv_type == HAL_CIPHER_CLR && sc->sc_hasclrkey == 0) {
230 * XXX TODO: check this:
232 * Group keys on hardware that supports multicast frame
233 * key search should only be done in adhoc/hostap mode,
236 * XXX TODO: what about mesh, tdma?
239 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
240 vap->iv_opmode == IEEE80211_M_IBSS) &&
244 (k->wk_flags & IEEE80211_KEY_GROUP) &&
247 * Group keys on hardware that supports multicast frame
248 * key search use a MAC that is the sender's address with
249 * the multicast bit set instead of the app-specified address.
251 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
258 ath_power_set_power_state(sc, HAL_PM_AWAKE);
259 if (hk.kv_type == HAL_CIPHER_TKIP &&
260 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
261 ret = ath_keyset_tkip(sc, k, &hk, mac);
263 KEYPRINTF(sc, k->wk_keyix, &hk, mac);
264 ret = ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
266 ath_power_restore_power_state(sc);
273 * Allocate tx/rx key slots for TKIP. We allocate two slots for
274 * each key, one for decrypt/encrypt and the other for the MIC.
277 key_alloc_2pair(struct ath_softc *sc,
278 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
282 KASSERT(sc->sc_splitmic, ("key cache !split"));
283 /* XXX could optimize */
284 for (i = 0; i < nitems(sc->sc_keymap)/4; i++) {
285 u_int8_t b = sc->sc_keymap[i];
288 * One or more slots in this byte are free.
296 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
297 if (isset(sc->sc_keymap, keyix+32) ||
298 isset(sc->sc_keymap, keyix+64) ||
299 isset(sc->sc_keymap, keyix+32+64)) {
300 /* full pair unavailable */
302 if (keyix == (i+1)*NBBY) {
303 /* no slots were appropriate, advance */
308 setbit(sc->sc_keymap, keyix);
309 setbit(sc->sc_keymap, keyix+64);
310 setbit(sc->sc_keymap, keyix+32);
311 setbit(sc->sc_keymap, keyix+32+64);
312 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
313 "%s: key pair %u,%u %u,%u\n",
314 __func__, keyix, keyix+64,
315 keyix+32, keyix+32+64);
321 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
326 * Allocate tx/rx key slots for TKIP. We allocate two slots for
327 * each key, one for decrypt/encrypt and the other for the MIC.
330 key_alloc_pair(struct ath_softc *sc,
331 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
335 KASSERT(!sc->sc_splitmic, ("key cache split"));
336 /* XXX could optimize */
337 for (i = 0; i < nitems(sc->sc_keymap)/4; i++) {
338 u_int8_t b = sc->sc_keymap[i];
341 * One or more slots in this byte are free.
349 if (isset(sc->sc_keymap, keyix+64)) {
350 /* full pair unavailable */
352 if (keyix == (i+1)*NBBY) {
353 /* no slots were appropriate, advance */
358 setbit(sc->sc_keymap, keyix);
359 setbit(sc->sc_keymap, keyix+64);
360 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
361 "%s: key pair %u,%u\n",
362 __func__, keyix, keyix+64);
363 *txkeyix = *rxkeyix = keyix;
367 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
372 * Allocate a single key cache slot.
375 key_alloc_single(struct ath_softc *sc,
376 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
380 if (sc->sc_hasclrkey == 0) {
382 * Map to slot 0 for the AR5210.
384 *txkeyix = *rxkeyix = 0;
388 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
389 for (i = 0; i < nitems(sc->sc_keymap); i++) {
390 u_int8_t b = sc->sc_keymap[i];
393 * One or more slots are free.
398 setbit(sc->sc_keymap, keyix);
399 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
401 *txkeyix = *rxkeyix = keyix;
405 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
410 * Allocate one or more key cache slots for a uniacst key. The
411 * key itself is needed only to identify the cipher. For hardware
412 * TKIP with split cipher+MIC keys we allocate two key cache slot
413 * pairs so that we can setup separate TX and RX MIC keys. Note
414 * that the MIC key for a TKIP key at slot i is assumed by the
415 * hardware to be at slot i+64. This limits TKIP keys to the first
419 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
420 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
422 struct ath_softc *sc = vap->iv_ic->ic_softc;
425 * Group key allocation must be handled specially for
426 * parts that do not support multicast key cache search
427 * functionality. For those parts the key id must match
428 * the h/w key index so lookups find the right key. On
429 * parts w/ the key search facility we install the sender's
430 * mac address (with the high bit set) and let the hardware
431 * find the key w/o using the key id. This is preferred as
432 * it permits us to support multiple users for adhoc and/or
433 * multi-station operation.
435 if (k->wk_keyix != IEEE80211_KEYIX_NONE) {
437 * Only global keys should have key index assigned.
439 if (!(&vap->iv_nw_keys[0] <= k &&
440 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
441 /* should not happen */
442 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
443 "%s: bogus group key\n", __func__);
446 if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
447 !(k->wk_flags & IEEE80211_KEY_GROUP) ||
450 * XXX we pre-allocate the global keys so
451 * have no way to check if they've already
455 ieee80211_crypto_get_key_wepidx(vap, k);
459 * Group key and device supports multicast key search.
461 k->wk_keyix = IEEE80211_KEYIX_NONE;
465 * We allocate two pair for TKIP when using the h/w to do
466 * the MIC. For everything else, including software crypto,
467 * we allocate a single entry. Note that s/w crypto requires
468 * a pass-through slot on the 5211 and 5212. The 5210 does
469 * not support pass-through cache entries and we map all
470 * those requests to slot 0.
472 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
473 return key_alloc_single(sc, keyix, rxkeyix);
474 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
475 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
477 return key_alloc_2pair(sc, keyix, rxkeyix);
479 return key_alloc_pair(sc, keyix, rxkeyix);
481 return key_alloc_single(sc, keyix, rxkeyix);
486 * Delete an entry in the key cache allocated by ath_key_alloc.
489 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
491 struct ath_softc *sc = vap->iv_ic->ic_softc;
492 struct ath_hal *ah = sc->sc_ah;
493 const struct ieee80211_cipher *cip = k->wk_cipher;
494 u_int keyix = k->wk_keyix;
496 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
499 ath_power_set_power_state(sc, HAL_PM_AWAKE);
500 ath_hal_keyreset(ah, keyix);
502 * Handle split tx/rx keying required for TKIP with h/w MIC.
504 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
505 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
506 ath_hal_keyreset(ah, keyix+32); /* RX key */
507 if (keyix >= IEEE80211_WEP_NKID) {
509 * Don't touch keymap entries for global keys so
510 * they are never considered for dynamic allocation.
512 clrbit(sc->sc_keymap, keyix);
513 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
514 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
515 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */
516 if (sc->sc_splitmic) {
517 /* +32 for RX key, +32+64 for RX key MIC */
518 clrbit(sc->sc_keymap, keyix+32);
519 clrbit(sc->sc_keymap, keyix+32+64);
523 ath_power_restore_power_state(sc);
529 * Set the key cache contents for the specified key. Key cache
530 * slot(s) must already have been allocated by ath_key_alloc.
533 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
535 struct ath_softc *sc = vap->iv_ic->ic_softc;
537 return ath_keyset(sc, vap, k, vap->iv_bss);