key.c 17 KB

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