key.c 17 KB

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