2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
33 * Driver for the Atheros Wireless LAN controller.
35 * This software is derived from work of Atsushi Onoe; his contribution
36 * is greatly appreciated.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/errno.h>
54 #include <sys/callout.h>
56 #include <sys/endian.h>
57 #include <sys/kthread.h>
58 #include <sys/taskqueue.h>
62 #include <net/if_var.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_llc.h>
70 #include <netproto/802_11/ieee80211_var.h>
74 #include <dev/netif/ath/ath/if_athvar.h>
76 #include <dev/netif/ath/ath/if_ath_debug.h>
77 #include <dev/netif/ath/ath/if_ath_keycache.h>
81 ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix,
82 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
84 static const char *ciphers[] = {
94 printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
95 for (i = 0, n = hk->kv_len; i < n; i++)
96 printf("%02x", hk->kv_val[i]);
97 printf(" mac %s", ether_sprintf(mac));
98 if (hk->kv_type == HAL_CIPHER_TKIP) {
99 printf(" %s ", sc->sc_splitmic ? "mic" : "rxmic");
100 for (i = 0; i < sizeof(hk->kv_mic); i++)
101 printf("%02x", hk->kv_mic[i]);
102 if (!sc->sc_splitmic) {
104 for (i = 0; i < sizeof(hk->kv_txmic); i++)
105 printf("%02x", hk->kv_txmic[i]);
113 * Set a TKIP key into the hardware. This handles the
114 * potential distribution of key state to multiple key
115 * cache slots for TKIP.
118 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
119 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
121 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
122 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
123 struct ath_hal *ah = sc->sc_ah;
125 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
126 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
127 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
128 if (sc->sc_splitmic) {
130 * TX key goes at first index, RX key at the rx index.
131 * The hal handles the MIC keys at index+64.
133 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
134 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
135 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
138 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
139 KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
140 /* XXX delete tx key on failure? */
141 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
144 * Room for both TX+RX MIC keys in one key cache
145 * slot, just set key at the first index; the hal
146 * will handle the rest.
148 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
149 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
150 KEYPRINTF(sc, k->wk_keyix, hk, mac);
151 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
153 } else if (k->wk_flags & IEEE80211_KEY_XMIT) {
154 if (sc->sc_splitmic) {
156 * NB: must pass MIC key in expected location when
157 * the keycache only holds one MIC key per entry.
159 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_txmic));
161 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
162 KEYPRINTF(sc, k->wk_keyix, hk, mac);
163 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
164 } else if (k->wk_flags & IEEE80211_KEY_RECV) {
165 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
166 KEYPRINTF(sc, k->wk_keyix, hk, mac);
167 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
170 #undef IEEE80211_KEY_XR
174 * Set a net80211 key into the hardware. This handles the
175 * potential distribution of key state to multiple key
176 * cache slots for TKIP with hardware MIC support.
179 ath_keyset(struct ath_softc *sc, struct ieee80211vap *vap,
180 const struct ieee80211_key *k,
181 struct ieee80211_node *bss)
183 #define N(a) (sizeof(a)/sizeof(a[0]))
184 static const u_int8_t ciphermap[] = {
185 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */
186 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */
187 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */
188 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */
189 (u_int8_t) -1, /* 4 is not allocated */
190 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */
191 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */
193 struct ath_hal *ah = sc->sc_ah;
194 const struct ieee80211_cipher *cip = k->wk_cipher;
195 u_int8_t gmac[IEEE80211_ADDR_LEN];
199 memset(&hk, 0, sizeof(hk));
201 * Software crypto uses a "clear key" so non-crypto
202 * state kept in the key cache are maintained and
203 * so that rx frames have an entry to match.
205 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
206 KASSERT(cip->ic_cipher < N(ciphermap),
207 ("invalid cipher type %u", cip->ic_cipher));
208 hk.kv_type = ciphermap[cip->ic_cipher];
209 hk.kv_len = k->wk_keylen;
210 memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
212 hk.kv_type = HAL_CIPHER_CLR;
215 * If we're installing a clear cipher key and
216 * the hardware doesn't support that, just succeed.
217 * Leave it up to the net80211 layer to figure it out.
219 if (hk.kv_type == HAL_CIPHER_CLR && sc->sc_hasclrkey == 0) {
224 * XXX TODO: check this:
226 * Group keys on hardware that supports multicast frame
227 * key search should only be done in adhoc/hostap mode,
230 * XXX TODO: what about mesh, tdma?
233 if ((vap->iv_opmode == IEEE80211_M_HOSTAP ||
234 vap->iv_opmode == IEEE80211_M_IBSS) &&
238 (k->wk_flags & IEEE80211_KEY_GROUP) &&
241 * Group keys on hardware that supports multicast frame
242 * key search use a MAC that is the sender's address with
243 * the multicast bit set instead of the app-specified address.
245 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
251 if (hk.kv_type == HAL_CIPHER_TKIP &&
252 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
253 return ath_keyset_tkip(sc, k, &hk, mac);
255 KEYPRINTF(sc, k->wk_keyix, &hk, mac);
256 return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
262 * Allocate tx/rx key slots for TKIP. We allocate two slots for
263 * each key, one for decrypt/encrypt and the other for the MIC.
266 key_alloc_2pair(struct ath_softc *sc,
267 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
269 #define N(a) (sizeof(a)/sizeof(a[0]))
272 KASSERT(sc->sc_splitmic, ("key cache !split"));
273 /* XXX could optimize */
274 for (i = 0; i < N(sc->sc_keymap)/4; i++) {
275 u_int8_t b = sc->sc_keymap[i];
278 * One or more slots in this byte are free.
286 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
287 if (isset(sc->sc_keymap, keyix+32) ||
288 isset(sc->sc_keymap, keyix+64) ||
289 isset(sc->sc_keymap, keyix+32+64)) {
290 /* full pair unavailable */
292 if (keyix == (i+1)*NBBY) {
293 /* no slots were appropriate, advance */
298 setbit(sc->sc_keymap, keyix);
299 setbit(sc->sc_keymap, keyix+64);
300 setbit(sc->sc_keymap, keyix+32);
301 setbit(sc->sc_keymap, keyix+32+64);
302 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
303 "%s: key pair %u,%u %u,%u\n",
304 __func__, keyix, keyix+64,
305 keyix+32, keyix+32+64);
311 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
317 * Allocate tx/rx key slots for TKIP. We allocate two slots for
318 * each key, one for decrypt/encrypt and the other for the MIC.
321 key_alloc_pair(struct ath_softc *sc,
322 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
324 #define N(a) (sizeof(a)/sizeof(a[0]))
327 KASSERT(!sc->sc_splitmic, ("key cache split"));
328 /* XXX could optimize */
329 for (i = 0; i < N(sc->sc_keymap)/4; i++) {
330 u_int8_t b = sc->sc_keymap[i];
333 * One or more slots in this byte are free.
341 if (isset(sc->sc_keymap, keyix+64)) {
342 /* full pair unavailable */
344 if (keyix == (i+1)*NBBY) {
345 /* no slots were appropriate, advance */
350 setbit(sc->sc_keymap, keyix);
351 setbit(sc->sc_keymap, keyix+64);
352 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
353 "%s: key pair %u,%u\n",
354 __func__, keyix, keyix+64);
355 *txkeyix = *rxkeyix = keyix;
359 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
365 * Allocate a single key cache slot.
368 key_alloc_single(struct ath_softc *sc,
369 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
371 #define N(a) (sizeof(a)/sizeof(a[0]))
374 if (sc->sc_hasclrkey == 0) {
376 * Map to slot 0 for the AR5210.
378 *txkeyix = *rxkeyix = 0;
382 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
383 for (i = 0; i < N(sc->sc_keymap); i++) {
384 u_int8_t b = sc->sc_keymap[i];
387 * One or more slots are free.
392 setbit(sc->sc_keymap, keyix);
393 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
395 *txkeyix = *rxkeyix = keyix;
399 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
405 * Allocate one or more key cache slots for a uniacst key. The
406 * key itself is needed only to identify the cipher. For hardware
407 * TKIP with split cipher+MIC keys we allocate two key cache slot
408 * pairs so that we can setup separate TX and RX MIC keys. Note
409 * that the MIC key for a TKIP key at slot i is assumed by the
410 * hardware to be at slot i+64. This limits TKIP keys to the first
414 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
415 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
417 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
420 * Group key allocation must be handled specially for
421 * parts that do not support multicast key cache search
422 * functionality. For those parts the key id must match
423 * the h/w key index so lookups find the right key. On
424 * parts w/ the key search facility we install the sender's
425 * mac address (with the high bit set) and let the hardware
426 * find the key w/o using the key id. This is preferred as
427 * it permits us to support multiple users for adhoc and/or
428 * multi-station operation.
430 if (k->wk_keyix != IEEE80211_KEYIX_NONE) {
432 * Only global keys should have key index assigned.
434 if (!(&vap->iv_nw_keys[0] <= k &&
435 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
436 /* should not happen */
437 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
438 "%s: bogus group key\n", __func__);
441 if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
442 !(k->wk_flags & IEEE80211_KEY_GROUP) ||
445 * XXX we pre-allocate the global keys so
446 * have no way to check if they've already
449 *keyix = *rxkeyix = k - vap->iv_nw_keys;
453 * Group key and device supports multicast key search.
455 k->wk_keyix = IEEE80211_KEYIX_NONE;
459 * We allocate two pair for TKIP when using the h/w to do
460 * the MIC. For everything else, including software crypto,
461 * we allocate a single entry. Note that s/w crypto requires
462 * a pass-through slot on the 5211 and 5212. The 5210 does
463 * not support pass-through cache entries and we map all
464 * those requests to slot 0.
466 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
467 return key_alloc_single(sc, keyix, rxkeyix);
468 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
469 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
471 return key_alloc_2pair(sc, keyix, rxkeyix);
473 return key_alloc_pair(sc, keyix, rxkeyix);
475 return key_alloc_single(sc, keyix, rxkeyix);
480 * Delete an entry in the key cache allocated by ath_key_alloc.
483 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
485 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
486 struct ath_hal *ah = sc->sc_ah;
487 const struct ieee80211_cipher *cip = k->wk_cipher;
488 u_int keyix = k->wk_keyix;
490 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
492 ath_hal_keyreset(ah, keyix);
494 * Handle split tx/rx keying required for TKIP with h/w MIC.
496 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
497 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
498 ath_hal_keyreset(ah, keyix+32); /* RX key */
499 if (keyix >= IEEE80211_WEP_NKID) {
501 * Don't touch keymap entries for global keys so
502 * they are never considered for dynamic allocation.
504 clrbit(sc->sc_keymap, keyix);
505 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
506 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
507 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */
508 if (sc->sc_splitmic) {
509 /* +32 for RX key, +32+64 for RX key MIC */
510 clrbit(sc->sc_keymap, keyix+32);
511 clrbit(sc->sc_keymap, keyix+32+64);
519 * Set the key cache contents for the specified key. Key cache
520 * slot(s) must already have been allocated by ath_key_alloc.
523 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
524 const u_int8_t mac[IEEE80211_ADDR_LEN])
526 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
528 return ath_keyset(sc, vap, k, vap->iv_bss);