common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. /* Common RX processing */
  26. /* Assumes you've already done the endian to CPU conversion */
  27. static bool ath9k_rx_accept(struct ath_common *common,
  28. struct sk_buff *skb,
  29. struct ieee80211_rx_status *rxs,
  30. struct ath_rx_status *rx_stats,
  31. bool *decrypt_error)
  32. {
  33. struct ath_hw *ah = common->ah;
  34. struct ieee80211_hdr *hdr;
  35. __le16 fc;
  36. hdr = (struct ieee80211_hdr *) skb->data;
  37. fc = hdr->frame_control;
  38. if (!rx_stats->rs_datalen)
  39. return false;
  40. /*
  41. * rs_status follows rs_datalen so if rs_datalen is too large
  42. * we can take a hint that hardware corrupted it, so ignore
  43. * those frames.
  44. */
  45. if (rx_stats->rs_datalen > common->rx_bufsize)
  46. return false;
  47. /*
  48. * rs_more indicates chained descriptors which can be used
  49. * to link buffers together for a sort of scatter-gather
  50. * operation.
  51. * reject the frame, we don't support scatter-gather yet and
  52. * the frame is probably corrupt anyway
  53. */
  54. if (rx_stats->rs_more)
  55. return false;
  56. /*
  57. * The rx_stats->rs_status will not be set until the end of the
  58. * chained descriptors so it can be ignored if rs_more is set. The
  59. * rs_more will be false at the last element of the chained
  60. * descriptors.
  61. */
  62. if (rx_stats->rs_status != 0) {
  63. if (rx_stats->rs_status & ATH9K_RXERR_CRC)
  64. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  65. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  66. return false;
  67. if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
  68. *decrypt_error = true;
  69. } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
  70. if (ieee80211_is_ctl(fc))
  71. /*
  72. * Sometimes, we get invalid
  73. * MIC failures on valid control frames.
  74. * Remove these mic errors.
  75. */
  76. rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
  77. else
  78. rxs->flag |= RX_FLAG_MMIC_ERROR;
  79. }
  80. /*
  81. * Reject error frames with the exception of
  82. * decryption and MIC failures. For monitor mode,
  83. * we also ignore the CRC error.
  84. */
  85. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  86. if (rx_stats->rs_status &
  87. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  88. ATH9K_RXERR_CRC))
  89. return false;
  90. } else {
  91. if (rx_stats->rs_status &
  92. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  93. return false;
  94. }
  95. }
  96. }
  97. return true;
  98. }
  99. static int ath9k_process_rate(struct ath_common *common,
  100. struct ieee80211_hw *hw,
  101. struct ath_rx_status *rx_stats,
  102. struct ieee80211_rx_status *rxs,
  103. struct sk_buff *skb)
  104. {
  105. struct ieee80211_supported_band *sband;
  106. enum ieee80211_band band;
  107. unsigned int i = 0;
  108. band = hw->conf.channel->band;
  109. sband = hw->wiphy->bands[band];
  110. if (rx_stats->rs_rate & 0x80) {
  111. /* HT rate */
  112. rxs->flag |= RX_FLAG_HT;
  113. if (rx_stats->rs_flags & ATH9K_RX_2040)
  114. rxs->flag |= RX_FLAG_40MHZ;
  115. if (rx_stats->rs_flags & ATH9K_RX_GI)
  116. rxs->flag |= RX_FLAG_SHORT_GI;
  117. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  118. return 0;
  119. }
  120. for (i = 0; i < sband->n_bitrates; i++) {
  121. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  122. rxs->rate_idx = i;
  123. return 0;
  124. }
  125. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  126. rxs->flag |= RX_FLAG_SHORTPRE;
  127. rxs->rate_idx = i;
  128. return 0;
  129. }
  130. }
  131. /*
  132. * No valid hardware bitrate found -- we should not get here
  133. * because hardware has already validated this frame as OK.
  134. */
  135. ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
  136. "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
  137. if ((common->debug_mask & ATH_DBG_XMIT))
  138. print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
  139. return -EINVAL;
  140. }
  141. static void ath9k_process_rssi(struct ath_common *common,
  142. struct ieee80211_hw *hw,
  143. struct sk_buff *skb,
  144. struct ath_rx_status *rx_stats)
  145. {
  146. struct ath_hw *ah = common->ah;
  147. struct ieee80211_sta *sta;
  148. struct ieee80211_hdr *hdr;
  149. struct ath_node *an;
  150. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  151. __le16 fc;
  152. hdr = (struct ieee80211_hdr *)skb->data;
  153. fc = hdr->frame_control;
  154. rcu_read_lock();
  155. /*
  156. * XXX: use ieee80211_find_sta! This requires quite a bit of work
  157. * under the current ath9k virtual wiphy implementation as we have
  158. * no way of tying a vif to wiphy. Typically vifs are attached to
  159. * at least one sdata of a wiphy on mac80211 but with ath9k virtual
  160. * wiphy you'd have to iterate over every wiphy and each sdata.
  161. */
  162. sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
  163. if (sta) {
  164. an = (struct ath_node *) sta->drv_priv;
  165. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
  166. !rx_stats->rs_moreaggr)
  167. ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
  168. last_rssi = an->last_rssi;
  169. }
  170. rcu_read_unlock();
  171. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  172. rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
  173. ATH_RSSI_EP_MULTIPLIER);
  174. if (rx_stats->rs_rssi < 0)
  175. rx_stats->rs_rssi = 0;
  176. /* Update Beacon RSSI, this is used by ANI. */
  177. if (ieee80211_is_beacon(fc))
  178. ah->stats.avgbrssi = rx_stats->rs_rssi;
  179. }
  180. /*
  181. * For Decrypt or Demic errors, we only mark packet status here and always push
  182. * up the frame up to let mac80211 handle the actual error case, be it no
  183. * decryption key or real decryption error. This let us keep statistics there.
  184. */
  185. int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
  186. struct ieee80211_hw *hw,
  187. struct sk_buff *skb,
  188. struct ath_rx_status *rx_stats,
  189. struct ieee80211_rx_status *rx_status,
  190. bool *decrypt_error)
  191. {
  192. struct ath_hw *ah = common->ah;
  193. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  194. /*
  195. * everything but the rate is checked here, the rate check is done
  196. * separately to avoid doing two lookups for a rate for each frame.
  197. */
  198. if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
  199. return -EINVAL;
  200. ath9k_process_rssi(common, hw, skb, rx_stats);
  201. if (ath9k_process_rate(common, hw, rx_stats, rx_status, skb))
  202. return -EINVAL;
  203. rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
  204. rx_status->band = hw->conf.channel->band;
  205. rx_status->freq = hw->conf.channel->center_freq;
  206. rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
  207. rx_status->antenna = rx_stats->rs_antenna;
  208. rx_status->flag |= RX_FLAG_TSFT;
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
  212. void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
  213. struct sk_buff *skb,
  214. struct ath_rx_status *rx_stats,
  215. struct ieee80211_rx_status *rxs,
  216. bool decrypt_error)
  217. {
  218. struct ath_hw *ah = common->ah;
  219. struct ieee80211_hdr *hdr;
  220. int hdrlen, padpos, padsize;
  221. u8 keyix;
  222. __le16 fc;
  223. /* see if any padding is done by the hw and remove it */
  224. hdr = (struct ieee80211_hdr *) skb->data;
  225. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  226. fc = hdr->frame_control;
  227. padpos = ath9k_cmn_padpos(hdr->frame_control);
  228. /* The MAC header is padded to have 32-bit boundary if the
  229. * packet payload is non-zero. The general calculation for
  230. * padsize would take into account odd header lengths:
  231. * padsize = (4 - padpos % 4) % 4; However, since only
  232. * even-length headers are used, padding can only be 0 or 2
  233. * bytes and we can optimize this a bit. In addition, we must
  234. * not try to remove padding from short control frames that do
  235. * not have payload. */
  236. padsize = padpos & 3;
  237. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  238. memmove(skb->data + padsize, skb->data, padpos);
  239. skb_pull(skb, padsize);
  240. }
  241. keyix = rx_stats->rs_keyix;
  242. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  243. ieee80211_has_protected(fc)) {
  244. rxs->flag |= RX_FLAG_DECRYPTED;
  245. } else if (ieee80211_has_protected(fc)
  246. && !decrypt_error && skb->len >= hdrlen + 4) {
  247. keyix = skb->data[hdrlen + 3] >> 6;
  248. if (test_bit(keyix, common->keymap))
  249. rxs->flag |= RX_FLAG_DECRYPTED;
  250. }
  251. if (ah->sw_mgmt_crypto &&
  252. (rxs->flag & RX_FLAG_DECRYPTED) &&
  253. ieee80211_is_mgmt(fc))
  254. /* Use software decrypt for management frames. */
  255. rxs->flag &= ~RX_FLAG_DECRYPTED;
  256. }
  257. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  258. int ath9k_cmn_padpos(__le16 frame_control)
  259. {
  260. int padpos = 24;
  261. if (ieee80211_has_a4(frame_control)) {
  262. padpos += ETH_ALEN;
  263. }
  264. if (ieee80211_is_data_qos(frame_control)) {
  265. padpos += IEEE80211_QOS_CTL_LEN;
  266. }
  267. return padpos;
  268. }
  269. EXPORT_SYMBOL(ath9k_cmn_padpos);
  270. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  271. {
  272. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  273. if (tx_info->control.hw_key) {
  274. if (tx_info->control.hw_key->alg == ALG_WEP)
  275. return ATH9K_KEY_TYPE_WEP;
  276. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  277. return ATH9K_KEY_TYPE_TKIP;
  278. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  279. return ATH9K_KEY_TYPE_AES;
  280. }
  281. return ATH9K_KEY_TYPE_CLEAR;
  282. }
  283. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  284. static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
  285. enum nl80211_channel_type channel_type)
  286. {
  287. u32 chanmode = 0;
  288. switch (chan->band) {
  289. case IEEE80211_BAND_2GHZ:
  290. switch (channel_type) {
  291. case NL80211_CHAN_NO_HT:
  292. case NL80211_CHAN_HT20:
  293. chanmode = CHANNEL_G_HT20;
  294. break;
  295. case NL80211_CHAN_HT40PLUS:
  296. chanmode = CHANNEL_G_HT40PLUS;
  297. break;
  298. case NL80211_CHAN_HT40MINUS:
  299. chanmode = CHANNEL_G_HT40MINUS;
  300. break;
  301. }
  302. break;
  303. case IEEE80211_BAND_5GHZ:
  304. switch (channel_type) {
  305. case NL80211_CHAN_NO_HT:
  306. case NL80211_CHAN_HT20:
  307. chanmode = CHANNEL_A_HT20;
  308. break;
  309. case NL80211_CHAN_HT40PLUS:
  310. chanmode = CHANNEL_A_HT40PLUS;
  311. break;
  312. case NL80211_CHAN_HT40MINUS:
  313. chanmode = CHANNEL_A_HT40MINUS;
  314. break;
  315. }
  316. break;
  317. default:
  318. break;
  319. }
  320. return chanmode;
  321. }
  322. /*
  323. * Update internal channel flags.
  324. */
  325. void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
  326. struct ath9k_channel *ichan)
  327. {
  328. struct ieee80211_channel *chan = hw->conf.channel;
  329. struct ieee80211_conf *conf = &hw->conf;
  330. ichan->channel = chan->center_freq;
  331. ichan->chan = chan;
  332. if (chan->band == IEEE80211_BAND_2GHZ) {
  333. ichan->chanmode = CHANNEL_G;
  334. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  335. } else {
  336. ichan->chanmode = CHANNEL_A;
  337. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  338. }
  339. if (conf_is_ht(conf))
  340. ichan->chanmode = ath9k_get_extchanmode(chan,
  341. conf->channel_type);
  342. }
  343. EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
  344. /*
  345. * Get the internal channel reference.
  346. */
  347. struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
  348. struct ath_hw *ah)
  349. {
  350. struct ieee80211_channel *curchan = hw->conf.channel;
  351. struct ath9k_channel *channel;
  352. u8 chan_idx;
  353. chan_idx = curchan->hw_value;
  354. channel = &ah->channels[chan_idx];
  355. ath9k_cmn_update_ichannel(hw, channel);
  356. return channel;
  357. }
  358. EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
  359. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  360. struct ath9k_keyval *hk, const u8 *addr,
  361. bool authenticator)
  362. {
  363. struct ath_hw *ah = common->ah;
  364. const u8 *key_rxmic;
  365. const u8 *key_txmic;
  366. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  367. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  368. if (addr == NULL) {
  369. /*
  370. * Group key installation - only two key cache entries are used
  371. * regardless of splitmic capability since group key is only
  372. * used either for TX or RX.
  373. */
  374. if (authenticator) {
  375. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  376. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  377. } else {
  378. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  379. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  380. }
  381. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  382. }
  383. if (!common->splitmic) {
  384. /* TX and RX keys share the same key cache entry. */
  385. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  386. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  387. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  388. }
  389. /* Separate key cache entries for TX and RX */
  390. /* TX key goes at first index, RX key at +32. */
  391. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  392. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  393. /* TX MIC entry failed. No need to proceed further */
  394. ath_print(common, ATH_DBG_FATAL,
  395. "Setting TX MIC Key Failed\n");
  396. return 0;
  397. }
  398. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  399. /* XXX delete tx key on failure? */
  400. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  401. }
  402. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  403. {
  404. int i;
  405. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  406. if (test_bit(i, common->keymap) ||
  407. test_bit(i + 64, common->keymap))
  408. continue; /* At least one part of TKIP key allocated */
  409. if (common->splitmic &&
  410. (test_bit(i + 32, common->keymap) ||
  411. test_bit(i + 64 + 32, common->keymap)))
  412. continue; /* At least one part of TKIP key allocated */
  413. /* Found a free slot for a TKIP key */
  414. return i;
  415. }
  416. return -1;
  417. }
  418. static int ath_reserve_key_cache_slot(struct ath_common *common)
  419. {
  420. int i;
  421. /* First, try to find slots that would not be available for TKIP. */
  422. if (common->splitmic) {
  423. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  424. if (!test_bit(i, common->keymap) &&
  425. (test_bit(i + 32, common->keymap) ||
  426. test_bit(i + 64, common->keymap) ||
  427. test_bit(i + 64 + 32, common->keymap)))
  428. return i;
  429. if (!test_bit(i + 32, common->keymap) &&
  430. (test_bit(i, common->keymap) ||
  431. test_bit(i + 64, common->keymap) ||
  432. test_bit(i + 64 + 32, common->keymap)))
  433. return i + 32;
  434. if (!test_bit(i + 64, common->keymap) &&
  435. (test_bit(i , common->keymap) ||
  436. test_bit(i + 32, common->keymap) ||
  437. test_bit(i + 64 + 32, common->keymap)))
  438. return i + 64;
  439. if (!test_bit(i + 64 + 32, common->keymap) &&
  440. (test_bit(i, common->keymap) ||
  441. test_bit(i + 32, common->keymap) ||
  442. test_bit(i + 64, common->keymap)))
  443. return i + 64 + 32;
  444. }
  445. } else {
  446. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  447. if (!test_bit(i, common->keymap) &&
  448. test_bit(i + 64, common->keymap))
  449. return i;
  450. if (test_bit(i, common->keymap) &&
  451. !test_bit(i + 64, common->keymap))
  452. return i + 64;
  453. }
  454. }
  455. /* No partially used TKIP slots, pick any available slot */
  456. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  457. /* Do not allow slots that could be needed for TKIP group keys
  458. * to be used. This limitation could be removed if we know that
  459. * TKIP will not be used. */
  460. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  461. continue;
  462. if (common->splitmic) {
  463. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  464. continue;
  465. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  466. continue;
  467. }
  468. if (!test_bit(i, common->keymap))
  469. return i; /* Found a free slot for a key */
  470. }
  471. /* No free slot found */
  472. return -1;
  473. }
  474. /*
  475. * Configure encryption in the HW.
  476. */
  477. int ath9k_cmn_key_config(struct ath_common *common,
  478. struct ieee80211_vif *vif,
  479. struct ieee80211_sta *sta,
  480. struct ieee80211_key_conf *key)
  481. {
  482. struct ath_hw *ah = common->ah;
  483. struct ath9k_keyval hk;
  484. const u8 *mac = NULL;
  485. int ret = 0;
  486. int idx;
  487. memset(&hk, 0, sizeof(hk));
  488. switch (key->alg) {
  489. case ALG_WEP:
  490. hk.kv_type = ATH9K_CIPHER_WEP;
  491. break;
  492. case ALG_TKIP:
  493. hk.kv_type = ATH9K_CIPHER_TKIP;
  494. break;
  495. case ALG_CCMP:
  496. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  497. break;
  498. default:
  499. return -EOPNOTSUPP;
  500. }
  501. hk.kv_len = key->keylen;
  502. memcpy(hk.kv_val, key->key, key->keylen);
  503. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  504. /* For now, use the default keys for broadcast keys. This may
  505. * need to change with virtual interfaces. */
  506. idx = key->keyidx;
  507. } else if (key->keyidx) {
  508. if (WARN_ON(!sta))
  509. return -EOPNOTSUPP;
  510. mac = sta->addr;
  511. if (vif->type != NL80211_IFTYPE_AP) {
  512. /* Only keyidx 0 should be used with unicast key, but
  513. * allow this for client mode for now. */
  514. idx = key->keyidx;
  515. } else
  516. return -EIO;
  517. } else {
  518. if (WARN_ON(!sta))
  519. return -EOPNOTSUPP;
  520. mac = sta->addr;
  521. if (key->alg == ALG_TKIP)
  522. idx = ath_reserve_key_cache_slot_tkip(common);
  523. else
  524. idx = ath_reserve_key_cache_slot(common);
  525. if (idx < 0)
  526. return -ENOSPC; /* no free key cache entries */
  527. }
  528. if (key->alg == ALG_TKIP)
  529. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  530. vif->type == NL80211_IFTYPE_AP);
  531. else
  532. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  533. if (!ret)
  534. return -EIO;
  535. set_bit(idx, common->keymap);
  536. if (key->alg == ALG_TKIP) {
  537. set_bit(idx + 64, common->keymap);
  538. if (common->splitmic) {
  539. set_bit(idx + 32, common->keymap);
  540. set_bit(idx + 64 + 32, common->keymap);
  541. }
  542. }
  543. return idx;
  544. }
  545. EXPORT_SYMBOL(ath9k_cmn_key_config);
  546. /*
  547. * Delete Key.
  548. */
  549. void ath9k_cmn_key_delete(struct ath_common *common,
  550. struct ieee80211_key_conf *key)
  551. {
  552. struct ath_hw *ah = common->ah;
  553. ath9k_hw_keyreset(ah, key->hw_key_idx);
  554. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  555. return;
  556. clear_bit(key->hw_key_idx, common->keymap);
  557. if (key->alg != ALG_TKIP)
  558. return;
  559. clear_bit(key->hw_key_idx + 64, common->keymap);
  560. if (common->splitmic) {
  561. ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
  562. clear_bit(key->hw_key_idx + 32, common->keymap);
  563. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  564. }
  565. }
  566. EXPORT_SYMBOL(ath9k_cmn_key_delete);
  567. static int __init ath9k_cmn_init(void)
  568. {
  569. return 0;
  570. }
  571. module_init(ath9k_cmn_init);
  572. static void __exit ath9k_cmn_exit(void)
  573. {
  574. return;
  575. }
  576. module_exit(ath9k_cmn_exit);