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 <net/if.h> 63 #include <net/if_var.h> 64 #include <net/if_dl.h> 65 #include <net/if_media.h> 66 #include <net/if_types.h> 67 #include <net/if_arp.h> 68 #include <net/ethernet.h> 69 #include <net/if_llc.h> 70 71 #include <netproto/802_11/ieee80211_var.h> 72 73 #include <net/bpf.h> 74 75 #include <dev/netif/ath/ath/if_athvar.h> 76 77 #include <dev/netif/ath/ath/if_ath_debug.h> 78 #include <dev/netif/ath/ath/if_ath_keycache.h> 79 #include <dev/netif/ath/ath/if_ath_misc.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 kprintf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]); 97 for (i = 0, n = hk->kv_len; i < n; i++) 98 kprintf("%02x", hk->kv_val[i]); 99 kprintf(" mac %s", ether_sprintf(mac)); 100 if (hk->kv_type == HAL_CIPHER_TKIP) { 101 kprintf(" %s ", sc->sc_splitmic ? "mic" : "rxmic"); 102 for (i = 0; i < sizeof(hk->kv_mic); i++) 103 kprintf("%02x", hk->kv_mic[i]); 104 if (!sc->sc_splitmic) { 105 kprintf(" txmic "); 106 for (i = 0; i < sizeof(hk->kv_txmic); i++) 107 kprintf("%02x", hk->kv_txmic[i]); 108 } 109 } 110 kprintf("\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 int ret; 201 202 memset(&hk, 0, sizeof(hk)); 203 /* 204 * Software crypto uses a "clear key" so non-crypto 205 * state kept in the key cache are maintained and 206 * so that rx frames have an entry to match. 207 */ 208 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 209 KASSERT(cip->ic_cipher < N(ciphermap), 210 ("invalid cipher type %u", cip->ic_cipher)); 211 hk.kv_type = ciphermap[cip->ic_cipher]; 212 hk.kv_len = k->wk_keylen; 213 memcpy(hk.kv_val, k->wk_key, k->wk_keylen); 214 } else 215 hk.kv_type = HAL_CIPHER_CLR; 216 217 /* 218 * If we're installing a clear cipher key and 219 * the hardware doesn't support that, just succeed. 220 * Leave it up to the net80211 layer to figure it out. 221 */ 222 if (hk.kv_type == HAL_CIPHER_CLR && sc->sc_hasclrkey == 0) { 223 return (1); 224 } 225 226 /* 227 * XXX TODO: check this: 228 * 229 * Group keys on hardware that supports multicast frame 230 * key search should only be done in adhoc/hostap mode, 231 * not STA mode. 232 * 233 * XXX TODO: what about mesh, tdma? 234 */ 235 #if 0 236 if ((vap->iv_opmode == IEEE80211_M_HOSTAP || 237 vap->iv_opmode == IEEE80211_M_IBSS) && 238 #else 239 if ( 240 #endif 241 (k->wk_flags & IEEE80211_KEY_GROUP) && 242 sc->sc_mcastkey) { 243 /* 244 * Group keys on hardware that supports multicast frame 245 * key search use a MAC that is the sender's address with 246 * the multicast bit set instead of the app-specified address. 247 */ 248 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr); 249 gmac[0] |= 0x01; 250 mac = gmac; 251 } else 252 mac = k->wk_macaddr; 253 254 ATH_LOCK(sc); 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 ATH_UNLOCK(sc); 265 266 return (ret); 267 #undef N 268 } 269 270 /* 271 * Allocate tx/rx key slots for TKIP. We allocate two slots for 272 * each key, one for decrypt/encrypt and the other for the MIC. 273 */ 274 static u_int16_t 275 key_alloc_2pair(struct ath_softc *sc, 276 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 277 { 278 #define N(a) (sizeof(a)/sizeof(a[0])) 279 u_int i, keyix; 280 281 KASSERT(sc->sc_splitmic, ("key cache !split")); 282 /* XXX could optimize */ 283 for (i = 0; i < N(sc->sc_keymap)/4; i++) { 284 u_int8_t b = sc->sc_keymap[i]; 285 if (b != 0xff) { 286 /* 287 * One or more slots in this byte are free. 288 */ 289 keyix = i*NBBY; 290 while (b & 1) { 291 again: 292 keyix++; 293 b >>= 1; 294 } 295 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */ 296 if (isset(sc->sc_keymap, keyix+32) || 297 isset(sc->sc_keymap, keyix+64) || 298 isset(sc->sc_keymap, keyix+32+64)) { 299 /* full pair unavailable */ 300 /* XXX statistic */ 301 if (keyix == (i+1)*NBBY) { 302 /* no slots were appropriate, advance */ 303 continue; 304 } 305 goto again; 306 } 307 setbit(sc->sc_keymap, keyix); 308 setbit(sc->sc_keymap, keyix+64); 309 setbit(sc->sc_keymap, keyix+32); 310 setbit(sc->sc_keymap, keyix+32+64); 311 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 312 "%s: key pair %u,%u %u,%u\n", 313 __func__, keyix, keyix+64, 314 keyix+32, keyix+32+64); 315 *txkeyix = keyix; 316 *rxkeyix = keyix+32; 317 return 1; 318 } 319 } 320 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__); 321 return 0; 322 #undef N 323 } 324 325 /* 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. 328 */ 329 static u_int16_t 330 key_alloc_pair(struct ath_softc *sc, 331 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 332 { 333 #define N(a) (sizeof(a)/sizeof(a[0])) 334 u_int i, keyix; 335 336 KASSERT(!sc->sc_splitmic, ("key cache split")); 337 /* XXX could optimize */ 338 for (i = 0; i < N(sc->sc_keymap)/4; i++) { 339 u_int8_t b = sc->sc_keymap[i]; 340 if (b != 0xff) { 341 /* 342 * One or more slots in this byte are free. 343 */ 344 keyix = i*NBBY; 345 while (b & 1) { 346 again: 347 keyix++; 348 b >>= 1; 349 } 350 if (isset(sc->sc_keymap, keyix+64)) { 351 /* full pair unavailable */ 352 /* XXX statistic */ 353 if (keyix == (i+1)*NBBY) { 354 /* no slots were appropriate, advance */ 355 continue; 356 } 357 goto again; 358 } 359 setbit(sc->sc_keymap, keyix); 360 setbit(sc->sc_keymap, keyix+64); 361 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 362 "%s: key pair %u,%u\n", 363 __func__, keyix, keyix+64); 364 *txkeyix = *rxkeyix = keyix; 365 return 1; 366 } 367 } 368 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__); 369 return 0; 370 #undef N 371 } 372 373 /* 374 * Allocate a single key cache slot. 375 */ 376 static int 377 key_alloc_single(struct ath_softc *sc, 378 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 379 { 380 #define N(a) (sizeof(a)/sizeof(a[0])) 381 u_int i, keyix; 382 383 if (sc->sc_hasclrkey == 0) { 384 /* 385 * Map to slot 0 for the AR5210. 386 */ 387 *txkeyix = *rxkeyix = 0; 388 return (1); 389 } 390 391 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */ 392 for (i = 0; i < N(sc->sc_keymap); i++) { 393 u_int8_t b = sc->sc_keymap[i]; 394 if (b != 0xff) { 395 /* 396 * One or more slots are free. 397 */ 398 keyix = i*NBBY; 399 while (b & 1) 400 keyix++, b >>= 1; 401 setbit(sc->sc_keymap, keyix); 402 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n", 403 __func__, keyix); 404 *txkeyix = *rxkeyix = keyix; 405 return 1; 406 } 407 } 408 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__); 409 return 0; 410 #undef N 411 } 412 413 /* 414 * Allocate one or more key cache slots for a uniacst key. The 415 * key itself is needed only to identify the cipher. For hardware 416 * TKIP with split cipher+MIC keys we allocate two key cache slot 417 * pairs so that we can setup separate TX and RX MIC keys. Note 418 * that the MIC key for a TKIP key at slot i is assumed by the 419 * hardware to be at slot i+64. This limits TKIP keys to the first 420 * 64 entries. 421 */ 422 int 423 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 424 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 425 { 426 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 427 428 /* 429 * Group key allocation must be handled specially for 430 * parts that do not support multicast key cache search 431 * functionality. For those parts the key id must match 432 * the h/w key index so lookups find the right key. On 433 * parts w/ the key search facility we install the sender's 434 * mac address (with the high bit set) and let the hardware 435 * find the key w/o using the key id. This is preferred as 436 * it permits us to support multiple users for adhoc and/or 437 * multi-station operation. 438 */ 439 if (k->wk_keyix != IEEE80211_KEYIX_NONE) { 440 /* 441 * Only global keys should have key index assigned. 442 */ 443 if (!(&vap->iv_nw_keys[0] <= k && 444 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 445 /* should not happen */ 446 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 447 "%s: bogus group key\n", __func__); 448 return 0; 449 } 450 if (vap->iv_opmode != IEEE80211_M_HOSTAP || 451 !(k->wk_flags & IEEE80211_KEY_GROUP) || 452 !sc->sc_mcastkey) { 453 /* 454 * XXX we pre-allocate the global keys so 455 * have no way to check if they've already 456 * been allocated. 457 */ 458 *keyix = *rxkeyix = k - vap->iv_nw_keys; 459 return 1; 460 } 461 /* 462 * Group key and device supports multicast key search. 463 */ 464 k->wk_keyix = IEEE80211_KEYIX_NONE; 465 } 466 467 /* 468 * We allocate two pair for TKIP when using the h/w to do 469 * the MIC. For everything else, including software crypto, 470 * we allocate a single entry. Note that s/w crypto requires 471 * a pass-through slot on the 5211 and 5212. The 5210 does 472 * not support pass-through cache entries and we map all 473 * those requests to slot 0. 474 */ 475 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 476 return key_alloc_single(sc, keyix, rxkeyix); 477 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP && 478 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) { 479 if (sc->sc_splitmic) 480 return key_alloc_2pair(sc, keyix, rxkeyix); 481 else 482 return key_alloc_pair(sc, keyix, rxkeyix); 483 } else { 484 return key_alloc_single(sc, keyix, rxkeyix); 485 } 486 } 487 488 /* 489 * Delete an entry in the key cache allocated by ath_key_alloc. 490 */ 491 int 492 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 493 { 494 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 495 struct ath_hal *ah = sc->sc_ah; 496 const struct ieee80211_cipher *cip = k->wk_cipher; 497 u_int keyix = k->wk_keyix; 498 499 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix); 500 501 ATH_LOCK(sc); 502 ath_power_set_power_state(sc, HAL_PM_AWAKE); 503 ath_hal_keyreset(ah, keyix); 504 /* 505 * Handle split tx/rx keying required for TKIP with h/w MIC. 506 */ 507 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 508 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) 509 ath_hal_keyreset(ah, keyix+32); /* RX key */ 510 if (keyix >= IEEE80211_WEP_NKID) { 511 /* 512 * Don't touch keymap entries for global keys so 513 * they are never considered for dynamic allocation. 514 */ 515 clrbit(sc->sc_keymap, keyix); 516 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 517 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) { 518 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */ 519 if (sc->sc_splitmic) { 520 /* +32 for RX key, +32+64 for RX key MIC */ 521 clrbit(sc->sc_keymap, keyix+32); 522 clrbit(sc->sc_keymap, keyix+32+64); 523 } 524 } 525 } 526 ath_power_restore_power_state(sc); 527 ATH_UNLOCK(sc); 528 return 1; 529 } 530 531 /* 532 * Set the key cache contents for the specified key. Key cache 533 * slot(s) must already have been allocated by ath_key_alloc. 534 */ 535 int 536 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k, 537 const u_int8_t mac[IEEE80211_ADDR_LEN]) 538 { 539 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 540 541 return ath_keyset(sc, vap, k, vap->iv_bss); 542 } 543