common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. * Module for common driver code between ath9k and ath9k_htc
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include "common.h"
  22. MODULE_AUTHOR("Atheros Communications");
  23. MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
  24. MODULE_LICENSE("Dual BSD/GPL");
  25. int ath9k_cmn_padpos(__le16 frame_control)
  26. {
  27. int padpos = 24;
  28. if (ieee80211_has_a4(frame_control)) {
  29. padpos += ETH_ALEN;
  30. }
  31. if (ieee80211_is_data_qos(frame_control)) {
  32. padpos += IEEE80211_QOS_CTL_LEN;
  33. }
  34. return padpos;
  35. }
  36. EXPORT_SYMBOL(ath9k_cmn_padpos);
  37. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  38. {
  39. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  40. if (tx_info->control.hw_key) {
  41. if (tx_info->control.hw_key->alg == ALG_WEP)
  42. return ATH9K_KEY_TYPE_WEP;
  43. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  44. return ATH9K_KEY_TYPE_TKIP;
  45. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  46. return ATH9K_KEY_TYPE_AES;
  47. }
  48. return ATH9K_KEY_TYPE_CLEAR;
  49. }
  50. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  51. static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
  52. enum nl80211_channel_type channel_type)
  53. {
  54. u32 chanmode = 0;
  55. switch (chan->band) {
  56. case IEEE80211_BAND_2GHZ:
  57. switch (channel_type) {
  58. case NL80211_CHAN_NO_HT:
  59. case NL80211_CHAN_HT20:
  60. chanmode = CHANNEL_G_HT20;
  61. break;
  62. case NL80211_CHAN_HT40PLUS:
  63. chanmode = CHANNEL_G_HT40PLUS;
  64. break;
  65. case NL80211_CHAN_HT40MINUS:
  66. chanmode = CHANNEL_G_HT40MINUS;
  67. break;
  68. }
  69. break;
  70. case IEEE80211_BAND_5GHZ:
  71. switch (channel_type) {
  72. case NL80211_CHAN_NO_HT:
  73. case NL80211_CHAN_HT20:
  74. chanmode = CHANNEL_A_HT20;
  75. break;
  76. case NL80211_CHAN_HT40PLUS:
  77. chanmode = CHANNEL_A_HT40PLUS;
  78. break;
  79. case NL80211_CHAN_HT40MINUS:
  80. chanmode = CHANNEL_A_HT40MINUS;
  81. break;
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. return chanmode;
  88. }
  89. /*
  90. * Update internal channel flags.
  91. */
  92. void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
  93. struct ath9k_channel *ichan)
  94. {
  95. struct ieee80211_channel *chan = hw->conf.channel;
  96. struct ieee80211_conf *conf = &hw->conf;
  97. ichan->channel = chan->center_freq;
  98. ichan->chan = chan;
  99. if (chan->band == IEEE80211_BAND_2GHZ) {
  100. ichan->chanmode = CHANNEL_G;
  101. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  102. } else {
  103. ichan->chanmode = CHANNEL_A;
  104. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  105. }
  106. if (conf_is_ht(conf))
  107. ichan->chanmode = ath9k_get_extchanmode(chan,
  108. conf->channel_type);
  109. }
  110. EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
  111. /*
  112. * Get the internal channel reference.
  113. */
  114. struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
  115. struct ath_hw *ah)
  116. {
  117. struct ieee80211_channel *curchan = hw->conf.channel;
  118. struct ath9k_channel *channel;
  119. u8 chan_idx;
  120. chan_idx = curchan->hw_value;
  121. channel = &ah->channels[chan_idx];
  122. ath9k_cmn_update_ichannel(hw, channel);
  123. return channel;
  124. }
  125. EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
  126. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  127. struct ath9k_keyval *hk, const u8 *addr,
  128. bool authenticator)
  129. {
  130. struct ath_hw *ah = common->ah;
  131. const u8 *key_rxmic;
  132. const u8 *key_txmic;
  133. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  134. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  135. if (addr == NULL) {
  136. /*
  137. * Group key installation - only two key cache entries are used
  138. * regardless of splitmic capability since group key is only
  139. * used either for TX or RX.
  140. */
  141. if (authenticator) {
  142. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  143. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  144. } else {
  145. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  146. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  147. }
  148. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  149. }
  150. if (!common->splitmic) {
  151. /* TX and RX keys share the same key cache entry. */
  152. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  153. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  154. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  155. }
  156. /* Separate key cache entries for TX and RX */
  157. /* TX key goes at first index, RX key at +32. */
  158. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  159. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  160. /* TX MIC entry failed. No need to proceed further */
  161. ath_print(common, ATH_DBG_FATAL,
  162. "Setting TX MIC Key Failed\n");
  163. return 0;
  164. }
  165. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  166. /* XXX delete tx key on failure? */
  167. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  168. }
  169. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  170. {
  171. int i;
  172. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  173. if (test_bit(i, common->keymap) ||
  174. test_bit(i + 64, common->keymap))
  175. continue; /* At least one part of TKIP key allocated */
  176. if (common->splitmic &&
  177. (test_bit(i + 32, common->keymap) ||
  178. test_bit(i + 64 + 32, common->keymap)))
  179. continue; /* At least one part of TKIP key allocated */
  180. /* Found a free slot for a TKIP key */
  181. return i;
  182. }
  183. return -1;
  184. }
  185. static int ath_reserve_key_cache_slot(struct ath_common *common)
  186. {
  187. int i;
  188. /* First, try to find slots that would not be available for TKIP. */
  189. if (common->splitmic) {
  190. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  191. if (!test_bit(i, common->keymap) &&
  192. (test_bit(i + 32, common->keymap) ||
  193. test_bit(i + 64, common->keymap) ||
  194. test_bit(i + 64 + 32, common->keymap)))
  195. return i;
  196. if (!test_bit(i + 32, common->keymap) &&
  197. (test_bit(i, common->keymap) ||
  198. test_bit(i + 64, common->keymap) ||
  199. test_bit(i + 64 + 32, common->keymap)))
  200. return i + 32;
  201. if (!test_bit(i + 64, common->keymap) &&
  202. (test_bit(i , common->keymap) ||
  203. test_bit(i + 32, common->keymap) ||
  204. test_bit(i + 64 + 32, common->keymap)))
  205. return i + 64;
  206. if (!test_bit(i + 64 + 32, common->keymap) &&
  207. (test_bit(i, common->keymap) ||
  208. test_bit(i + 32, common->keymap) ||
  209. test_bit(i + 64, common->keymap)))
  210. return i + 64 + 32;
  211. }
  212. } else {
  213. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  214. if (!test_bit(i, common->keymap) &&
  215. test_bit(i + 64, common->keymap))
  216. return i;
  217. if (test_bit(i, common->keymap) &&
  218. !test_bit(i + 64, common->keymap))
  219. return i + 64;
  220. }
  221. }
  222. /* No partially used TKIP slots, pick any available slot */
  223. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  224. /* Do not allow slots that could be needed for TKIP group keys
  225. * to be used. This limitation could be removed if we know that
  226. * TKIP will not be used. */
  227. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  228. continue;
  229. if (common->splitmic) {
  230. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  231. continue;
  232. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  233. continue;
  234. }
  235. if (!test_bit(i, common->keymap))
  236. return i; /* Found a free slot for a key */
  237. }
  238. /* No free slot found */
  239. return -1;
  240. }
  241. /*
  242. * Configure encryption in the HW.
  243. */
  244. int ath9k_cmn_key_config(struct ath_common *common,
  245. struct ieee80211_vif *vif,
  246. struct ieee80211_sta *sta,
  247. struct ieee80211_key_conf *key)
  248. {
  249. struct ath_hw *ah = common->ah;
  250. struct ath9k_keyval hk;
  251. const u8 *mac = NULL;
  252. int ret = 0;
  253. int idx;
  254. memset(&hk, 0, sizeof(hk));
  255. switch (key->alg) {
  256. case ALG_WEP:
  257. hk.kv_type = ATH9K_CIPHER_WEP;
  258. break;
  259. case ALG_TKIP:
  260. hk.kv_type = ATH9K_CIPHER_TKIP;
  261. break;
  262. case ALG_CCMP:
  263. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  264. break;
  265. default:
  266. return -EOPNOTSUPP;
  267. }
  268. hk.kv_len = key->keylen;
  269. memcpy(hk.kv_val, key->key, key->keylen);
  270. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  271. /* For now, use the default keys for broadcast keys. This may
  272. * need to change with virtual interfaces. */
  273. idx = key->keyidx;
  274. } else if (key->keyidx) {
  275. if (WARN_ON(!sta))
  276. return -EOPNOTSUPP;
  277. mac = sta->addr;
  278. if (vif->type != NL80211_IFTYPE_AP) {
  279. /* Only keyidx 0 should be used with unicast key, but
  280. * allow this for client mode for now. */
  281. idx = key->keyidx;
  282. } else
  283. return -EIO;
  284. } else {
  285. if (WARN_ON(!sta))
  286. return -EOPNOTSUPP;
  287. mac = sta->addr;
  288. if (key->alg == ALG_TKIP)
  289. idx = ath_reserve_key_cache_slot_tkip(common);
  290. else
  291. idx = ath_reserve_key_cache_slot(common);
  292. if (idx < 0)
  293. return -ENOSPC; /* no free key cache entries */
  294. }
  295. if (key->alg == ALG_TKIP)
  296. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  297. vif->type == NL80211_IFTYPE_AP);
  298. else
  299. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  300. if (!ret)
  301. return -EIO;
  302. set_bit(idx, common->keymap);
  303. if (key->alg == ALG_TKIP) {
  304. set_bit(idx + 64, common->keymap);
  305. if (common->splitmic) {
  306. set_bit(idx + 32, common->keymap);
  307. set_bit(idx + 64 + 32, common->keymap);
  308. }
  309. }
  310. return idx;
  311. }
  312. EXPORT_SYMBOL(ath9k_cmn_key_config);
  313. /*
  314. * Delete Key.
  315. */
  316. void ath9k_cmn_key_delete(struct ath_common *common,
  317. struct ieee80211_key_conf *key)
  318. {
  319. struct ath_hw *ah = common->ah;
  320. ath9k_hw_keyreset(ah, key->hw_key_idx);
  321. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  322. return;
  323. clear_bit(key->hw_key_idx, common->keymap);
  324. if (key->alg != ALG_TKIP)
  325. return;
  326. clear_bit(key->hw_key_idx + 64, common->keymap);
  327. if (common->splitmic) {
  328. ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
  329. clear_bit(key->hw_key_idx + 32, common->keymap);
  330. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  331. }
  332. }
  333. EXPORT_SYMBOL(ath9k_cmn_key_delete);
  334. static int __init ath9k_cmn_init(void)
  335. {
  336. return 0;
  337. }
  338. module_init(ath9k_cmn_init);
  339. static void __exit ath9k_cmn_exit(void)
  340. {
  341. return;
  342. }
  343. module_exit(ath9k_cmn_exit);