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