key.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Key management related functions.
  4. *
  5. * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
  6. * Copyright (c) 2010, ST-Ericsson
  7. */
  8. #include <linux/etherdevice.h>
  9. #include <net/mac80211.h>
  10. #include "key.h"
  11. #include "wfx.h"
  12. #include "hif_tx_mib.h"
  13. static int wfx_alloc_key(struct wfx_dev *wdev)
  14. {
  15. int idx;
  16. idx = ffs(~wdev->key_map) - 1;
  17. if (idx < 0 || idx >= MAX_KEY_ENTRIES)
  18. return -1;
  19. wdev->key_map |= BIT(idx);
  20. return idx;
  21. }
  22. static void wfx_free_key(struct wfx_dev *wdev, int idx)
  23. {
  24. WARN(!(wdev->key_map & BIT(idx)), "inconsistent key allocation");
  25. wdev->key_map &= ~BIT(idx);
  26. }
  27. static u8 fill_wep_pair(struct hif_wep_pairwise_key *msg,
  28. struct ieee80211_key_conf *key, u8 *peer_addr)
  29. {
  30. WARN(key->keylen > sizeof(msg->key_data), "inconsistent data");
  31. msg->key_length = key->keylen;
  32. memcpy(msg->key_data, key->key, key->keylen);
  33. ether_addr_copy(msg->peer_address, peer_addr);
  34. return HIF_KEY_TYPE_WEP_PAIRWISE;
  35. }
  36. static u8 fill_wep_group(struct hif_wep_group_key *msg,
  37. struct ieee80211_key_conf *key)
  38. {
  39. WARN(key->keylen > sizeof(msg->key_data), "inconsistent data");
  40. msg->key_id = key->keyidx;
  41. msg->key_length = key->keylen;
  42. memcpy(msg->key_data, key->key, key->keylen);
  43. return HIF_KEY_TYPE_WEP_DEFAULT;
  44. }
  45. static u8 fill_tkip_pair(struct hif_tkip_pairwise_key *msg,
  46. struct ieee80211_key_conf *key, u8 *peer_addr)
  47. {
  48. u8 *keybuf = key->key;
  49. WARN(key->keylen != sizeof(msg->tkip_key_data)
  50. + sizeof(msg->tx_mic_key)
  51. + sizeof(msg->rx_mic_key), "inconsistent data");
  52. memcpy(msg->tkip_key_data, keybuf, sizeof(msg->tkip_key_data));
  53. keybuf += sizeof(msg->tkip_key_data);
  54. memcpy(msg->tx_mic_key, keybuf, sizeof(msg->tx_mic_key));
  55. keybuf += sizeof(msg->tx_mic_key);
  56. memcpy(msg->rx_mic_key, keybuf, sizeof(msg->rx_mic_key));
  57. ether_addr_copy(msg->peer_address, peer_addr);
  58. return HIF_KEY_TYPE_TKIP_PAIRWISE;
  59. }
  60. static u8 fill_tkip_group(struct hif_tkip_group_key *msg,
  61. struct ieee80211_key_conf *key,
  62. struct ieee80211_key_seq *seq,
  63. enum nl80211_iftype iftype)
  64. {
  65. u8 *keybuf = key->key;
  66. WARN(key->keylen != sizeof(msg->tkip_key_data)
  67. + 2 * sizeof(msg->rx_mic_key), "inconsistent data");
  68. msg->key_id = key->keyidx;
  69. memcpy(msg->rx_sequence_counter,
  70. &seq->tkip.iv16, sizeof(seq->tkip.iv16));
  71. memcpy(msg->rx_sequence_counter + sizeof(u16),
  72. &seq->tkip.iv32, sizeof(seq->tkip.iv32));
  73. memcpy(msg->tkip_key_data, keybuf, sizeof(msg->tkip_key_data));
  74. keybuf += sizeof(msg->tkip_key_data);
  75. if (iftype == NL80211_IFTYPE_AP)
  76. // Use Tx MIC Key
  77. memcpy(msg->rx_mic_key, keybuf + 0, sizeof(msg->rx_mic_key));
  78. else
  79. // Use Rx MIC Key
  80. memcpy(msg->rx_mic_key, keybuf + 8, sizeof(msg->rx_mic_key));
  81. return HIF_KEY_TYPE_TKIP_GROUP;
  82. }
  83. static u8 fill_ccmp_pair(struct hif_aes_pairwise_key *msg,
  84. struct ieee80211_key_conf *key, u8 *peer_addr)
  85. {
  86. WARN(key->keylen != sizeof(msg->aes_key_data), "inconsistent data");
  87. ether_addr_copy(msg->peer_address, peer_addr);
  88. memcpy(msg->aes_key_data, key->key, key->keylen);
  89. return HIF_KEY_TYPE_AES_PAIRWISE;
  90. }
  91. static u8 fill_ccmp_group(struct hif_aes_group_key *msg,
  92. struct ieee80211_key_conf *key,
  93. struct ieee80211_key_seq *seq)
  94. {
  95. WARN(key->keylen != sizeof(msg->aes_key_data), "inconsistent data");
  96. memcpy(msg->aes_key_data, key->key, key->keylen);
  97. memcpy(msg->rx_sequence_counter, seq->ccmp.pn, sizeof(seq->ccmp.pn));
  98. memreverse(msg->rx_sequence_counter, sizeof(seq->ccmp.pn));
  99. msg->key_id = key->keyidx;
  100. return HIF_KEY_TYPE_AES_GROUP;
  101. }
  102. static u8 fill_sms4_pair(struct hif_wapi_pairwise_key *msg,
  103. struct ieee80211_key_conf *key, u8 *peer_addr)
  104. {
  105. u8 *keybuf = key->key;
  106. WARN(key->keylen != sizeof(msg->wapi_key_data)
  107. + sizeof(msg->mic_key_data), "inconsistent data");
  108. ether_addr_copy(msg->peer_address, peer_addr);
  109. memcpy(msg->wapi_key_data, keybuf, sizeof(msg->wapi_key_data));
  110. keybuf += sizeof(msg->wapi_key_data);
  111. memcpy(msg->mic_key_data, keybuf, sizeof(msg->mic_key_data));
  112. msg->key_id = key->keyidx;
  113. return HIF_KEY_TYPE_WAPI_PAIRWISE;
  114. }
  115. static u8 fill_sms4_group(struct hif_wapi_group_key *msg,
  116. struct ieee80211_key_conf *key)
  117. {
  118. u8 *keybuf = key->key;
  119. WARN(key->keylen != sizeof(msg->wapi_key_data)
  120. + sizeof(msg->mic_key_data), "inconsistent data");
  121. memcpy(msg->wapi_key_data, keybuf, sizeof(msg->wapi_key_data));
  122. keybuf += sizeof(msg->wapi_key_data);
  123. memcpy(msg->mic_key_data, keybuf, sizeof(msg->mic_key_data));
  124. msg->key_id = key->keyidx;
  125. return HIF_KEY_TYPE_WAPI_GROUP;
  126. }
  127. static u8 fill_aes_cmac_group(struct hif_igtk_group_key *msg,
  128. struct ieee80211_key_conf *key,
  129. struct ieee80211_key_seq *seq)
  130. {
  131. WARN(key->keylen != sizeof(msg->igtk_key_data), "inconsistent data");
  132. memcpy(msg->igtk_key_data, key->key, key->keylen);
  133. memcpy(msg->ipn, seq->aes_cmac.pn, sizeof(seq->aes_cmac.pn));
  134. memreverse(msg->ipn, sizeof(seq->aes_cmac.pn));
  135. msg->key_id = key->keyidx;
  136. return HIF_KEY_TYPE_IGTK_GROUP;
  137. }
  138. static int wfx_add_key(struct wfx_vif *wvif, struct ieee80211_sta *sta,
  139. struct ieee80211_key_conf *key)
  140. {
  141. int ret;
  142. struct hif_req_add_key k = { };
  143. struct ieee80211_key_seq seq;
  144. struct wfx_dev *wdev = wvif->wdev;
  145. int idx = wfx_alloc_key(wvif->wdev);
  146. bool pairwise = key->flags & IEEE80211_KEY_FLAG_PAIRWISE;
  147. WARN(key->flags & IEEE80211_KEY_FLAG_PAIRWISE && !sta, "inconsistent data");
  148. ieee80211_get_key_rx_seq(key, 0, &seq);
  149. if (idx < 0)
  150. return -EINVAL;
  151. k.int_id = wvif->id;
  152. k.entry_index = idx;
  153. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  154. key->cipher == WLAN_CIPHER_SUITE_WEP104) {
  155. if (pairwise)
  156. k.type = fill_wep_pair(&k.key.wep_pairwise_key, key,
  157. sta->addr);
  158. else
  159. k.type = fill_wep_group(&k.key.wep_group_key, key);
  160. } else if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  161. if (pairwise)
  162. k.type = fill_tkip_pair(&k.key.tkip_pairwise_key, key,
  163. sta->addr);
  164. else
  165. k.type = fill_tkip_group(&k.key.tkip_group_key, key,
  166. &seq, wvif->vif->type);
  167. } else if (key->cipher == WLAN_CIPHER_SUITE_CCMP) {
  168. if (pairwise)
  169. k.type = fill_ccmp_pair(&k.key.aes_pairwise_key, key,
  170. sta->addr);
  171. else
  172. k.type = fill_ccmp_group(&k.key.aes_group_key, key,
  173. &seq);
  174. } else if (key->cipher == WLAN_CIPHER_SUITE_SMS4) {
  175. if (pairwise)
  176. k.type = fill_sms4_pair(&k.key.wapi_pairwise_key, key,
  177. sta->addr);
  178. else
  179. k.type = fill_sms4_group(&k.key.wapi_group_key, key);
  180. } else if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  181. k.type = fill_aes_cmac_group(&k.key.igtk_group_key, key, &seq);
  182. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
  183. } else {
  184. dev_warn(wdev->dev, "unsupported key type %d\n", key->cipher);
  185. wfx_free_key(wdev, idx);
  186. return -EOPNOTSUPP;
  187. }
  188. ret = hif_add_key(wdev, &k);
  189. if (ret) {
  190. wfx_free_key(wdev, idx);
  191. return -EOPNOTSUPP;
  192. }
  193. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE |
  194. IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
  195. key->hw_key_idx = idx;
  196. return 0;
  197. }
  198. static int wfx_remove_key(struct wfx_vif *wvif, struct ieee80211_key_conf *key)
  199. {
  200. WARN(key->hw_key_idx >= MAX_KEY_ENTRIES, "corrupted hw_key_idx");
  201. wfx_free_key(wvif->wdev, key->hw_key_idx);
  202. return hif_remove_key(wvif->wdev, key->hw_key_idx);
  203. }
  204. int wfx_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  205. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  206. struct ieee80211_key_conf *key)
  207. {
  208. int ret = -EOPNOTSUPP;
  209. struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
  210. mutex_lock(&wvif->wdev->conf_mutex);
  211. if (cmd == SET_KEY)
  212. ret = wfx_add_key(wvif, sta, key);
  213. if (cmd == DISABLE_KEY)
  214. ret = wfx_remove_key(wvif, key);
  215. mutex_unlock(&wvif->wdev->conf_mutex);
  216. return ret;
  217. }