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