key.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2009 Atheros Communications Inc.
  3. * Copyright (c) 2010 Bruno Randolf <br1@einfach.org>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <asm/unaligned.h>
  18. #include <net/mac80211.h>
  19. #include "ath.h"
  20. #include "reg.h"
  21. #define REG_READ (common->ops->read)
  22. #define REG_WRITE(_ah, _reg, _val) (common->ops->write)(_ah, _val, _reg)
  23. #define ENABLE_REGWRITE_BUFFER(_ah) \
  24. if (common->ops->enable_write_buffer) \
  25. common->ops->enable_write_buffer((_ah));
  26. #define REGWRITE_BUFFER_FLUSH(_ah) \
  27. if (common->ops->write_flush) \
  28. common->ops->write_flush((_ah));
  29. #define IEEE80211_WEP_NKID 4 /* number of key ids */
  30. /************************/
  31. /* Key Cache Management */
  32. /************************/
  33. bool ath_hw_keyreset(struct ath_common *common, u16 entry)
  34. {
  35. u32 keyType;
  36. void *ah = common->ah;
  37. if (entry >= common->keymax) {
  38. ath_err(common, "keycache entry %u out of range\n", entry);
  39. return false;
  40. }
  41. keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
  42. ENABLE_REGWRITE_BUFFER(ah);
  43. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
  44. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
  45. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
  46. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
  47. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
  48. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
  49. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
  50. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
  51. if (keyType == AR_KEYTABLE_TYPE_TKIP) {
  52. u16 micentry = entry + 64;
  53. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
  54. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  55. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
  56. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  57. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  58. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  59. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  60. AR_KEYTABLE_TYPE_CLR);
  61. }
  62. }
  63. REGWRITE_BUFFER_FLUSH(ah);
  64. return true;
  65. }
  66. EXPORT_SYMBOL(ath_hw_keyreset);
  67. static bool ath_hw_keysetmac(struct ath_common *common,
  68. u16 entry, const u8 *mac)
  69. {
  70. u32 macHi, macLo;
  71. u32 unicast_flag = AR_KEYTABLE_VALID;
  72. void *ah = common->ah;
  73. if (entry >= common->keymax) {
  74. ath_err(common, "keycache entry %u out of range\n", entry);
  75. return false;
  76. }
  77. if (mac != NULL) {
  78. /*
  79. * AR_KEYTABLE_VALID indicates that the address is a unicast
  80. * address, which must match the transmitter address for
  81. * decrypting frames.
  82. * Not setting this bit allows the hardware to use the key
  83. * for multicast frame decryption.
  84. */
  85. if (mac[0] & 0x01)
  86. unicast_flag = 0;
  87. macHi = (mac[5] << 8) | mac[4];
  88. macLo = (mac[3] << 24) |
  89. (mac[2] << 16) |
  90. (mac[1] << 8) |
  91. mac[0];
  92. macLo >>= 1;
  93. macLo |= (macHi & 1) << 31;
  94. macHi >>= 1;
  95. } else {
  96. macLo = macHi = 0;
  97. }
  98. ENABLE_REGWRITE_BUFFER(ah);
  99. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
  100. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | unicast_flag);
  101. REGWRITE_BUFFER_FLUSH(ah);
  102. return true;
  103. }
  104. static bool ath_hw_set_keycache_entry(struct ath_common *common, u16 entry,
  105. const struct ath_keyval *k,
  106. const u8 *mac)
  107. {
  108. void *ah = common->ah;
  109. u32 key0, key1, key2, key3, key4;
  110. u32 keyType;
  111. if (entry >= common->keymax) {
  112. ath_err(common, "keycache entry %u out of range\n", entry);
  113. return false;
  114. }
  115. switch (k->kv_type) {
  116. case ATH_CIPHER_AES_OCB:
  117. keyType = AR_KEYTABLE_TYPE_AES;
  118. break;
  119. case ATH_CIPHER_AES_CCM:
  120. if (!(common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)) {
  121. ath_dbg(common, ATH_DBG_ANY,
  122. "AES-CCM not supported by this mac rev\n");
  123. return false;
  124. }
  125. keyType = AR_KEYTABLE_TYPE_CCM;
  126. break;
  127. case ATH_CIPHER_TKIP:
  128. keyType = AR_KEYTABLE_TYPE_TKIP;
  129. if (entry + 64 >= common->keymax) {
  130. ath_dbg(common, ATH_DBG_ANY,
  131. "entry %u inappropriate for TKIP\n", entry);
  132. return false;
  133. }
  134. break;
  135. case ATH_CIPHER_WEP:
  136. if (k->kv_len < WLAN_KEY_LEN_WEP40) {
  137. ath_dbg(common, ATH_DBG_ANY,
  138. "WEP key length %u too small\n", k->kv_len);
  139. return false;
  140. }
  141. if (k->kv_len <= WLAN_KEY_LEN_WEP40)
  142. keyType = AR_KEYTABLE_TYPE_40;
  143. else if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  144. keyType = AR_KEYTABLE_TYPE_104;
  145. else
  146. keyType = AR_KEYTABLE_TYPE_128;
  147. break;
  148. case ATH_CIPHER_CLR:
  149. keyType = AR_KEYTABLE_TYPE_CLR;
  150. break;
  151. default:
  152. ath_err(common, "cipher %u not supported\n", k->kv_type);
  153. return false;
  154. }
  155. key0 = get_unaligned_le32(k->kv_val + 0);
  156. key1 = get_unaligned_le16(k->kv_val + 4);
  157. key2 = get_unaligned_le32(k->kv_val + 6);
  158. key3 = get_unaligned_le16(k->kv_val + 10);
  159. key4 = get_unaligned_le32(k->kv_val + 12);
  160. if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  161. key4 &= 0xff;
  162. /*
  163. * Note: Key cache registers access special memory area that requires
  164. * two 32-bit writes to actually update the values in the internal
  165. * memory. Consequently, the exact order and pairs used here must be
  166. * maintained.
  167. */
  168. if (keyType == AR_KEYTABLE_TYPE_TKIP) {
  169. u16 micentry = entry + 64;
  170. /*
  171. * Write inverted key[47:0] first to avoid Michael MIC errors
  172. * on frames that could be sent or received at the same time.
  173. * The correct key will be written in the end once everything
  174. * else is ready.
  175. */
  176. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
  177. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
  178. /* Write key[95:48] */
  179. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  180. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  181. /* Write key[127:96] and key type */
  182. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  183. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  184. /* Write MAC address for the entry */
  185. (void) ath_hw_keysetmac(common, entry, mac);
  186. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  187. /*
  188. * TKIP uses two key cache entries:
  189. * Michael MIC TX/RX keys in the same key cache entry
  190. * (idx = main index + 64):
  191. * key0 [31:0] = RX key [31:0]
  192. * key1 [15:0] = TX key [31:16]
  193. * key1 [31:16] = reserved
  194. * key2 [31:0] = RX key [63:32]
  195. * key3 [15:0] = TX key [15:0]
  196. * key3 [31:16] = reserved
  197. * key4 [31:0] = TX key [63:32]
  198. */
  199. u32 mic0, mic1, mic2, mic3, mic4;
  200. mic0 = get_unaligned_le32(k->kv_mic + 0);
  201. mic2 = get_unaligned_le32(k->kv_mic + 4);
  202. mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
  203. mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
  204. mic4 = get_unaligned_le32(k->kv_txmic + 4);
  205. ENABLE_REGWRITE_BUFFER(ah);
  206. /* Write RX[31:0] and TX[31:16] */
  207. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  208. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
  209. /* Write RX[63:32] and TX[15:0] */
  210. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  211. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
  212. /* Write TX[63:32] and keyType(reserved) */
  213. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
  214. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  215. AR_KEYTABLE_TYPE_CLR);
  216. REGWRITE_BUFFER_FLUSH(ah);
  217. } else {
  218. /*
  219. * TKIP uses four key cache entries (two for group
  220. * keys):
  221. * Michael MIC TX/RX keys are in different key cache
  222. * entries (idx = main index + 64 for TX and
  223. * main index + 32 + 96 for RX):
  224. * key0 [31:0] = TX/RX MIC key [31:0]
  225. * key1 [31:0] = reserved
  226. * key2 [31:0] = TX/RX MIC key [63:32]
  227. * key3 [31:0] = reserved
  228. * key4 [31:0] = reserved
  229. *
  230. * Upper layer code will call this function separately
  231. * for TX and RX keys when these registers offsets are
  232. * used.
  233. */
  234. u32 mic0, mic2;
  235. mic0 = get_unaligned_le32(k->kv_mic + 0);
  236. mic2 = get_unaligned_le32(k->kv_mic + 4);
  237. ENABLE_REGWRITE_BUFFER(ah);
  238. /* Write MIC key[31:0] */
  239. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  240. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  241. /* Write MIC key[63:32] */
  242. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  243. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  244. /* Write TX[63:32] and keyType(reserved) */
  245. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  246. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  247. AR_KEYTABLE_TYPE_CLR);
  248. REGWRITE_BUFFER_FLUSH(ah);
  249. }
  250. ENABLE_REGWRITE_BUFFER(ah);
  251. /* MAC address registers are reserved for the MIC entry */
  252. REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
  253. REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
  254. /*
  255. * Write the correct (un-inverted) key[47:0] last to enable
  256. * TKIP now that all other registers are set with correct
  257. * values.
  258. */
  259. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  260. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  261. REGWRITE_BUFFER_FLUSH(ah);
  262. } else {
  263. ENABLE_REGWRITE_BUFFER(ah);
  264. /* Write key[47:0] */
  265. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  266. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  267. /* Write key[95:48] */
  268. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  269. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  270. /* Write key[127:96] and key type */
  271. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  272. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  273. REGWRITE_BUFFER_FLUSH(ah);
  274. /* Write MAC address for the entry */
  275. (void) ath_hw_keysetmac(common, entry, mac);
  276. }
  277. return true;
  278. }
  279. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  280. struct ath_keyval *hk, const u8 *addr,
  281. bool authenticator)
  282. {
  283. const u8 *key_rxmic;
  284. const u8 *key_txmic;
  285. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  286. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  287. if (addr == NULL) {
  288. /*
  289. * Group key installation - only two key cache entries are used
  290. * regardless of splitmic capability since group key is only
  291. * used either for TX or RX.
  292. */
  293. if (authenticator) {
  294. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  295. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  296. } else {
  297. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  298. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  299. }
  300. return ath_hw_set_keycache_entry(common, keyix, hk, addr);
  301. }
  302. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  303. /* TX and RX keys share the same key cache entry. */
  304. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  305. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  306. return ath_hw_set_keycache_entry(common, keyix, hk, addr);
  307. }
  308. /* Separate key cache entries for TX and RX */
  309. /* TX key goes at first index, RX key at +32. */
  310. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  311. if (!ath_hw_set_keycache_entry(common, keyix, hk, NULL)) {
  312. /* TX MIC entry failed. No need to proceed further */
  313. ath_err(common, "Setting TX MIC Key Failed\n");
  314. return 0;
  315. }
  316. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  317. /* XXX delete tx key on failure? */
  318. return ath_hw_set_keycache_entry(common, keyix + 32, hk, addr);
  319. }
  320. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  321. {
  322. int i;
  323. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  324. if (test_bit(i, common->keymap) ||
  325. test_bit(i + 64, common->keymap))
  326. continue; /* At least one part of TKIP key allocated */
  327. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) &&
  328. (test_bit(i + 32, common->keymap) ||
  329. test_bit(i + 64 + 32, common->keymap)))
  330. continue; /* At least one part of TKIP key allocated */
  331. /* Found a free slot for a TKIP key */
  332. return i;
  333. }
  334. return -1;
  335. }
  336. static int ath_reserve_key_cache_slot(struct ath_common *common,
  337. u32 cipher)
  338. {
  339. int i;
  340. if (cipher == WLAN_CIPHER_SUITE_TKIP)
  341. return ath_reserve_key_cache_slot_tkip(common);
  342. /* First, try to find slots that would not be available for TKIP. */
  343. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  344. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  345. if (!test_bit(i, common->keymap) &&
  346. (test_bit(i + 32, common->keymap) ||
  347. test_bit(i + 64, common->keymap) ||
  348. test_bit(i + 64 + 32, common->keymap)))
  349. return i;
  350. if (!test_bit(i + 32, common->keymap) &&
  351. (test_bit(i, common->keymap) ||
  352. test_bit(i + 64, common->keymap) ||
  353. test_bit(i + 64 + 32, common->keymap)))
  354. return i + 32;
  355. if (!test_bit(i + 64, common->keymap) &&
  356. (test_bit(i , common->keymap) ||
  357. test_bit(i + 32, common->keymap) ||
  358. test_bit(i + 64 + 32, common->keymap)))
  359. return i + 64;
  360. if (!test_bit(i + 64 + 32, common->keymap) &&
  361. (test_bit(i, common->keymap) ||
  362. test_bit(i + 32, common->keymap) ||
  363. test_bit(i + 64, common->keymap)))
  364. return i + 64 + 32;
  365. }
  366. } else {
  367. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  368. if (!test_bit(i, common->keymap) &&
  369. test_bit(i + 64, common->keymap))
  370. return i;
  371. if (test_bit(i, common->keymap) &&
  372. !test_bit(i + 64, common->keymap))
  373. return i + 64;
  374. }
  375. }
  376. /* No partially used TKIP slots, pick any available slot */
  377. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  378. /* Do not allow slots that could be needed for TKIP group keys
  379. * to be used. This limitation could be removed if we know that
  380. * TKIP will not be used. */
  381. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  382. continue;
  383. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  384. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  385. continue;
  386. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  387. continue;
  388. }
  389. if (!test_bit(i, common->keymap))
  390. return i; /* Found a free slot for a key */
  391. }
  392. /* No free slot found */
  393. return -1;
  394. }
  395. /*
  396. * Configure encryption in the HW.
  397. */
  398. int ath_key_config(struct ath_common *common,
  399. struct ieee80211_vif *vif,
  400. struct ieee80211_sta *sta,
  401. struct ieee80211_key_conf *key)
  402. {
  403. struct ath_keyval hk;
  404. const u8 *mac = NULL;
  405. u8 gmac[ETH_ALEN];
  406. int ret = 0;
  407. int idx;
  408. memset(&hk, 0, sizeof(hk));
  409. switch (key->cipher) {
  410. case 0:
  411. hk.kv_type = ATH_CIPHER_CLR;
  412. break;
  413. case WLAN_CIPHER_SUITE_WEP40:
  414. case WLAN_CIPHER_SUITE_WEP104:
  415. hk.kv_type = ATH_CIPHER_WEP;
  416. break;
  417. case WLAN_CIPHER_SUITE_TKIP:
  418. hk.kv_type = ATH_CIPHER_TKIP;
  419. break;
  420. case WLAN_CIPHER_SUITE_CCMP:
  421. hk.kv_type = ATH_CIPHER_AES_CCM;
  422. break;
  423. default:
  424. return -EOPNOTSUPP;
  425. }
  426. hk.kv_len = key->keylen;
  427. if (key->keylen)
  428. memcpy(hk.kv_val, key->key, key->keylen);
  429. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  430. switch (vif->type) {
  431. case NL80211_IFTYPE_AP:
  432. memcpy(gmac, vif->addr, ETH_ALEN);
  433. gmac[0] |= 0x01;
  434. mac = gmac;
  435. idx = ath_reserve_key_cache_slot(common, key->cipher);
  436. break;
  437. case NL80211_IFTYPE_ADHOC:
  438. if (!sta) {
  439. idx = key->keyidx;
  440. break;
  441. }
  442. memcpy(gmac, sta->addr, ETH_ALEN);
  443. gmac[0] |= 0x01;
  444. mac = gmac;
  445. idx = ath_reserve_key_cache_slot(common, key->cipher);
  446. break;
  447. default:
  448. idx = key->keyidx;
  449. break;
  450. }
  451. } else if (key->keyidx) {
  452. if (WARN_ON(!sta))
  453. return -EOPNOTSUPP;
  454. mac = sta->addr;
  455. if (vif->type != NL80211_IFTYPE_AP) {
  456. /* Only keyidx 0 should be used with unicast key, but
  457. * allow this for client mode for now. */
  458. idx = key->keyidx;
  459. } else
  460. return -EIO;
  461. } else {
  462. if (WARN_ON(!sta))
  463. return -EOPNOTSUPP;
  464. mac = sta->addr;
  465. idx = ath_reserve_key_cache_slot(common, key->cipher);
  466. }
  467. if (idx < 0)
  468. return -ENOSPC; /* no free key cache entries */
  469. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  470. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  471. vif->type == NL80211_IFTYPE_AP);
  472. else
  473. ret = ath_hw_set_keycache_entry(common, idx, &hk, mac);
  474. if (!ret)
  475. return -EIO;
  476. set_bit(idx, common->keymap);
  477. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  478. set_bit(idx + 64, common->keymap);
  479. set_bit(idx, common->tkip_keymap);
  480. set_bit(idx + 64, common->tkip_keymap);
  481. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  482. set_bit(idx + 32, common->keymap);
  483. set_bit(idx + 64 + 32, common->keymap);
  484. set_bit(idx + 32, common->tkip_keymap);
  485. set_bit(idx + 64 + 32, common->tkip_keymap);
  486. }
  487. }
  488. return idx;
  489. }
  490. EXPORT_SYMBOL(ath_key_config);
  491. /*
  492. * Delete Key.
  493. */
  494. void ath_key_delete(struct ath_common *common, struct ieee80211_key_conf *key)
  495. {
  496. ath_hw_keyreset(common, key->hw_key_idx);
  497. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  498. return;
  499. clear_bit(key->hw_key_idx, common->keymap);
  500. if (key->cipher != WLAN_CIPHER_SUITE_TKIP)
  501. return;
  502. clear_bit(key->hw_key_idx + 64, common->keymap);
  503. clear_bit(key->hw_key_idx, common->tkip_keymap);
  504. clear_bit(key->hw_key_idx + 64, common->tkip_keymap);
  505. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  506. ath_hw_keyreset(common, key->hw_key_idx + 32);
  507. clear_bit(key->hw_key_idx + 32, common->keymap);
  508. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  509. clear_bit(key->hw_key_idx + 32, common->tkip_keymap);
  510. clear_bit(key->hw_key_idx + 64 + 32, common->tkip_keymap);
  511. }
  512. }
  513. EXPORT_SYMBOL(ath_key_delete);