iwl-sta.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include <linux/etherdevice.h>
  31. #include "iwl-eeprom.h"
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  38. {
  39. int i;
  40. int start = 0;
  41. int ret = IWL_INVALID_STATION;
  42. unsigned long flags;
  43. DECLARE_MAC_BUF(mac);
  44. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
  45. (priv->iw_mode == IEEE80211_IF_TYPE_AP))
  46. start = IWL_STA_ID;
  47. if (is_broadcast_ether_addr(addr))
  48. return priv->hw_params.bcast_sta_id;
  49. spin_lock_irqsave(&priv->sta_lock, flags);
  50. for (i = start; i < priv->hw_params.max_stations; i++)
  51. if (priv->stations[i].used &&
  52. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  53. addr))) {
  54. ret = i;
  55. goto out;
  56. }
  57. IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
  58. print_mac(mac, addr), priv->num_stations);
  59. out:
  60. spin_unlock_irqrestore(&priv->sta_lock, flags);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL(iwl_find_station);
  64. int iwl_send_add_sta(struct iwl_priv *priv,
  65. struct iwl_addsta_cmd *sta, u8 flags)
  66. {
  67. struct iwl_rx_packet *res = NULL;
  68. int ret = 0;
  69. u8 data[sizeof(*sta)];
  70. struct iwl_host_cmd cmd = {
  71. .id = REPLY_ADD_STA,
  72. .meta.flags = flags,
  73. .data = data,
  74. };
  75. if (!(flags & CMD_ASYNC))
  76. cmd.meta.flags |= CMD_WANT_SKB;
  77. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  78. ret = iwl_send_cmd(priv, &cmd);
  79. if (ret || (flags & CMD_ASYNC))
  80. return ret;
  81. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  82. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  83. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  84. res->hdr.flags);
  85. ret = -EIO;
  86. }
  87. if (ret == 0) {
  88. switch (res->u.add_sta.status) {
  89. case ADD_STA_SUCCESS_MSK:
  90. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  91. break;
  92. default:
  93. ret = -EIO;
  94. IWL_WARNING("REPLY_ADD_STA failed\n");
  95. break;
  96. }
  97. }
  98. priv->alloc_rxb_skb--;
  99. dev_kfree_skb_any(cmd.meta.u.skb);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(iwl_send_add_sta);
  103. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  104. {
  105. int i;
  106. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  107. if (!test_and_set_bit(i, &priv->ucode_key_table))
  108. return i;
  109. return -1;
  110. }
  111. int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  112. {
  113. int i, not_empty = 0;
  114. u8 buff[sizeof(struct iwl_wep_cmd) +
  115. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  116. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  117. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  118. struct iwl_host_cmd cmd = {
  119. .id = REPLY_WEPKEY,
  120. .data = wep_cmd,
  121. .meta.flags = CMD_ASYNC,
  122. };
  123. memset(wep_cmd, 0, cmd_size +
  124. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  125. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  126. wep_cmd->key[i].key_index = i;
  127. if (priv->wep_keys[i].key_size) {
  128. wep_cmd->key[i].key_offset = i;
  129. not_empty = 1;
  130. } else {
  131. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  132. }
  133. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  134. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  135. priv->wep_keys[i].key_size);
  136. }
  137. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  138. wep_cmd->num_keys = WEP_KEYS_MAX;
  139. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  140. cmd.len = cmd_size;
  141. if (not_empty || send_if_empty)
  142. return iwl_send_cmd(priv, &cmd);
  143. else
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
  147. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  148. struct ieee80211_key_conf *keyconf)
  149. {
  150. int ret;
  151. unsigned long flags;
  152. spin_lock_irqsave(&priv->sta_lock, flags);
  153. if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
  154. IWL_ERROR("index %d not used in uCode key table.\n",
  155. keyconf->keyidx);
  156. priv->default_wep_key--;
  157. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  158. ret = iwl_send_static_wepkey_cmd(priv, 1);
  159. spin_unlock_irqrestore(&priv->sta_lock, flags);
  160. return ret;
  161. }
  162. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  163. int iwl_set_default_wep_key(struct iwl_priv *priv,
  164. struct ieee80211_key_conf *keyconf)
  165. {
  166. int ret;
  167. unsigned long flags;
  168. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  169. keyconf->hw_key_idx = keyconf->keyidx;
  170. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  171. spin_lock_irqsave(&priv->sta_lock, flags);
  172. priv->default_wep_key++;
  173. if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
  174. IWL_ERROR("index %d already used in uCode key table.\n",
  175. keyconf->keyidx);
  176. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  177. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  178. keyconf->keylen);
  179. ret = iwl_send_static_wepkey_cmd(priv, 0);
  180. spin_unlock_irqrestore(&priv->sta_lock, flags);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(iwl_set_default_wep_key);
  184. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  185. struct ieee80211_key_conf *keyconf,
  186. u8 sta_id)
  187. {
  188. unsigned long flags;
  189. __le16 key_flags = 0;
  190. int ret;
  191. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  192. keyconf->hw_key_idx = keyconf->keyidx;
  193. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  194. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  195. key_flags &= ~STA_KEY_FLG_INVALID;
  196. if (keyconf->keylen == WEP_KEY_LEN_128)
  197. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  198. if (sta_id == priv->hw_params.bcast_sta_id)
  199. key_flags |= STA_KEY_MULTICAST_MSK;
  200. spin_lock_irqsave(&priv->sta_lock, flags);
  201. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  202. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  203. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  204. memcpy(priv->stations[sta_id].keyinfo.key,
  205. keyconf->key, keyconf->keylen);
  206. memcpy(&priv->stations[sta_id].sta.key.key[3],
  207. keyconf->key, keyconf->keylen);
  208. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  209. == STA_KEY_FLG_NO_ENC)
  210. priv->stations[sta_id].sta.key.key_offset =
  211. iwl_get_free_ucode_key_index(priv);
  212. /* else, we are overriding an existing key => no need to allocated room
  213. * in uCode. */
  214. priv->stations[sta_id].sta.key.key_flags = key_flags;
  215. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  216. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  217. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  218. spin_unlock_irqrestore(&priv->sta_lock, flags);
  219. return ret;
  220. }
  221. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  222. struct ieee80211_key_conf *keyconf,
  223. u8 sta_id)
  224. {
  225. unsigned long flags;
  226. __le16 key_flags = 0;
  227. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  228. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  229. key_flags &= ~STA_KEY_FLG_INVALID;
  230. if (sta_id == priv->hw_params.bcast_sta_id)
  231. key_flags |= STA_KEY_MULTICAST_MSK;
  232. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  233. keyconf->hw_key_idx = keyconf->keyidx;
  234. spin_lock_irqsave(&priv->sta_lock, flags);
  235. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  236. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  237. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  238. keyconf->keylen);
  239. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  240. keyconf->keylen);
  241. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  242. == STA_KEY_FLG_NO_ENC)
  243. priv->stations[sta_id].sta.key.key_offset =
  244. iwl_get_free_ucode_key_index(priv);
  245. /* else, we are overriding an existing key => no need to allocated room
  246. * in uCode. */
  247. priv->stations[sta_id].sta.key.key_flags = key_flags;
  248. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  249. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  250. spin_unlock_irqrestore(&priv->sta_lock, flags);
  251. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  252. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  253. }
  254. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  255. struct ieee80211_key_conf *keyconf,
  256. u8 sta_id)
  257. {
  258. unsigned long flags;
  259. int ret = 0;
  260. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  261. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  262. keyconf->hw_key_idx = keyconf->keyidx;
  263. spin_lock_irqsave(&priv->sta_lock, flags);
  264. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  265. priv->stations[sta_id].keyinfo.conf = keyconf;
  266. priv->stations[sta_id].keyinfo.keylen = 16;
  267. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  268. == STA_KEY_FLG_NO_ENC)
  269. priv->stations[sta_id].sta.key.key_offset =
  270. iwl_get_free_ucode_key_index(priv);
  271. /* else, we are overriding an existing key => no need to allocated room
  272. * in uCode. */
  273. /* This copy is acutally not needed: we get the key with each TX */
  274. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  275. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  276. spin_unlock_irqrestore(&priv->sta_lock, flags);
  277. return ret;
  278. }
  279. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  280. struct ieee80211_key_conf *keyconf,
  281. u8 sta_id)
  282. {
  283. unsigned long flags;
  284. int ret = 0;
  285. u16 key_flags;
  286. u8 keyidx;
  287. priv->key_mapping_key = 0;
  288. spin_lock_irqsave(&priv->sta_lock, flags);
  289. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  290. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  291. if (keyconf->keyidx != keyidx) {
  292. /* We need to remove a key with index different that the one
  293. * in the uCode. This means that the key we need to remove has
  294. * been replaced by another one with different index.
  295. * Don't do anything and return ok
  296. */
  297. spin_unlock_irqrestore(&priv->sta_lock, flags);
  298. return 0;
  299. }
  300. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  301. &priv->ucode_key_table))
  302. IWL_ERROR("index %d not used in uCode key table.\n",
  303. priv->stations[sta_id].sta.key.key_offset);
  304. memset(&priv->stations[sta_id].keyinfo, 0,
  305. sizeof(struct iwl_hw_key));
  306. memset(&priv->stations[sta_id].sta.key, 0,
  307. sizeof(struct iwl4965_keyinfo));
  308. priv->stations[sta_id].sta.key.key_flags =
  309. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  310. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  311. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  312. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  313. IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
  314. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, 0);
  315. spin_unlock_irqrestore(&priv->sta_lock, flags);
  316. return ret;
  317. }
  318. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  319. int iwl_set_dynamic_key(struct iwl_priv *priv,
  320. struct ieee80211_key_conf *key, u8 sta_id)
  321. {
  322. int ret;
  323. priv->key_mapping_key = 1;
  324. switch (key->alg) {
  325. case ALG_CCMP:
  326. ret = iwl_set_ccmp_dynamic_key_info(priv, key, sta_id);
  327. break;
  328. case ALG_TKIP:
  329. ret = iwl_set_tkip_dynamic_key_info(priv, key, sta_id);
  330. break;
  331. case ALG_WEP:
  332. ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
  333. break;
  334. default:
  335. IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
  336. ret = -EINVAL;
  337. }
  338. return ret;
  339. }
  340. EXPORT_SYMBOL(iwl_set_dynamic_key);
  341. #ifdef CONFIG_IWLWIFI_DEBUG
  342. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  343. struct iwl_link_quality_cmd *lq)
  344. {
  345. int i;
  346. IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
  347. IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
  348. lq->general_params.single_stream_ant_msk,
  349. lq->general_params.dual_stream_ant_msk);
  350. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  351. IWL_DEBUG_RATE("lq index %d 0x%X\n",
  352. i, lq->rs_table[i].rate_n_flags);
  353. }
  354. #else
  355. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  356. struct iwl_link_quality_cmd *lq)
  357. {
  358. }
  359. #endif
  360. int iwl_send_lq_cmd(struct iwl_priv *priv,
  361. struct iwl_link_quality_cmd *lq, u8 flags)
  362. {
  363. struct iwl_host_cmd cmd = {
  364. .id = REPLY_TX_LINK_QUALITY_CMD,
  365. .len = sizeof(struct iwl_link_quality_cmd),
  366. .meta.flags = flags,
  367. .data = lq,
  368. };
  369. if ((lq->sta_id == 0xFF) &&
  370. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  371. return -EINVAL;
  372. if (lq->sta_id == 0xFF)
  373. lq->sta_id = IWL_AP_ID;
  374. iwl_dump_lq_cmd(priv,lq);
  375. if (iwl_is_associated(priv) && priv->assoc_station_added &&
  376. priv->lq_mngr.lq_ready)
  377. return iwl_send_cmd(priv, &cmd);
  378. return 0;
  379. }
  380. EXPORT_SYMBOL(iwl_send_lq_cmd);